Page 594 - Introduction to Programming with Java: A Problem Solving Approach
P. 594

                560 Chapter 14 Exception Handling
 /***************************************************************
*
LinePlot.java
Dean & Dean
This program plots a line as a series of user-specified
line segments.
***************************************************************/
*
*
*
*
import java.util.Scanner;
public class LinePlot
{
private int oldX = 0; // oldX and oldY save previous point
private int oldY = 0; // starting point is the origin (0,0)
//************************************************************
// This method prints description of a line segment from the
// previous point to the current point.
public void plotSegment(int x, int y)
{
System.out.println("New segment = (" + oldX + "," + oldY +
")-(" + x + "," + y + ")");
oldX = x;
oldY = y;
Apago PDF Enhancer
} // end plotSegment
//************************************************************
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
LinePlot line = new LinePlot();
String xStr, yStr;
int x, y;
// coordinates for point in String form
// coordinates for point
System.out.print("Enter x & y coordinates (q to quit): ");
xStr = stdIn.next();
while (!xStr.equalsIgnoreCase("q"))
{
    }
// end class LinePlot
}
// end main
}
yStr = stdIn.next();
x = Integer.parseInt(xStr); ⎫ ⎬
y = Integer.parseInt(yStr); ⎭
line.plotSegment(x, y);
System.out.print("Enter x & y coordinates (q to quit): "); xStr = stdIn.next();
// end while
These could generate runtime errors.
Figure 14.2 LinePlot program that plots a line—first draft













































   592   593   594   595   596