CS46A Fall 18

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:

Override this method

Provide 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.

12A draft:
12A final:

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.

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 getCmykColor() method. Code setCmykColor as a stub. Do not override setColor until the final


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 this 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 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

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

12C draft:
12C final: