 |
|

|
 |

10/27-29/09 - Classes to model real objects in Java
|
 |
 |
 |
  | Java Class to model a box:
|
 |
 |
 |
 |
  | When you create a class to represent a particular object, the class contains instance variables related to the object and methods which manipulate those instance variables. We say the class "encapsulates" the data and the methods because the data and the methods, together, give us all the information we need to use the object, and all that information and capability is contained in the class.
In the code below, we use a class to represent a simple object, a box. A box has a length, width and height, and those become our instance variables. We could add additional characteristics of a box, such as material (wood, cardboard, metal, plastic, etc.) and weight and color and ..., but to keep it simple for now, we'll just use length, width and height. The reason we call length, width and height "instance" variables is because every box we make will have its own particular values for length, width and height. So each instance of a box, has a length, width and height. We almost always declare the instance variables to be "private." That means they are accessible from anywhere inside the class, but they are not accessible from outside the class, so we don't need to worry about someone changing their values without our knowledge.
The first method we need to write is the constructor. The purpose of the constructor is to initialize or give starting values to the instance variables. In particular, we want to make sure that the instance variables don't have "bad" values. For our box example below, a "bad" value would be any negative value. So, the constructor has to make sure the instance variables length, width and height are not negative. We can have more than one constructor. They just need to have different parameter lists. For the box example, we have one constructor which takes no parameters at all. This is generally called the "default" constructor. In this case, the default constructor sets the length, width and height to zero. We have another constructor which takes length, width and height parameters so that we can make a box of any dimensions we want. This constructor checks to see if the parameters are positive and, if so, assigns them to the instance variables. If not, it assigns zero to the instance variables. If the parameters and the instance variables have the same name, then we need to prefix the instance variables with the word "this" to refer to the current object. Hence, we could write this.length = length. this.length refers to the instance variable for our object, and length refers to the parameter.
It's fairly common to have accessor (get) and mutator (set) methods for each of the instance variables. So for the Box class, we have setLength and getLength, setWidth and getWidth, and setHeight and getHeight methods.
For a Box, it also makes sense to have methods to return the volume and surface area. So we have getVolume and getArea methods.
Finally, every class should implement the toString method. The idea of the toString method is to return a string representation of the object. So the toString method for the Box class returns a string labeling and identifying the value of each instance variable.
|
 |
 |
 |
 |
  | Java code for the Box class:
|
 |
 |
 |
 |
  | /** * A class to represent a box with length, width and height. * * @author Chuck Iverson * @version 10/27/09 */ public class Box { private double length; private double width; private double height;
public Box() { length = width = height = 0; }
public Box(double length, double width, double height) { this.length = (length > 0) ? length : 0; this.width = (width > 0) ? width : 0; this.height = (height > 0) ? height : 0; }
public double getVolume() { return length*width*height; } public double getArea() { return 2*(length*width + length*height + width*height); } public void setLength(double newLength) { length = (newLength > 0) ? newLength : 0; } public double getLength() { return length; }
public void setWidth(double newWidth) { width = (newWidth > 0) ? newWidth : 0; } public double getWidth() { return width; }
public void setHeight(double newHeight) { height = (newHeight > 0) ? newHeight : 0; } public double getHeight() { return height; } public String toString() { String s = "Length: " + length + "\n"; s += "Height: " + height + "\n"; s += "Width: " + width; return s; } }
|
 |
 |
 |
 |
  | Java code to test the Box class:
|
 |
 |
 |
 |
  | /** * A class to test the Box class. * * @author Chuck Iverson * @version 10/27/09 */ public class BoxTest { public static void main(String [] args) { Box box1 = new Box(); System.out.println("Box #1:\n" + box1.toString()); System.out.println("Volume: " + box1.getVolume()); System.out.println("Area: " + box1.getArea()); // change length, height and width of box1 box1.setLength(5); box1.setHeight(8); box1.setWidth(20); System.out.println("\nBox #1 Revised:\n" + box1.toString()); System.out.println("Volume: " + box1.getVolume()); System.out.println("Area: " + box1.getArea()); Box box2 = new Box(2,3,4); System.out.println("\nBox #2:\n" + box2.toString()); System.out.println("Volume: " + box2.getVolume()); System.out.println("Area: " + box2.getArea()); } }
/* sample run
Box #1: Length: 0.0 Height: 0.0 Width: 0.0 Volume: 0.0 Area: 0.0
Box #1 Revised: Length: 5.0 Height: 8.0 Width: 20.0 Volume: 800.0 Area: 600.0
Box #2: Length: 2.0 Height: 4.0 Width: 3.0 Volume: 24.0 Area: 52.0
*/
|
 |
 |
 |
 |
  | Class exercise: Create a class to model a book and create a separate class with a main to test it.
|
 |
 |
 |
 |
  | Create a class to model an AM/FM radio. Your Radio class should have a default constructor and a constructor that takes parameters. Your Radio class should also have a toString method to label and show the current values of all instance variables. Your Radio class should have get and set methods for each instance variable. Also create a TestRadio class to create instances of Radio objects and call appropriate Radio methods to change and display each radio object's status.
|
 |
 |
|


 |
 |
 |