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 Product
, Food
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:
public double getVitaminC())
- returns the amount of vitaminC C in this Food
public double howMuch()
- how many servings of this food are needed to meet the minimum daily requirement. If the amount of vitamin C in the Food is 0, return 0. You won't get any vitamin C, no matter how much of the food you eat!Override this method
public String getDescription()
overrides the getDescription
method in Product
to also include the amount of vitamin C. Call the getDescription
method of the super class and add the new information on the end. It will look like this for spinach: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.
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 RGBRectangle
. Do 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.
rgbColor
to "gray". Call setRGBColor rather than doing all the test again in the constructor.Provide methods
setRGBColor(String newColor)
sets the rgbColor
instance variable to the new color and calls the super class setColor method to set the appropriate color. Color.RED if newColor is "red", Color.BLUE is the newColor is "blue", and Color.GREEN is the newColor is "green" If newColor is any other value, set the color to Color.GRAYgetRGBColor()
gets the RGB color of this Rectangle as a string.Override
public void setColor(Color aColor)
It should not do anything. The reason to have it is so that the user can not set the color of the rectangle to an invalid color like GREEN. The color can only be changed using setRGBColor
,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.
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
public void eat(int amountToEat)
- which increases the amount of energy the animal has by amountToEatpublic void move(int amountToMove)
- which decreases the energy the animal has by amountToMove. public int getEnergy()
- which returns the amount of energy the animal has leftNotice 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
eat
method: If the amount the BetterAnimal
eats would set its energy above the max, the energy level is only increased to the max. Also energy is only changed if the amount > 0move
method: Energy changes only if amount > 0. The energy can never be less than 0. If a BetterAnimal
has an energy of 2 and tries to move 5, its energy will be 0.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