Object Model
(Top)
The following figure shows the class model, a "real world" example,
and an instance model that corresponds to the example.
FIGURE 1: LINE SEGMENT EXAMPLE
Point Class
(Top)
Examine the source code for the Point class. Except for the toString() operation, mapping the model into Java is just a matter of syntax. The toString() operation uses a temporary instance of the StringBuffer class to construct the string. This is more efficient than using the string concatenation operator (plus). But note that the method could have been written as:
public String toString()
{
return("(" + xPos + "," + yPos + ")");
}
since the Java compiler will direct primitive types to be
converted to strings for string concatenation.
//file Point.java
public class Point
{
//Instance variables
private double xPos = 0.0;
private double yPos = 0.0;
//Constructors
public Point(double xLoc, double yLoc)
{
xPos = xLoc;
yPos = yLoc;
}
//Instance methods
public final double getX()
{
return(xPos);
}
public final double getY()
{
return(yPos);
}
//Utility methods
public String toString() // Overrides Object.toString()
{
//Create string buffer
StringBuffer buff = new StringBuffer(20);
//Encode point into buffer
buff.append('(');
buff.append(xPos);
buff.append(',');
buff.append(yPos);
buff.append(')');
//Return string
return(buff.toString());
}
} //class
|
LineSeg Class
(Top)
Examine the source code for the LineSeg class. Note that the constructor in this class precomputes the x and y axis length for the line. Here the programmer probably made this choice because they are both needed for the getLength and getSlope operations. Since the methods are hidden from the user, this decision could be altered later without changing the public interface.
//file LineSeg.java
public class LineSeg
{
//Instance variables
private Point point1;
private Point point2;
private double xLen;
private double yLen;
//Constructors
public LineSeg(Point p1, Point p2)
{
point1 = p1;
point2 = p2;
xLen = p1.getX() - p2.getX();
yLen = p1.getY() - p2.getY();
}
//Instance methods
public final double getLength()
{
return(Math.sqrt(xLen * xLen + yLen * yLen));
}
public final double getSlope()
{
return(yLen / xLen);
}
//Utility methods
public String toString() // Overrides Object.toString()
{
//Create string buffer
StringBuffer buff = new StringBuffer(20);
//Encode line into buffer
buff.append("[p1");
buff.append(point1.toString());
buff.append(" p2");
buff.append(point2.toString());
buff.append(']');
//Return string
return(buff.toString());
}
} //class
|
Test Program
(Top)
A good programmer always writes a "test harness" to test the accuracy of the coding. If the designers do not supply suggested test ideas, the programmer must invent their own. Here is the source code for the TestLine class.
//file TestLine.java
import java.io.*;
public class TestLine
{
private static void execute()
{
//Construct two points
Point point1 = new Point(0,0);
Point point2 = new Point(3,4);
//Display the points
System.out.print(" Point1 " + point1.toString());
System.out.print(" Point2 " + point2.toString());
System.out.println();
//Construct a line using the points
LineSeg line = new LineSeg(point1,point2);
//Display the line and its attributes
System.out.print(" Line " + line.toString());
System.out.print(" has length ");
System.out.print(line.getLength());
System.out.print(" and slope ");
System.out.print(line.getSlope());
System.out.println();
}
public static void main(String[] args) throws IOException
{
//Execute test
System.out.println("INIT PROGRAM");
execute();
System.out.println("TERM PROGRAM");
//Wait for response
System.out.print("Type any char to exit ...");
System.out.flush();
int c = System.in.read();
}
} //class
|
Output
(Top)
INIT PROGRAM Point1 (0.0,0.0) Point2 (3.0,4.0) Line [p1(0.0,0.0) p2(3.0,4.0)] has length 5.0 and slope 1.33333333 TERM PROGRAM Type any char to exit ... |
Prepared by David L. March -- Last Revised on September 7, 1998
COPYRIGHT © 1997 BY DAVID L. MARCH