Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In order to manage their illness, people with Type 1 Diabetes need to calculate the carbohydrates in the foods they eat so that they can

In order to manage their illness, people with Type 1 Diabetes need to calculate the carbohydrates in the foods they eat so that they can then inject the correct amount of insulin to process the food.

Any food with less than 1 gram of carbohydrates per serving can legally be called "zero carb". A diabetic can eat any amount of zero carb foods.

Make a subclass Food of the Product class we created in class. Besides the description and price inherited from Product, Food also has an instance variable carbs (a double). Do not redefine price and description variables. Call the super class to initialize them.

Food has methods:

public double getCarbs()

public boolean isZeroCarb() - which tells if this Food can be considered zero carb. A Food is considered zero carb if it has less than 1 gram of carbohydrates per serving. Return true if the Food is zero carb. Otherwise, return false.

public String getDescription() - overrides the getDescription method in Product to also include the number of carbs. Call the getDescription method in the super class and add the new information on the end. The return string will look like this: spinach carbs=0.2.

Provide Javadoc.

Testers

/** * Test Food class */ public class FoodTester { public static void main(String[] args) { Food berries = new Food("Strawberries", 4.95, 10); Food cheese = new Food("String Cheese", 5.50, 1); Food spinach = new Food("Spinach",3.95, 0.2); Food cucumber = new Food("Cucumber", 1.50, 0.9); Food rice = new Food("Rice", 4.95, 45.0); System.out.println(rice.getCarbs()); System.out.println("Expected: 45"); System.out.println(spinach.getCarbs()); System.out.println("Expected: 0.2"); System.out.println(cheese.isZeroCarb()); System.out.println("Expected: false"); System.out.println(berries.isZeroCarb()); System.out.println("Expected: false"); System.out.println(spinach.isZeroCarb()); System.out.println("Expected: true"); System.out.println(cucumber.isZeroCarb()); System.out.println("Expected: true"); System.out.println(spinach.getDescription()); System.out.println("Expected: Spinach carbs=0.2"); System.out.println(cheese.getDescription()); System.out.println("Expected: String Cheese carbs=1.0"); } } 

Product.java

/** * Models a product wih a price and description */ public class Product { private double price; private String description; /** * Constructs a Product with a price and description * @param thePrice the price of this product * @param theDescription the description of this * product */ public Product(String theDescription, double thePrice ) { price = thePrice; description = theDescription; } /** * Gets the price of this Product * @return the price of this object */ public double getPrice() { return price; } /** * Gets the description of this Product * @return the description of this Product */ public String getDescription() { return description; } /** * Reduce the price by the given percent * @param percent the pecentage to reduce the price by */ public void reducePrice(double percent) { price = price - price * percent/100; } /** * Increase the price of the Product by the * given percent * @param percent the percentage to increase the * price by */ public void increasePrice(double percent) { price = price * (1.0 + percent/100); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions