logo Practice-It logo

Food

Related Links:
Author: Marty Stepp

Suppose you have a pre-existing class Food that represents items of food that can be eaten at a restaurant. The class has the following data and behavior:

NameDescription
private String kind kind of food such as "hamburger"
private double price price of this food item, such as 1.99
public Food(String kind, double price) makes a food item of the given kind and price
public String getKind() returns the kind of food
public double getPrice() returns the food item's price
public String toString() returns a String such as "hamburger: $1.99"

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. Food items are ordered by the kind of food, case-insensitively. In other words, a food item of kind "Hamburger" comes before "pizza" which comes before "STEAK". If there are two food items of the same kind, they are compared by price, with less expensive food items considered "less than" more expensive ones. If two food items are of the same kind and have the same price, they are considered to be "equal." Your method should not modify the food item's state. You may assume the parameter passed is not null.

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.