Question

I need help with my code. It keeps getting this error:

Food.java:28: error: method getName in class Food cannot be applied to given types; return String.format (Food - name: %10sThe assignment is:

Create a class to represent a Food object. Use the description provided below in UML.

Food

  • name : String

  • calories : int

  • Food(String, int) // The only constructor. Food name and calories must be

// specified

  • setName(String) : void // Sets the name of the Food

  • getName() : String // Returns the name of the Food

  • setCalories(int) : void // Sets the calories of the Food

  • getCalories() : int // Returns the calories of the Food

  • toString() : String @Override //see Note1:

  • equals(Object) : boolean @Override //see Note2:

NOTE1:

toString() method returns a String with the following format:

“Food - name: %10s | calories: %4d”

NOTE2:

Two instances of Food are equal if, and only if, their names are equal and their calories are equal.

My code is:

import java.util.*;
public class Food {
   private String name;
   private int calories;
  
   public Food (String name, int calories) {
       this.setName(name);
       this.setCalories(calories);
   }
  
   public String setName (String name) {
       this.name = name;
   }
  
   public int setCalories (int calories) {
       this.calories = calories;
   }
  
   public String getName (String name) {
       return this.name;
   }
  
   public int getCalories (int calories) {
       return this.calories;
   }

   public String toString(){
       return String.format("Food - name: %10s | calories: %4d",this.getName(),this.getCalories());
   }
  
   public boolean equals(Object other){
       if (this == other) {
           return true;
       }
       if (other == null || getClass() != other.getClass()) {
           return false;
       }
       Food food = (Food) other;
       return name == food.getName() &&
       calories == food.getCalories;
   }
}

Please help and thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Food {
   private String name;
   private int calories;
  
   public Food (String name, int calories) {
       this.setName(name);
       this.setCalories(calories);
   }
  
   public String setName (String name) {
       this.name = name;
   }
  
   public int setCalories (int calories) {
       this.calories = calories;
   }
  
   public String getName () {
       return this.name;
   }
  
   public int getCalories () {
       return this.calories;
   }

   public String toString(){
       return String.format("Food - name: %10s | calories: %4d", this.getName(), this.getCalories());
   }
  
   public boolean equals(Object other){
       if (this == other) {
           return true;
       }
       if (other == null || getClass() != other.getClass()) {
           return false;
       }
       Food food = (Food) other;
       return name.equals(food.getName()) && (calories == food.getCalories());
   }
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
I need help with my code. It keeps getting this error: The assignment is: Create a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a java class that i need to rewrite in python. this is what i...

    I have a java class that i need to rewrite in python. this is what i have so far: class Publisher: __publisherName='' __publisherAddress=''    def __init__(self,publisherName,publisherAddress): self.__publisherName=publisherName self.__publisherAddress=publisherAddress    def getName(self): return self.__publisherName    def setName(self,publisherName): self.__publisherName=publisherName    def getAddress(self): return self.__publisherAddress    def setAddress(self,publisherAddress): self.__publisherAddress=publisherAddress    def toString(self): and here is the Java class that i need in python: public class Publisher { //Todo: Publisher has a name and an address. private String name; private String address; public Publisher(String...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • I am getting an error from step 3 where it says "constructor Star in class Star...

    I am getting an error from step 3 where it says "constructor Star in class Star cannot be applied to given types; required: no arguments found: int, int, int reason: actual and formal argument lists differ in length." please lmk why.

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • How to create a constructor that uses parameters from different classes? I have to create a...

    How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT