I need help with my code. It keeps getting this error:
The 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!
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.
I need help with my code. It keeps getting this error: The assignment is: Create a...
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 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 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 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; 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 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 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 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; 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 { 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)...