CSCI 470/680E - Assignment 2 - Fall 2009
Study the Line class and test program given below and compile and run it. Then see the "Enhancements" below (after the code) and add the features described there to this program. Compile and test the new version and hand it in as Assignment 2.
/***************************************************************** This program demonstrates a simple "Line" class.
Here, a Line class is defined with its properties and interface (i.e., its methods).
A main class then creates instances of this Line class and calls on the methods to demonstrate its behavior. *****************************************************************/
import java.io.*;
//****************************************************************
class Line
{
private int x1, y1, x2, y2; //coordinates of the line
//constructor -- always same name as class and no return value
//Receives 4 integers which are the Line's start and end points.
//
public Line(int left, int top, int right, int bottom)
{
// each of these validates its argument - see below.
setLeft(left);
setTop(top);
setRight(right);
setBottom(bottom);
} // end constructor
//*************************************
//method draw() calls another method called drawLine(),
//which is assumed to be a graphics primitive on the
//system. However, since this program will be
//run in console mode, a text description of the Line
//will be displayed.
//
public void draw()
{
drawLine(x1, y1, x2, y2);
}
//*************************************
//method drawLine() simulates drawing of a line for console mode.
//It should describe all the important attributes of the line.
//In a graphics mode program, we would delete this and use the
//system's Graphics library drawLine().
//
private void drawLine(int x1, int y1, int x2, int y2)
{
System.out.println("Draw a line from x of " + x1 + " and y of " + y1);
System.out.println("to x of " + x2 + " and y of " + y2 + "\n");
}
//*************************************
//Method setLine() allows user to change the points of the
//already existing Line.
//
public void setLine(int left, int top, int right, int bottom)
{
setLeft(left);
setTop(top);
setRight(right);
setBottom(bottom);
}
// -- the individual setXXXX methods that prevent // any line's coordinate from being offscreen. // In the event of an invalid (offscreen) value, // that value is (silently) set to 0.
//**************************
public void setLeft(int left)
{
if (left < 0 || left > 639)
x1 = 0;
else
x1 = left;
}
//**************************
public void setTop(int top)
{
if (top < 0 || top > 479)
y1 = 0;
else
y1 = top;
}
//**************************
public void setRight(int right)
{
if (right > 639 || right < 0)
x2 = 0;
else
x2 = right;
}
//**************************
public void setBottom(int bottom)
{
if (bottom > 479 || bottom < 0)
y2 = 0;
else
y2 = bottom;
}
//Now for some "get" Access methods to get individual values
//**************************
public int getLeft()
{
return x1;
}
//**************************
public int getTop()
{
return y1;
}
//**************************
public int getRight()
{
return x2;
}
//**************************
public int getBottom()
{
return y2;
}
} // end class Line
/*************************************************************** Now we will define a class with main() where execution will begin. It is this class, and this code, that will create instances of the Line and call its methods. */
//****************************************************************
public class TestLine
{
public static void main(String args[])
{
Line l1, l2; //declare 2 instances of Line class
//create 1 Line object
l1 = new Line (10, 10, 100, 100);
//draw it
l1.draw();
//change start point with valid values
l1.setLine(5, 5, l1.getRight(), l1.getBottom());
//draw it again with new start point
l1.draw();
//try to change left (x1) to an illegal value
l1.setLeft(3000);
//draw the line...x1 should now be zero
l1.draw();
//create a second Line instance, or object
l2 = new Line(100, 100, 400, 400);
//draw 2nd line
l2.draw();
//set a new valid bottom for line 2
l2.setBottom(479);
//draw 2nd line again
l2.draw();
} // end of main
} // end class TestLine
Line Class Enhancement Assignment
Modify this program to accomplish the following:
1.) Add additional methods and variables to the Line class to implement the following behaviors:
You may implement the color and width features in any reasonable way. For example, color could be an int value or a String (or other reasonable implementation).
Add code to main() to test these new features.
2.) Instead of setting a coordinate to zero when an invalid value is encountered (in the constructor or any of the setter methods), have the appropriate code in the Line class detect invalid values and throw a BadArgException that will be caught by the calling code. This Exception type is a new class that you will write which extends the Exception class (which is a checked exception). You should provide two methods for this class:
Each of these methods requires just one line of code.
(Note: there is another way to achieve the needed functionality; involving the use of the super keyword to invoke the appropriate constructor or method of the super class (i.e. the Exception class). Your instructor will tell you if you should use this technique or if it is optional.
Modify the calling code in the test program to catch this exception and handle it. So in the test program, arrange to throw and catch the BadArgException:
3.) Write a second constructor for the Line class that will accept 2 TwoDPoint objects instead of 4 ints. Write your own TwoDPoint class. A TwoDPoint has 2 public data members called x and y and a constructor that accepts two integer arguments and stores them in x and y. That's all there is to the class. It has no other methods.
This second Line constructor should simply call the first constructor (the one that accepts 4 ints and may throw an Exception). So extract the x and y ints from each TwoDPoint and send them on to the other constructor. There is a special syntax for calling one constructor from another. Find this in a book.
Add code to main() to test this new constructor.
After (and/or before) each operation in main display informational messages so the user knows what is being attempted and what the result was. Reading the output from the program should make it clear to the reader what the sequence of operations was and what happened. Make use of information (which you should supply to every thrown exception) in each exception object's message. You will lose some credit if this is done poorly.
Hand in one version of the modified program which implements and tests all the features mentioned above. You will submit the source code in class. Remember to apply applicable coding, formatting, and documentation standards!