所以基本上我有一个程序,我将pc添加到数组列表中,并且必须选择其中最好的一个(取决于分数/价格数学运算),这意味着得分为10的PC和价格5分比10分和10分的分数要好。问题是布尔(isBetterThan)方法必须实现,并以某种方式用于比较它们,我无法弄清楚究竟如何)。 这是迄今为止的代码:
package filipapp;
public interface Better {
public double getPrice();
public void setPrice(double price);
public int getScore();
public void setScore(int score);
public boolean isBetterThan(Object obj);
}
类
package filipapp;
public class Computer implements Better {
private String model;
private double price;
private int score;
public Computer (String model,double price,int score){
this.model = model;
this.price = price;
this.score = score;
}
@Override
public double getPrice(){
return price;
}
@Override
public void setPrice(double price){
this.price = price;
}
@Override
public int getScore(){
return score;
}
@Override
public void setScore(int score){
this.score = score;
}
@Override
public boolean isBetterThan(Object obj){
obj = getScore()/getPrice();
return true;
}
@Override
public String toString(){
return ("Model "+model+" at price: "+price+" with score: "+score);
}
}
GUI
package filipapp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FilipApp extends JFrame implements ActionListener {
private JLabel l1,l2,l3;
private JTextField t1,t2,t3;
private JPanel p1,p2,p3;
private JButton b1,b2;
private JTextArea area;
private JScrollPane scroll;
public Computer p;
ArrayList <Computer> pc = null;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
new FilipApp().setVisible(true);
}
});
}
public FilipApp(){
super("The best PC");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
Container container = this.getContentPane();
p1 = new JPanel();
p1.setLayout(new GridLayout(3,2));
l1 = new JLabel("Model",JLabel.CENTER);
p1.add(l1);
t1 = new JTextField(15);
p1.add(t1);
l2 = new JLabel("Price",JLabel.CENTER);
p1.add(l2);
t2 = new JTextField(15);
p1.add(t2);
l3 = new JLabel("Score",JLabel.CENTER);
p1.add(l3);
t3 = new JTextField(15);
p1.add(t3);
container.add(p1,BorderLayout.PAGE_START);
p2 = new JPanel();
b1 = new JButton("Add");
b1.addActionListener(this);
p2.add(b1);
b2 = new JButton("Best");
b2.addActionListener(this);
p2.add(b2);
container.add(p2,BorderLayout.CENTER);
p3 = new JPanel();
area = new JTextArea(15,35);
scroll = new JScrollPane(area ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p3.add(scroll);
container.add(p3,BorderLayout.PAGE_END);
pc = new ArrayList <>();
}
@Override
public void actionPerformed(ActionEvent e){
try{
Object source = e.getSource();
if (source == b1){
p = new Computer(t1.getText(),Double.parseDouble(t2.getText()),Integer.parseInt(t3.getText()));
pc.add(p);
area.append("Added PC \n"+p.toString()+"\n");
t1.setText("");t2.setText("");t3.setText("");
}
else if (source == b2 && pc.isEmpty()){
area.append("Empty List \n");
}
else if (source == b2){
for (Computer element : pc){
element.isBetterThan(element);
area.append("Best PC: "+element.toString());
}
}
}catch(NumberFormatException ex){
System.out.println("Error in input data");
}
}
}
答案 0 :(得分:0)
在yout循环中,您应该使用速度变量作为最佳项目。
else if (source == b2){
Computer best = pc.get(0); // Depend on your logic, you should verify if the pc list is empty or not before get(0)
for (int i = 1; i < pc.size(); i++){
Computer element = pc.get(i);
if (element.isBetterThan(best)) {
best = element;
}
}
// When loop exits, you have access to the best pc
area.append("Best PC: "+best.toString());
}
答案 1 :(得分:0)
首先,如果它是更好的&#34;而另一个是主观的,我发现你使用getScore()/getPrice()
进行判断,所以我会继续假设没问题。
首先要理解的是isBetterThan
正在将this
Computer
对象与另一个计算机对象进行比较。所以启动界面应该看起来像
public boolean isBetterThan(Computer comp);
然后用你的除法逻辑来表示它看起来像
public boolean isBetterThan(Computer comp){
if((this.getScore()/this.getPrice()) <
comp.getScore()/comp.getPrice()) {
return false;
}
return true;
}