Zero: See Exercises below (after the code)
/*****************************************************************
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.
*/
//****************************************************************
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

 


Assignment Zero Exercises

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 these features in any reasonable way.  For example, color could be an int value or a String.  You should detect the special case of a vertical (slope) Line and handle it by throwing an IllegalArgumentException.  The caller should catch this exception and handle it in a reasonable way.

Add code to main() to test these new features.

2.)  Instead of setting a coordinate to zero when an invalid value is encountered, have appropriate code in the Line class detect invalid values and throw an IllegalArgumentException that will be caught by the calling code. Modify the calling code to catch this exception and handle it.  Do this

Add code to main() to test this.

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 Point 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. 

Note: IllegalArgumentException is an unchecked exception; that is, the compiler does not require you to catch it.  For this assignment, pretend it is a checked exception; that is, anytime it could occur, be prepared to catch this exception.

 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!