Note: in this homework, we will start deducting points for formatting and poorly named variables.
3A
Write a Person
class. A Person
has properties of name and age. You construct a Person
object by providing the name and age in that order. We want to be able to get and set the name. We want to be able to get the age and to increase the age by 1 when the person has a birthday.
What will the object need to remember? name and age - those are the instance variables.
Person class has this constructor :
public Person (String theName, int theAge)
- Constructs a new person with the given name and age. Remember that the job of the constructor is to initialize the instance variablesIt has these methods.
public String getName()
- Gets the name of this Personpublic int getAge()
- Gets the age of this Personpublic void setName(String newName)
- Sets the new name for this Personpublic void haveBirthday()
- Adds one to this Person's ageThe methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation. The stub for an accessor method returns 0 for numbers or null for objects like strings. A stub for a mutator method has no body at all. A class with stubs for methods will compile, but it does not yet behave correctly. You still need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications.
Javadoc is also provided for this class. Study how it is done so you can write the required Javadoc in the next problem. Notice that even Javadoc says what the method will do when complete - not what the stud does.
You are given a PersonTester class in Codecheck along with the starter for the Person class. Copy both into your project. PersonTester is an application with a main method. To run your application, right click on it and execute its main method.
For the draft, declare the instance variable. Then complete the constructor and the getName method
B
Complete the class ChocolateKissesStash
to model a stash of Hershy's chocolate kisses. A ChocolateKissesStash
has a maximum capacity (the box you store them in can only hold so many) and a current amount.
What does the ChocolateKissesStash
object need to remember? Those are the instance variables.
Provide a constructor that takes the maximum capacity as a parameter. The constructor initializes the instance variables (two of them).
Provide methods
public int getMaxCapacity()
gets the maximum capacity of this ChocolateKissesStash
public int getCurrentAmount()
gets the number of kisses in the ChocolateKissesStash
public void eat(int amountToEat)
simulates eating the specified number of kisses. (It reduces the current amount by amountToEat)public void buyMore(int amountToBuy)
simulates adding more kisses to the ChocolateKissesStash
Provide Javadoc for all methods and for the class itself. Codecheck will run CheckStyle to check that you have the correct Javadoc. You can look at this URL for some help interpreting the error messages
For the draft, provide the constructor to initialize (to a assign the value to) the instance variables. Write the getMaxCapacity
method. Provide stubs for all other methods and supply the Javadoc for the constructor and all the methods. The Javadoc for the class itself is provided
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 getCurrentAmount()
public int getCurrentAmount() { return 0; }
In the Javadoc, describe what the final version of the method will do, not what the stub does now.
C
In this problem, you will revisit hw2c. In that homework, you drew a snowman on a graphical canvas. Our client wants to use the snowman as his logo and to have several snowmen in different places on the Canvas. You could recalculate all the coordinates needed to draw each one, but then what if the client wants to add another or to change the locations? That's a lot of work. Fortunately object-oriented programming provides a solution - Create one class from which you can make multiple objects in different locations.
Make a Bluej project and select Project->Import ... to add the graphics files to your project. Be sure the folder you import contains the files directly and does not contain a second folder that contains the files. (See the screenshot from hw2C) Create two classes Snowman
and SnowmanViewer
. Copy the code in SnowmanViewer
from Codecheck. You should not modify SnowmanViewer
at all. You will write the Snowman
class yourself.
SnowmanViewer
is an application with a main method. Right click and execute its main method to see your drawing when your code is finished.
In hw2c, you hard-coded the coordinates of the upper left-hand corner of drawing as (50, 10) and calculated all the other values from those. That means the drawing is always at the same location. We want to be able to start the drawing at different locations. To do that, we will write a constructor for the class. The constructor will take the x and y coordinates of the upper, left-hand point of the hat as parameters.
You will calculate all the other coordinates relative to that x, y. For example, the coordinates of the point on the left-side top of the hat are (x,y). The width and height of the hat are both 20. So you create the rectangle representing the hatbox like this: Rectangle hatbox = new Rectangle (x, y, 20, 20)
. Now you can put the hatbox at any location on the Canvas.
Constructor
ublic Snowman(int theX, int theY)
The constructor initializes the instance variableness. Do not draw in the constructor.Method
public void draw()
which will create the drawing as specified below. (The daw method should be able to draw the figure at any location, not just those in the SnowmanViewer)Include Javadoc for constructor and method
Your drawing should follow these specifications.
It is a good idea to figure out the coordinates of each point on a piece of paper before trying to code. I did not follow my own advice. I thought this was simple enough and I was clever enough to figure out the coordinates as I coded. I was so wrong, and it cost me a lot of time.
For the draft, provide instance variables, for x and y. Write the constructor to initialize the instance variables. Add a draw method which will just draw the hatbox and brim. Write the Javadoc for the class, the constructor, and the method.