我在上课时遇到了一些问题。首先,我建立了一个Computer类,该类可以与单独的“ SystemBuilder”类配合使用。班上的一切都很好。我现在正在建立一个单独的类以存储对象数组。此类的要求之一是创建一个对象数组,一个add方法,一个toString方法和一个search方法。
嗯,我完成了toString方法的添加-无法使其正常运行。虽然我了解这里发生的所有事情的基本概念,但类关系却有些混乱。
当我从对象集合类(ComputerStore)中的对象上调用toString函数时,现在似乎正在发生什么,它将从Computer类中调用toString方法并以该格式打印。但是,我不希望这种情况发生在ComputerStore对象上,我只希望这种情况发生在Computer Objects上。
有人可以按照我需要去这里的方式引导我,并对发生的事情有所启发吗?
非常感谢...下面是计算机类,然后是ComputerStore类。
//This program will create Computer objects with different data members
//and will also upgrade those data members based on setters. This program
//also has a depreciation function and upgrade function. This (Computer) is the class
//and the SystemBuilder class is the class used for creating the objects.
public class Computer {
// Data Members - These belong to the class and are private.
// They all go to new objects.
private String cpu;
private int ram;
private String gpu;
private String motherboard;
private String psu;
private double cost;
private int serialnumber;
private double depreciation;
// Initial constructor with no arguments
Computer() {
cpu = "";
ram = 0;
gpu = "";
motherboard = "";
psu = "";
cost = 0.0;
serialnumber = 0;
depreciation = 0.0;
}
// Constructor with data members
Computer(String c, int r, String g, String m, String p, double co, int sn, double d) {
cpu = new String(c);
ram = r;
gpu = new String(g);
motherboard = new String(m);
psu = new String(p);
cost = co;
serialnumber = sn;
depreciation = d;
}
// Getters, allow retrieval of data members from outside of class
public String getCpu() {
return cpu;
}
public int getRam() {
return ram;
}
public String getGpu() {
return gpu;
}
public String getMotherboard() {
return motherboard;
}
public String getPsu() {
return psu;
}
public double getCost() {
return cost;
}
public int getSerialnumber() {
return serialnumber;
}
public double getDepreciation() {
return depreciation;
}
// Setters, allow setting of data members from outside of class
public void setCpu(String c) {
cpu = new String(c);
}
public void setRam(int r) {
ram = r;
}
public void setGpu(String g) {
gpu = new String(g);
}
public void setMotherboard(String m) {
motherboard = new String(m);
}
public void setPsu(String p) {
psu = new String(p);
}
public void setCost(double co) {
cost = co;
}
public void setSerialnumber(int sn) {
serialnumber = sn;
}
public void setDepreciation(double d) {
depreciation = d;
}
// Boolean below will compare computers to see if equal
// based on same motherboard SN#.
public boolean equals(Computer c) {
if (this.serialnumber == (c.serialnumber)) {
return true;
} else {
return false;
}
}
// To string method will print characteristics about object.
public String toString() {
return ("CPU:\t\t" + cpu + "\n" + "RAM:\t\t" + ram + "\n" + "GPU:\t\t" + gpu + "\n" + "Motherboard:\t"
+ motherboard + "\n" + "PSU:\t\t" + psu + "\n" + "Cost:\t\t" + "$" + cost + "\n" + "SN#:\t\t"
+ serialnumber + "\n" + "Depreciation:\t" + "$" + depreciation + " (annually)");
}
// A method to depreciate the cost of the computer
// The formula is observed below, but this is a
// straight line depreciation equation, calculated based
// on the values the user passes into the function. This method
// will show an output of annual depreciation based on useful
// life, entered in "years" by the user.
public void depreciate(double purchasePrice, double salvageValue, double lifeSpanYears) {
double depreciableCost;
double annualDepreciation;
depreciableCost = purchasePrice - salvageValue;
annualDepreciation = depreciableCost / lifeSpanYears;
depreciation = annualDepreciation;
}
// A method to upgrade the ram or the video card
// The method will accpet argumetns for ram (in int) and a gpu (string).
public void upgrade(int newRam, String newGpu) {
ram = newRam;
gpu = new String(newGpu);
}
}
public class ComputerStore {
private Computer[] systems;
private int sysNumbers;
public ComputerStore() {
systems = new Computer[200];
sysNumbers = 0;
}
public void add(String c, int r, String g, String m, String p, double co, int sn, double d)
{
systems[sysNumbers++] = new Computer(c, r, g, m, p, co, sn, d);
}
public String toString() {
String result = "";
for (int i = 0; i < sysNumbers; i++)
result += systems[i].toString() + "\n";
return result;
}
}
答案 0 :(得分:0)
ComputerStore.toString
是文本,每一行都是Computer.toString
。
误导是缩进和{}。
public String toString() {
String result = "";
for (int i = 0; i < sysNumbers; i++)
result += systems[i].toString() + "\n";
return result;
}
更清晰的代码和结果将是:
public String toString() {
StringBuilder result = new StringBuilder("{");
for (int i = 0; i < sysNumbers; i++) {
if (i > 0) {
result.append(", ");
}
result.append(systems[i].getSerialNumber());
}
result.append("}");
return result.toString();
}