Due by the end of Tuesday, November 14
Today we are going to work with overloading the constructor, static variables and methods, and the toString method.
In particular, we are going to define a data type class called Point, which represents a coloured dot on a screen. The dot needs to track how far across the screen it is, how far down the screen it is, and what colour it is.
Create a new project named L08 with a program named TestingPoints. Replace the auto-generated contents of the TestingPoint class with the contents of the starter file linked above.
Note: TestingPoints.java is the main program to test your implemented class Point and sometimes is called the driver program. You are not supposed to change TestingPoints.java at all or submit it. So, DON'T CHANGE IT (except possibly to add/change/remove the package command, or to comment out/uncomment code).
Create a new class file called Point within the same folder. (Not a main class file.) ALL your code should be written in the new file called Point.java (that has no main method!).
As you create the following items, keep in mind the meanings of the words static and final.
- static means that it doesn't matter which object you ask; the result is always the same. These values/methods are associated with the class as a whole, and are sometimes referred to as class variables/constants/methods.
- final means that the item gets one value when it's created and that value never changes. These values are sometimes referred to as constants.
Once you have created Point.java, add four instance variables to it.
I will refer to the across and down instance variables as the Point's coordinates.
Next create a class constant for the default colour:
Next create a class variable to hold the number we're going to use as the ID of the next dot we create. The first point will have a pointID of 1, and that number is going to increase by one every time we create a Point.
Activate the code in TestPoint that declares the Point variables and prints out the default colour, if you haven't already. There should be no errors in the program. If there are, then you did something wrong in Point.java.
You should see the output:
In this activity we are going to create four constructors. The purpose of a constructor, recall, is to give values to every instance variable/constant in the class.
We will start with our primary constructor, which is told the Points coordinates (how far across and down) and its colour. We are going to allow any String for a colour (even stupid ones like "Dog"), but we will not accept negative numbers for the coordinates.
You might be wondering about ID
!
We are going to set pointID
using the class variable nextPointID.
It is not included in the method header because we don't want the user
to control it. Rather we want to set it automatically.
This Point's pointID
will be set to nextPointID,
and then nextPointID is incremented.
We will now create a secondary constructor for the class. This constructor is only given the Point's coordinates. Its colour is set to the default colour.
Keep in mind what we said about secondary constructors in class (and the course notes). All secondary constructors should be a single line. That line consists of the word this followed by a parenthesized list of the values the primary constructor needs.
Next, create two more secondary constructors, one with just the Point's colour, and the other with no information. The default coordinates are 0 and 0 (and so there's no need to name them as class constants, tho' that wouldn't be wrong).
Finally, change the instance variable pointID to an instance constant.
Now you can remove the comment for Activity #2 in TestingPoints.java. There should be no errors in the program. If there are, then you did something wrong in Point.java. In addition to what you saw above, you should see the output:
The third activity is to add getters and setters for all instance variables. That is, getAcross, setAcross, getDown, setDown, getColour, setColour. Also add a method to get the Point's pointID, getPointID. The setters have the same restrictions as the constructors, but remember that a setter should not change the value if the request is invalid.
Activate the code for activity 3. You should see the following output:
The fourth task of this lab is to write a toString() method. This method is going to require an @Override written just above the header and outside of the Javadoc. Why is this? It is because every class in Java includes another class called Object. That's just the way it is. The Object class has a method called toString() as well. By providing the @Override you are telling Java that you realize you are going to use your method toString() instead of the one in Object. The toString() method is of type String and takes no parameters, so its header looks like:
public String toString()
The body of the method is dedicated to the creation of a String which will be returned by the method, and then usually displayed by System.out.println(). So, this method just returns the constructed string WITHOUT printing it. Your task is to construct the String appearing in the output below.
In addition to all that you saw above, you should now see:
Submit this/these files:
You will be graded on the following: