英文:
Setters and Getters
问题
public class FoodClass {
public static void main(String[] args) {
Food food1 = new Food();
Food food2 = new Food();
food1.setDescription("Olives");
food1.setBarCode("C-234-bb-56");
food2.setDescription("Duncan Hind Cake Mix");
food2.setBarCode("23-d-445-66");
Food food3 = new Food();
Food food4 = new Food();
food3.setDescription("Dog Food");
food3.setBarCode("34-456-77");
food3.setPrice(1.45);
food4.setDescription("Cat Food");
food4.setBarCode("2344-56-2-");
food4.setPrice(2.44);
System.out.println(food1.getDescription() + "\t\t\t\t\t" + food1.getBarCode());
System.out.println(food2.getDescription() + "\t\t\t\t\t" + food2.getBarCode());
System.out.println(food3.getDescription() + "\t\t\t\t\t" + food3.getBarCode() + "\t" + food3.getPrice());
System.out.println(food4.getDescription() + "\t\t\t\t\t" + food4.getBarCode() + "\t" + food4.getPrice());
}
}
public class Food {
private String description;
private String barCode;
private double price;
public void setDescription(String descriptionGiven) {
description = descriptionGiven;
}
public void setBarCode(String barCodeGiven) {
barCode = barCodeGiven;
}
public void setPrice(double priceGiven) {
price = priceGiven;
}
public String getDescription() {
return description;
}
public String getBarCode() {
return barCode;
}
public double getPrice() {
return price;
}
public Food(String foodDescription, String foodBarCode) {
description = foodDescription;
barCode = foodBarCode;
price = 0;
}
public Food(String foodDescription, String foodBarCode, double foodPrice) {
description = foodDescription;
barCode = foodBarCode;
price = foodPrice;
}
public Food() {
description = "";
price = 0;
barCode = "";
}
}
英文:
I have a program that uses setters and getters to return food names, barcode numbers, and prices. I have the program split into two files. The Food.java and the FoodClass.java. My problem is that I keep getting compile errors in my FoodClass.java file and I'm struggling to figure out what I need to fix. here is what I have so far for my FoodClass file code and to create a method for food 1 and 2 and other method for food 3 and 4.
the errors I am getting:
C:\Users\xXxFl\Desktop\FoodClass.java:17: error: cannot find symbol
food1.setDescription( "Olives" );
^
symbol: method setDescription(String)
location: variable food1 of type Food
C:\Users\xXxFl\Desktop\FoodClass.java:20: error: cannot find symbol
food2.setDescription( "Duncan Hind Cake Mix" );
^
symbol: method setDescription(String)
location: variable food2 of type Food
C:\Users\xXxFl\Desktop\FoodClass.java:27: error: cannot find symbol
food3.setDescription( "Dog Food" );
^
symbol: method setDescription(String)
location: variable food3 of type Food
C:\Users\xXxFl\Desktop\FoodClass.java:31: error: cannot find symbol
food4.setDescription( "Cat Food" );
^
symbol: method setDescription(String)
location: variable food4 of type Food
C:\Users\xXxFl\Desktop\FoodClass.java:39: error: cannot find symbol
System.out.println( food3.getDescription() + "\t\t\t\t\t" + food3.getbarCode + food3.getPrice() );
^
symbol: variable getbarCode
location: variable food3 of type Food
C:\Users\xXxFl\Desktop\FoodClass.java:40: error: cannot find symbol
System.out.println( food4.getDescription() + "\t\t\t\t\t" + food4.getbarCode + food4.getPrice() );
^
symbol: variable getbarCode
location: variable food4 of type Food
6 errors
public class FoodClass
{
public static void main(String [] args )
{
//place food1 and food 2 into a String getFoodList method
Food food1 = new Food();
Food food2 = new Food();
food1.setDescription( "Olives" );
food1.setBarCode("C-234-bb-56");
food2.setDescription( "Duncan Hind Cake Mix" );
food2.setBarCode( "23-d-445-66" );
//place food3 and food4 into a string getFoodPrice method
Food food3 = new Food();
Food food4 = new Food();
food3.setDescription( "Dog Food" );
food3.setBarCode("34-456-77");
food3.setPrice( 1.45 );
food4.setDescription( "Cat Food" );
food4.setBarCode( "2344-56-2-" );
food4.setPrice( 2.44 );
System.out.println( food1.getDescription() + "\t\t\t\t\t" + food1.getbarCode() );
System.out.println( food2.getDescription() + "\t\t\t\t\t" + food2.getbarCode() );
System.out.println( food3.getDescription() + "\t\t\t\t\t" + food3.getbarCode + food3.getPrice() );
System.out.println( food4.getDescription() + "\t\t\t\t\t" + food4.getbarCode + food4.getPrice() );
}
}
This is what I have for the Food.java code:
public class Food
{
// Fields
private String description;
private String barCode;
private double price;
public void setDesrciption( String descriptionGiven ) {
description = descriptionGiven;
}
public void setBarCode( String barCodeGiven ) {
barCode = barCodeGiven;
}
public void setPrice(double priceGiven ) {
price = priceGiven;
}
public String getDescription() {
return description;
}
public String getbarCode() {
return barCode;
}
public double getPrice() {
return price;
}
public Food( String foodDescription, String foodBarCode) {
description = foodDescription;
barCode = foodBarCode;
price = 0;
}
public Food( String foodDescription, String foodBarCode, double foodPrice) {
description = foodDescription;
barCode = foodBarCode;
price = foodPrice;
}
public Food() {
description = "";
price = 0;
barCode = "";
}
}
答案1
得分: 3
在 Food
中,setDesrciption
应为 setDescription
,以及 food3.getbarCode
和 food4.getbarCode
应改为 food3.getbarCode()
和 food4.getbarCode()
。
英文:
setDesrciption
in Food
should be setDescription
and food3.getbarCode
and food4.getbarCode
should be food3.getbarCode()
and food4.getbarCode()
.
答案2
得分: 1
问题在于FoodClass.java
和Food.java
中遵循了不统一的命名。
以下两种更改之一可以解决您的问题:
- 在
Food.java
中使用setDescription
而不是setDesrciption
。
或者
- 在
FoodClass.java
中使用setDesrciption
而不是setDescription
。
此外,在FoodClass.java
中,food3.getbarCode
和food4.getbarCode
必须分别改为food3.getbarCode()
和food4.getbarCode()
。
进行这些更改,将生成如下所示的所需输出。
"C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\jbr\bin\java.exe" "-javaagent:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\lib\idea_rt.jar=58409:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\work\trial\out\production\trial FoodClass
Olives C-234-bb-56
Duncan Hind Cake Mix 23-d-445-66
Dog Food 34-456-771.45
Cat Food 2344-56-2-2.44
Process finished with exit code 0
要再次解决此类重构问题,理想情况下,像IntelliJ这样的IDE可以成为您的完美助手。
英文:
Well the problem here is non-uniform naming followed in FoodClass.java
& Food.java
.
Either of the below change must get your job resolved.
- Use
setDescription
instead ofsetDesrciption
inFood.java
.
> or - Use
setDesrciption
instead ofsetDescription
inFoodClass.java
.
Also in FoodClass.java
food3.getbarCode
and food4.getbarCode
must be food3.getbarCode()
& food4.getbarCode()
respectively.
Making this changes, does generate the desired output as below.
"C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\jbr\bin\java.exe" "-javaagent:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\lib\idea_rt.jar=58409:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\work\trial\out\production\trial FoodClass
Olives C-234-bb-56
Duncan Hind Cake Mix 23-d-445-66
Dog Food 34-456-771.45
Cat Food 2344-56-2-2.44
Process finished with exit code 0
Again to resolve such issues of refactoring, ideally an IDE like Intellij can be your perfect helper.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论