好的,我正在尝试创建一个汉堡包对象,它必须有一系列浇头和其他东西。每次我尝试为每个对象的数组赋值时,我都会收到编译错误。这是班级
/*
This Hamburger class is to define how to make a Hamburger sandwich
Then this class will be used to create a Hamburger object in the tester class
Fields & their purpose
4. toppings – an array of Strings storing the toppings on the burger.
*/
public class Hamburger {
//declear private fields
private int weight;
private String doneness;
private boolean cheese;
private String[] toppings;
//creat arrays for each object, then use it in each specific object
//String[] hamburgerToppings = {"l", "m"};
//String[] cheseburgerToppings = {"l", "m"};
//String[] anotherCheseburgerToppings = {"l", "m"};
//create printable array of toppings
//full constructor
public Hamburger(int weightOZ, String done,
boolean WantCheese, String[] topps) {
weight = weightOZ;
doneness = done;
cheese = WantCheese;
toppings = topps;
}
//overloaded no-arg constructor
public Hamburger(){
}
//copy constructor
public Hamburger(Hamburger burger){
weight = burger.weight;
doneness = burger.doneness;
cheese = burger.cheese;
toppings = burger.toppings;
}
//declear getter methods
public int getWeight() {
return weight;
}
public String getDoneness() {
return doneness;
}
这是测试者类
//This class will test & run the Hamburger class
//It has the main method to run the other methods in the Hamburger class
public class HamburgerTester {
public static void main(String[] args) {
//Creating Hamburger objects
Hamburger hamburger = new Hamburger();
Hamburger cheseburger = new Hamburger();
Hamburger anotherCheseburger = new Hamburger(cheseburger);
//Setting Values for hamburger object
hamburger.setWeight(7);
hamburger.setDoneness("Mediuem Rare");
hamburger.setCheese(false);
hamburger.setToppings();
//Setting Values for cheseburger object
cheseburger.setWeight(10);
cheseburger.setDoneness("Well Done");
cheseburger.setCheese(true);
//cheseburger.setToppings();
//anotherCheseburger object is a copy of cheseburger
//there's no need to set special values for it
//Printing Results
System.out.println("Hamburger weight is: " + hamburger.getWeight() +
" doneness: " + hamburger.getDoneness() +
" with/without cheese: " + hamburger.getCheese() +
" Toppings: " + hamburger.getToppings());
System.out.println("cheseburger: " + cheseburger.toString());
System.out.println("cheseburger copy: " + anotherCheseburger.toString());
System.out.println("cheseburger wieght after a bite: " + cheseburger.bite());
System.out.println("cheseburger wieght after a 2 bites: " + cheseburger.bite());
}
}
public boolean getCheese() {
return cheese;
}
public String[] getToppings() {
return toppings;
}
//declear setter methods
public void setWeight(int weightOZ) {
this.weight = weightOZ;
}
public void setDoneness(String done) {
this.doneness = done;
}
public void setCheese(boolean WantCheese) {
this.cheese = WantCheese;
}
public void setToppings(String[] topps) {
this.toppings = topps;
for(int i =0; i < toppings.length; i++) {
if (i > 0)
System.out.print(", ");
System.out.print(toppings[i]);
}
}
//copy method
public Hamburger copy(){
Hamburger burger = new Hamburger(weight, doneness, cheese, toppings);
return burger;
}
//bite methode
public int bite(){
if (weight > 0)
weight--;
return weight;
}
//toString method to print what each Hamburger contains
public String toString(){
String str = "Weight is: " + weight +
"\nDoneness is: " + doneness +
"\nCheese is: " + cheese +
"\nToppings are: " + toppings;
return str;
}
}
感谢任何帮助,谢谢!
答案 0 :(得分:-1)
我试着帮忙,你想做什么?对象Hamburger的getter和setter方法似乎在Hamburgertester类中。