Note: in this homework, we deduct points
3A
Complete the PiggyBank
class which models a child's piggy bank. Copy it and the tester from Codecheck. We need to be able to add and remove a specified amount from the PiggyBank
. We also need to be able to get the total in the piggy bank.
What does the class need to remember? That is the instance variable.
PiggyBank
has a no-argument constructor which initializes the instance variable (total) to 0. Remember that a constructor has the same name as the class.
It has the following methods:
public void add(double amount)
adds this amount to the total for this PiggyBank
public void remove(double amount)
subtracts this amount from the total for this PiggyBank
public double getTotal()
gets the total amount in this PiggyBank
Provide Javadoc for the class, the constructor, methods, parameters and return values.
For the draft, provide the instance variable and implement the constructor to initialize the instance variable. Provide Javadoc at the top of the class and for the constructor
3B
Create a RobotFriend
class. RobotFriend
is a new toy just on the market. Copy the starter class and the tester form Codecheck. A RobotFriend
has a name and replies to various commands
Provide a constructor that takes the name of the RobotFriend
as a parameter. Remember that a constructor has the same name as the class.
What does the class need to remember? That is the instance variable.
Provide the following methods:
public String getName()
gets the name of this RobotFriend
public void setName(String newName)
sets a new name for this RobotFriend
public String whatDoYouNeed()
returns a string consisting of name + " needs a battery charge" where name is the name of this RobotFriend
(wich was supplied in the constructor)public String doCommand(String whatToDo)
returns a string consisting of "Your friend " + name + " does not " + whatToDo where name is the name of this RobotFriend
and whatToDo is the parameterSupply Javadoc for the class itself (at the top), for the constructor and all the methods
You can use the String concat method to join strings together, but I find the easiest thing to do is use the + operator. Look at this example of combining two Strings to make a third. I want to combine the phrase "My cat is " with the variable containing my cat's name and then a period.
String cat = "Corky" String message = "My cat is " + cat +".";
This is called concatenation.
What is a stub? A stub has a method header complete with braces. If the method is void, that is all you need. If not, then you need to return a temporary dummy value: null for Strings and other objects, 0 for doubles and integers.
Here is the stub for whatDoYouNeed()
public String whatDoYouNeed() { return null; }
Provide Javadoc for the class, the constructor, methods, parameters and return values.
3C
In this exercise, we are going to revisit the snowman from homework2c. In that assignment you wrote a class SnowmanDrawing
with a main method and did all the drawing in it. But there is a problem with that design: If you wanted to draw a second snowman (or a third or a fourth), you would have to recalculate all the coordinates and repeat all the lines of code. Not good. The solution to this problem is to create a Snowman
class that models a snowman at a given (x,y) coordinate and can draw itself. Then that class can be used in an application to draw an many Snowman
objects as you want. I provide you with a class SnowmanViewer
which is an application that uses the Snowman
class. There is not starter class for Snowman
. You will write all the code. But be sure to copy SnowmanViewer
from Codecheck.
Snowman
has have a constructor that takes the x and y coordinates of the upper left hand corner of the rectangular part of the hat as parameters. Do not draw in the constructor.
Snowman
has a draw method that can draw the Snowman
at its x,y coordinates. It takes no parameters
draw method specifications:
Provide Javadoc for the class itself, the constructor and the draw method.
Remember you need to import the graphics classes into the Bluej project.
If you had trouble with hw2c, then you can look at the solution in the Modules on Canvas.
Snowman's
draw method will have less that 20 lines of code. If you have a lot more than that. You are doing something wrong - even if it passes Codecheck.
SnowmanViewer
draws several Snowmans
at different locations.
For the draft, provide the constructor, instance variables and a draw method that draws the hat (hatbox and brim). Provide the Javadoc