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 Product, Food 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:
public double getCarbs()public boolean isZeroCarb() which tells if this Food can be considered zero carb. A Food is considered zero carb if it has less than 1 gram of carbohydrates per serving. Return true if the Food is zero carb. Otherwise, return false.public String getDescription() overrides the getDescription method in Product to also include the number of carbs. Call the getDescription method in the super class and add the new information on the end. The return string will look like this: spinach carbs=0.2Provide 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.
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 Rectangle. Do 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.
primaryColor to "purple". Purple is (159, 0, 197)Provide methods
setPrimaryColor(String newColor) sets the primaryColor 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.YELLOW is the newColor is "yellow"getPrimaryColor()gets the primary 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 GREEN. The color can only be changed using setPimaryColor,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.
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.
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 an 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