180k views
4 votes
Create a class called Food with the following features:

a) Two private instance variables; an int called calories and a string called foodName
b) Constructor to initialize both instance variables to parameters
c) Method to get the calories value
d) Method to set the calories value
e) Methodbool isfattening (int maxCal) that returns true if and only if the calories is greater than maxCal

User Benji
by
8.8k points

1 Answer

0 votes

Final answer:

To create the Food class, define it in a programming language with the specified properties and methods, including a constructor, getter/setter for calories, and an isFattening method to compare the calories with a threshold.

Step-by-step explanation:

To create a class called Food as per the requirements, you would use the following structure in a programming language like Java:

public class Food {
private int calories;
private String foodName;

public Food(int calories, String foodName) {
this.calories = calories;
this.foodName = foodName;
}

public int getCalories() {
return calories;
}

public void setCalories(int calories) {
this.calories = calories;
}

public boolean isFattening(int maxCal) {
return this.calories > maxCal;
}
}
This code provides a blueprint for objects that represent food items, where each object contains information about the item's calorie content and name. The constructor initializes the instance variables, while the 'get' and 'set' methods allow you to access and modify the calorie count. The isFattening method determines if the food exceeds a specified calorie threshold.The subject of this question is Computer Science.In this question, you are asked to create a class called Food with two private instance variables: calories (an int) and foodName (a string). The question also specifies a constructor to initialize the instance variables, as well as methods to get and set the calories value. Additionally, a method called isFattening is required, which returns true if the calories value is greater than a given maxCal.

User Karush Mahajan
by
9.1k points