CS46A Fall 19

12A

Vitamin C is an important nutrient in the human diet. Some foods are high in vitamin C and some are not. The minimum daily requirement for Vitamin C is 65 mg.

Make a subclass Food of the Product class we created in class. The code is in Codecheck.

Besides the description and price  inherited from ProductFood also has a double instance variable representing the milligrams of vitamin C in that food. Do not redefine the price and description variables in the subclass. Call the super class to initialize them

The constructor

public Food(String description, double price, double vitaminC)

Provide these methods:

Override this method

Define a constant called the MDR (minimum daily requirement) of vitamin C for the minimum daily requirement. The constant should be accessible from any class. Be sure to use it in your howMuch method rather hardcoding 65 there.

Provide Javadoc.

For the draft, make Food a subclass of Product. Provide the new instance variable. Write the constructor that takes description, price, and vitamin C in that order. Provide getVitaminC(). Implement the howMuch() method as stub. Be sure to tell what the method will do in the final not what it does in the draft. Do not override 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 RGBRectangle of the Rectangle class which can only have one of the colors, "red", "green", "blue", or "gray". The RGBRectangle remembers its color. RGBRectangle has a String instance variable called rgbColor which specifies the color of this RGBRectangleDo not add any instance variables except the rgbColor. 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 rgbColor instance variable. It then must call the super class setColor() method so the RGBRectangle will be drawn in the correct color. If rgbColor 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 getRGBColor() method. Code setRGBColor 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. You will lose points if you define a setEnergy method.

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: