L09

Due by the end of Tuesday, November 14

Starter Files:


SUBMIT / CHECK

Today's Activities

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.

Activity #1

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:

This program instantiate some points with the provided information. ------------------------------------------------------------------- The default colour for Points is Black ...press enter...
Activity #2

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:

Coordinate must not be negative. Coordinate must not be negative. Coordinate must not be negative. Coordinate must not be negative. Coordinate must not be negative. Coordinate must not be negative. ...press enter...
Activity #3

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:

A Point: ID: 1 Colour: Blue Across: 1 Down: 3 A Point: ID: 2 Colour: Black Across: 10 Down: 6 A Point: ID: 3 Colour: Red Across: 0 Down: 0 A Point: ID: 4 Colour: Black Across: 0 Down: 0 A Point: ID: 5 Colour: Red Across: 0 Down: 0 A Point: ID: 6 Colour: Orange Across: 4 Down: 0 A Point: ID: 7 Colour: Yellow Across: 0 Down: 3 A Point: ID: 8 Colour: Black Across: 0 Down: 2 A Point: ID: 9 Colour: Black Across: 7 Down: 0 ...press enter... Changing colour: A Point: ID: 1 Colour: Green Across: 1 Down: 3 Changing across: A Point: ID: 2 Colour: Black Across: 56 Down: 6 Changing down: A Point: ID: 3 Colour: Red Across: 0 Down: 40 Failing to change down: Coordinate must not be negative. A Point: ID: 7 Colour: Yellow Across: 0 Down: 3 ...press enter...
Activity #4

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:

Using the toString method: Point #1 is a Green point at 1 across and 3 down. Point #2 is a Black point at 56 across and 6 down. Point #3 is a Red point at 0 across and 40 down. Point #4 is a Black point at 0 across and 0 down. ...press enter...

Submit this/these files:

You will be graded on the following:

  1. All four instance variables have been created and are protected from other classes (private and/or final)
  2. The point identification instance variable has been made a constant
  3. The default colour has been declared and initialized as a class constant
  4. The next pointID variable has been created and initialized as a class variable
  5. The three-argument constructor has been declared properly
  6. ... and assigns correct values to all four IVs (even for "bad" requests)
  7. ... and updates the next pointID variable correctly
  8. The three other constructors are declared properly
  9. ... and each calls the primary constructor
  10. All required getters and setters have been created and do exactly what they're supposed to and no more
  11. toString method has been declared properly (including the @Override)
  12. ... and does what it's supposed to and no more
  13. Correct javadoc comments on all methods
  14. Correct style in all code

SUBMIT   /   CHECK