CS-140 Fall 2017 Lab 2 -- Classes and References

In this lab we will learn about classes that reference other classes. Specifically, we will make a Point class which contains fields x and y. Then we will make a Rectangle class which has three fields - the first will be a reference to the point (an object in the Point class) which represents the lower left corner of the rectangle. The second fields will contain the width of the rectangle, and the third field will contain the height.

To Do

Make a lab02 directory in your cs140 directory.

Write the Point class

Create a class called Point in package lab02. This class should have two fields, a double precision floating point field for the x value, and a double precision floating point for the y value of the point.

You should have a creation method for the Point class which accepts two parameters - the initial x value, and the initial y value for the point.

You should also provide a move method which takes two double precision arguments... the amount to move the x coordinate of the point, and the amount to move the y coordinate. Since a move will always be successful, this method can return void.

You will need to provide two very simple "getter" methods, one called getx, which takes no explicit arguments, and returns the current x coordinate of the point, and a similar gety method which returns the current y coordinate of the point.

Finally, code a toString method, which looks like:

  public String toString() {
	return "(" + <x field> + "," + <y field> + ")";
  }
(Where <x field> is whatever name you have chosen for the x coordinate field, and <y field> is your name for the y coordinate field.)

A little explanation is in order here. When you enclose text in double quotes, Java makes a String class object from that text. In Java, you can concatenate (i.e. paste together) two strings by using a plus sign. Java also knows how to convert fields or variables with primitive types into strings. So if the x field has the value 10.0, and the y field has the value 20.0, then the above code will return a string that contains "(10.0,20.0)".

Also, (and we'll learn more about this later), if you try to concatenate a reference field or variable to a string, and there is a public toString method for that object, then Java will automatically invoke the toString method to convert the object into a text format that can be concatenated to a string. Thus, with the above toString method, if we have Java code such as:

  Point p = new Point(10.0,20.0);
  System.out.println("Point is: " + p)

Then Java will print out: Point is (10.0,20.0)

When you have finished writing this class, compile it and make sure it compiles.

Write the Rectangle Class

Create a class called Rectangle in package lab02. This class should have three fields, a reference to a Point object which represents the lower left corner of the rectangle, a double precision floating point value for the width of the rectangle, and a double precision floating point value for the height of the rectangle.

You should have a creation method for the Rectangle class which accepts three parameters - the initial lower left point, and the initial width value, and the initial height value for the rectangle.

You should also provide a move method which takes two double precision arguments... the amount to move the x coordinates of the rectangle, and the amount to move the y coordinates. Since a move will always be successful, this method can return void.

Write a grow method that takes a single double precision floating point argument - the growth factor. This method should multiply both the width and the height of the rectangle by this factor. (Despite the name, this method can be used to either grow or shrink the rectangle. The rectangle grows if the factor is greater than 1, or shrinks if the factor is between 0 and 1.) The grow method should not change the lower left corner of rectangle.

Just for convenience (and to make grading easier), here is a toString method for the Rectangle class:

  public String toString() {
	return "Rectangle from " + <lower left point field> + " width=" + <width field> + " height="+ <height field> ;
  }

Finally, write a contains method in the Rectangle class. The contains method should take a single argument - a reference to a Point object, and return a boolean value, either "false" if the point is outside the rectangle, or "true" if either the point is inside or on the boundary of the rectangle.

Compile your Rectangle class and make sure it compiles clean.

Testing your code

To save time, we have already coded a Tester class which creates a rectangle, and calls various different methods for that rectangle. You can download the tester class from Tester.java. Make sure this ends up in your lab02 directory, and compiles clean with your Point.java and Rectangle.java files.

Once everything compiles clean, run the command:
java -cp . lab02.Tester

If you have coded everything correctly, you should get the following results:

Created Rectangle from (20.0,30.0) width=20.0 height=40.0
Grown by 25%, thats: Rectangle from (20.0,30.0) width=25.0 height=50.0
This rectangle does contain point (40.0,65.0)
This rectangle does not contain point (60.0,95.0)

If you get something different, figure out what you did wrong and fix it.

Creating a zip file for submission

Change directories to the parent of the lab02 directory that contains all of your source code. Then create a zip file with your source code, using the following command:

  > zip -r lab02_<userid>.zip lab02
where <userid> is your bmail user id. This will create a file called lab02_<userid>.zip that contains all of your source files. Please double check and make sure you .java files are in the zipped submission file! Now, go to MyCourses using your web browser, open the CS-140-B1 course, click on "Content"; then "Lab assignments" and then "Lab 02 Submission" and upload your zip file.

Grading

This lab is worth 10 points. You will get the full 10 points if your code compiles without warnings, and produces the correct output. The following are reasons for deductions: