Question

The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and...

The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and calories (int), along with get/set methods for both. Implement the methods in the Meal class found in Meal.java

public class FoodItem{
private String name;
private int calories;
  
public FoodItem(String iName, int iCals){
name = iName;
calories = iCals;
}
  
public void setName(String newName){
name = newName;
}
  
public void setCalories(int newCals){
calories = newCals;
}
  
public int getCalories(){
return calories;
}
  
public String getName(){
return name;
}
}

//Represents a meal made up of up to maxItems food items
public class Meal{
private FoodItem[] items; //stores all of the food items that are in the meal
private static final int maxItems = 25;
  
//Constructor should initialize all instance variables
public Meal(){

}
  
//Add the given food item to the items array, if there is still room
//If there is no room, do nothing.
public void addFoodItem(FoodItem item){
  
}
  
//Return the FoodItem in the items array that has the largest number of calories
//If there are ties, you may return any of the largest
public FoodItem mostCalories(){
return null; //included so code will compile
}
  
//Computes and returns the total number of calories of all food items in this meal
public int calculateTotalCalories(){
return -1; //included so code will compile
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Represents a meal made up of up to maxItems food items
public class Meal {
    private FoodItem[] items; //stores all of the food items that are in the meal
    private static final int maxItems = 25;
    private int count;

    //Constructor should initialize all instance variables
    public Meal() {
        items = new FoodItem[maxItems];
        count = 0;
    }

    //Add the given food item to the items array, if there is still room
//If there is no room, do nothing.
    public void addFoodItem(FoodItem item) {
        if (count < items.length) {
            items[count++] = item;
        }
    }

    //Return the FoodItem in the items array that has the largest number of calories
//If there are ties, you may return any of the largest
    public FoodItem mostCalories() {
        if (count == 0)
            return null;
        FoodItem maxItem = items[0];
        for (int i = 0; i < count; i++) {
            if (items[i].getCalories() > maxItem.getCalories()) {
                maxItem = items[i];
            }
        }
        return maxItem;
    }

    //Computes and returns the total number of calories of all food items in this meal
    public int calculateTotalCalories() {
        int total = 0;
        for (int i = 0; i < count; i++) {
            total += items[i].getCalories();
        }
        return total;
    }
}
Add a comment
Know the answer?
Add Answer to:
The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and...
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
  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major...

    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access modifier: private Data type: int [ ] Static variables Name: maximumNumberOfCourses Access modifier: private Data type: int Initial Value: 40 Constructor Name: CollegeDegree Access modifier: public Parameters: none (default constructor) Task: sets major to the empty string...

  • Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....

    Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin {    /**     * Constructor for objects of class...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

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