12A
Many people who want to eat a healthy diet will only choose processed foods where the the calories from fat is less than 1/3 of the total calories per serving.
Make a subclass Food
of the Product
class we created in class. It is also in Codecheck.
Besides the description and price inherited from Product, Food also has two instance variables: total calories and calories from fat as ints. Do not redefine price and description variables in the subclass. Call the super class to initialize them
The constructor
public Food(String description, double price, int calories, int caloriesFromFat)
Provide these methods:
public int getCalories()
public int getCaloriesFromFat()
public boolean isAllowed()
which tells if this Food is allowed in the diet. A Food is allowed if the calories from fat is less than 1/3 the total calories per serving. Return true if the Food is allowed. Otherwise, return false.Override this method
public String getDescription()
overrides the getDescription method in Product to also include the percentage of calories from fat. Call the getDescription method in the super class and add the new information on the end as a number with 1 decimal point. For example: One 2 ounce Snickers candy bar has 280 calories and 122 calories form fat. That is 43.6% The return string will look like this: snickers %fat=43.6Provide Javadoc.
For the draft, make Food
a subclass of Product
. Provide the new instance variables. Write the constructor that takes description, price, total calories, and calories from fat in that order. Be sure to initialize all the instance variables. Provide getCalories
and getCaloriesFromFat
. Implement the isAllowed
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 override getDescription
in the draft.
12B
When rendering colors on electronic displays, RGB color mode is used (red, green, blue). But in a print medium, CMYK color mode is used (cyan, magenta, yellow, black)
The Rectangle
class in the graphics package can have an infinite number of colors. Write a subclass CMYKRectangle
of the Rectangle
class which will always be one of the four colors, "cyan", "magenta", "yellow" or "black. CMYKRectangle
has a String instance variable called cmykColor
which specifies the color of this Rectangle
as a String. Do not add any instance variables except the cmykColor. 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 cmykColor instance variable. It then must call the super class setColor()
method so the CMYKRectangle
will be drawn in the correct color. For example, if cmykColor is "yellow", call the super class setColor method with Color.YELLOW.
public CMYKRectangle(int x, int y, int w, int h)
The first constructor takes 4 parameters: the x, y coordinate of the upper left-hand corner and the width and the height as ints. This constructor sets a default value of "cyan" to the instance variable. And calls setColor in the super class with Color.CYANpublic CMYKRectangle(int x, int y, int w, int h, String color)
A second constructor takes 5 parameter: the x, y coordinate of the upper left-hand corner and the width and the height and a String for the color. The constructor should call the setCmykColor
method with the color string to handle setting the color.. Provide methods
setCmykColor(String newColor)
If newColor is any other value besides the four valid strings ("cyan", "magenta", "yellow" or "black), sets cmykColor to "cyan", otherwise sets the cmykColor instance variable to the new color. It then calls the super class setColor method to set the appropriate color. Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK for this rectangle. getCmykColor()
gets the color of this RectangleOverride
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 GRAY. The color can only be changed using setCmykColor,Provide Javadoc for the new and overridden methods.
For the draft, provide the instance variables and implement the constructor with 4 parameters. . Implement the getCmykColor() method. Code setCmykColor as a stub. Do not override setColor until the 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 this 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.
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 EnhancedAnimal
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
EnhancedAnimal
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 > 0EnhancedAnimal
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. You can get a working version from the draft solutions if you need it.
Provide Javadoc for both classes.
For the draft, write the Animal class. Include Javadoc