CS46A Spring 18

12

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 ProductFood 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:

Provide Javadoc.

For the draft, make Food a subclass of Product. Provide the new instance variable, carbs. Write the constructor that takes description, price, and carbs in that order. Be sure to initialize all the instance variables. Provide getCarbs. Implement the isZeroCarb method as stub. Remember that if a stub's return type is boolean, it should return false. Be sure to tell what the method will do in the final not what it does in the draft. Do not include getDescription in the draft.

12A draft:
12A final:

12B

The Rectangle class in the graphics package can have an infinite number of colors. Write a subclass PrimaryRectangle of the Rectangle class which will always be one of the primary colors, "red", "blue", or "yellow". PrimaryRectangle has a String instance variable called primaryColor which specifies the color of this RectangleDo not add any instance variables except the primaryColor. You will lose points if you do.

Provide two constructors:

Each constructor will need to call super to set the instance variables it inherits and then set the primaryColor instance variable. It then must call the super class setColor() method so the PrimaryRectangle will be drawn in the correct color. If primaryColor is "red", call the super class setColor method with Color.RED, etc.

Provide methods

Override

provide Javadoc for the new and overridden methods.

For the draft, provide the instance variables and implement the constructor with 4 parameters. . Implement the getPrimaryColor() method. Code setPrimaryColor as a stub.

12B draft:
12B final:

12C

Animals need energy to move. They get energy from eating food.

Moving consumes energy.

Create a class Animal with a constructor that takes no parameters and has an instance variable:

private int energy;

When an animal is "born," it has one unit of energy.

It has the methods

Notice there is no setEnergy method. Energy is only changed by eating or moving.

It isn't realistic for animals to be able to gather infinite amounts of energy, to have negative energy or to eat or move a negative amount.

Create a subclass BetterAnimal which has a cap on the amount of energy an animal can have. The constructor takes a parameter that specifies a maximum for energy. You will need to save this in another instance variable.

Override the eat and move methods

For the final, Animal will not change, but you still need to submit it.

Provide Javadoc for both classes.

For the draft, write the Animal class. Include Javadoc

12C draft:
12C final: