CS401 Lab 5: Writing Classes


Introduction

In the text and in lecture we saw some examples of using and writing simple classes in Java. We saw the basics of how instance variables, constructors, accessor methods and mutator methods are declared and used. We also saw how to accomplish encapsulation with data hiding by declaring our instance variables to be private and by declaring our instance methods to be public. Although this will not always be the case, it is a good initial rule-of-thumb to use for writing new classes. In this lab you will write a new, simple class and see it work by running it with a simple driver program.


MyRectangle

You will write a simple class that will represent a rectangle. Later on we will see how to do this with graphics, but for now we will just have to use our imaginations for the visual representations of the rectangles.

Instance Variable

There are a couple of ways we could go with this. Our rectangle needs some representation that reflects the dimensions of the sides and its position on the "screen." We could accomplish this with a set of points that store the position of the corners, which minimally would just need to be two opposing corners. Alternatively, we could a single corner point along with the rectangles dimensions. With either approach, we can calculate all the properties of a rectangle. For this lab, we'll go with the second approach. We will call the dimensions width (for the X-dimension) and height (for the Y-dimension). The types of these dimensions could be either floating point (double) or integer (int). Since pixels on the computer display are discrete, we will make these dimensions int. In addition to its size dimensions, a MyRectangle must have a position on the screen. We will base this position on the upper left corner of the MyRectangle. The graphics coordinate space for computers starts at (0,0) and proceeds to the right for positive X and down for positive Y. Call the X position startX and call the Y position startY, and, like the dimension variables, make these both ints. For example, consider the following instance variable values for a MyRectangle:

	startx:		100
	starty:		50
	width:		80
	height:		20

This would define the MyRectangle shown (not exactly to scale) in the figure above and to the right.

We do not want users of MyRectangle to have direct access to the instance variables, so we will declare them as private.

Constructors

Recall that constructor methods are used to create new objects of a given class. They are special methods in that they have no return type (not even void). For the MyRectangle class you will have two constructors. One is a default constructor -- this is used to create objects when no arguments are used. For MyRectangle the default constructor will initialize all 4 of its instance variables to 0. The second constructor will have four parameters (in order): X position, Y position, new width and new height, and it will simply initialize the instance variables from the parameters.

For example, the MyRectangle above could be created by the following:

MyRectangle R = new MyRectangle(100, 50, 80, 20);

and a "default" MyRectangle could be created by the following:

MyRectangle R2 = new MyRectangle();

Accessors

Accessor methods allow us to get information from objects or have them perform tasks that do not alter the objects themselves. There is no special designation for accessor methods; rather we label them as accessors based on what we use them for. For MyRectangle we will have the following accessors:

public int area() 			// return the area of the given MyRectangle
public String toString() 		// return the MyRectangle's information in the form of
					// a String. See sample output for details.
public boolean isInside(int X, int Y)	// Return true if point X, Y is inside the
					// MyRectangle, and false otherwise

Mutators

Mutators allow us to change the data within an object. As with accessors, there is no special designator for mutators; we label them based on what we use them for. For MyRectangle, we will have the following mutators:

public void setSize(int newWidth, int newHeight)// change width and height of
						// this MyRectangle to the values passed in

public void setPosition(int newX, int newY)	// change X and Y position of this
						// MyRectangle to the values passed in

Main Program

When you start working on larger and larger projects, you'll begin working in teams, and different parts of the projects will be delegated to other members of your team. In order to ensure that the program will compile and execute once everyone's contribution is combined, you'll first agree on a set interface between all the different parts of the program. Thus every other member who needs to use your part can be sure you'll implement your interface. The above description constitutes the interface for your MyRectangle class. Thus, the Lab5 program below has been written with the assumption that your MyRectangle class has implemented this interface.

Lab5.java

Download this file and compile once you have completed the details of MyRectangle.java. Then run it to make sure it works as intended. Also read the comments carefully -- some additional important / useful information is provided there. Sample output from the program to compare with yours is online here:

lab5out.txt


Notes

You've learned that all public classes have to be contained in their own files with a consistent file name and class name. The compiler and runtime environment can locate a class that is part of a package using the import statement, which you added to your programs to utilize the Scanner and StringBuffer classes, or they can search a set of directories for the class, which includes the current working directory. When found, it will automatically be compiled if needed. For example, in this lab your main program is called Lab5.java. When it is being compiled, the compiler sees that the class MyRectangle is used, so it automatically looks to compile file MyRectangle.java in the same directory. If MyRectangle.java is not present, the compiler will report an error that the class is not found (test this by renaming MyRectangle.java to something else and then trying to compile Lab5.java).