Question
CSCI 470/502E - Assignment 2- Enhancing a Line.class 100 points This assignment practices writing Java classes, packages, adding variables and methods to the class, as
CSCI 470/502E - Assignment 2- Enhancing a Line.class 100 points This assignment practices writing Java classes, packages, adding variables and methods to the class, as well as exception throwing and handling. Below is code for a Line class and a driver program (TestLine class). I will give you a text file that you can upload onto turing with these two classes. Make a directory/package off of your linux home directory called yourlastnamelinepackage (example smithlinepackage, and cut and move the Line class code into the subdirectory/package as Line.java, (you will also need to write a TwoDPoint.java class in this directory as well (see below). Leave the driver code (TestLine) in your home directory. Then make changes to the Line class as described below. Summary: Your home directory has TestLine class in a TestLine.java file, your subdirectory/package has has Line.java and TwoDPoint.java. Compile and test your new version of Line class that you create and hand it in as Assignment 2. Update the documentation to reflect the changes you have made. A nice way to do this is to add a section after the existing documentation headed "Code Changes and Enhancements /***************************************************************** This program demonstrates a simple "Line" class. Here, a Line class is defined with its properties and interface (i.e., its methods). A main method (in TestLineDriver.java) then creates instances of this Line class and calls on the methods to demonstrate its behavior. *****************************************************************/
import java.io.*; public class Line { private int x1, y1, x2, y2; //coordinates of the line //Constructor //Receives 4 integers which are the Line's start and end points. public Line(int xOne, int yOne, int xTwo, int yTwo) { // each of these validates its argument - see below. setXOne(xOne); setYOne(yOne); setXTwo(xTwo); setYTwo(yTwo); } // 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 + " "); } //************************************* //Method setLine() allows user to change the points of the //already existing Line. // public void setLine(int xOne, int yOne, int xTwo, int yTwo) { setXOne(xOne); setYOne(yOne); setXTwo(xTwo); setYTwo(yTwo); } // -- 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 setXOne(int xOne) { if (xOne < 0 || xOne > 639) x1 = 0; else x1 = xOne; } //************************** public void setYOne(int yOne) { if (yOne < 0 || yOne > 479) y1 = 0; else y1 = yOne; } //************************** public void setXTwo(int xTwo) { if (xTwo > 639 || xTwo < 0) x2 = 0; else x2 = xTwo; } //************************** public void setYTwo(int yTwo) { if (yTwo > 479 || yTwo < 0) y2 = 0; else y2 = yTwo; } //Now for some "get" Access methods to get individual values //************************** public int getXOne() { return x1; } //************************** public int getYOne() { return y1; } //************************** public int getXTwo() { return x2; } //************************** public int getYTwo() { return y2; } }
// end class Line /***************************************************************
Now we will code a driver program (see below) called TestLine with main() where execution will begin. It is this class, and this code, that will create instances of the Line and call its methods. As a test module, this code would be improved with additional System.out.println() statements that explain what is being attempted and what the results should be, for example: "About to change l1 to an invalid value and then redraw it. Line position should not change: " */ //*********************************************************
class TestLine { public static void main(String args[]) { Line l1 = null, l2 = null; //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.getXTwo(), l1.getYTwo()); //draw it again with new start point l1.draw(); //try to change xOne (x1) to an illegal value l1.setXOne(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 yTwo for line 2 l2.setYTwo(479); //draw 2nd line again l2.draw(); } // end of main } // end class TestLine
First copy into linux the source code that your instructor will send you. The source code will be a copy of the above and put it in a Line.java file in a subdirectory as specified above. To compile it, type javac Line.java. Modify the above Java program to accomplish the following: 1) Add additional methods and variables to the Line class to implement the following behaviors: a. Add a method called getLength(). Calculate and return (get) the length of a Line based on its coordinates. Do not add an instance variable to hold the length. Instead, you are asked to do all the math calculations in a single return statement. The method returns a double. The formula is sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)). You can use the java.lang.Math class for calculating the square root (The method in Math is sqrt(), you need to check the API doc for its usage). b. Add a method called getAngle(). Calculate and return (get) the angle of a Line, which is defined as the angle between that line and the horizontal line that starts from (x1, y1) and goes to right. Do this in a single return statement. Return value is between -pi/2 and pi/2. e.g.: If a line is from (10, 10) to (20, 20), the angle is pi/4 (45 degree); If a line is from (10,10) to (15, 5), then the angle is -pi/4. Math class is again used. One way of calculating the angle is asin((y2-y1)/length). Add code to main() of TestLineDriver.java to test these new features. 2) Write another class and add another constructor in the Line class. The class is called TwoDPoint and should be in its own file called TwoDPoint.java. It is very simple: It 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. Add another constructor for Line that will accept 2 TwoDPoint objects instead of 4 ints. This second Line constructor should simply call the first constructor (the one that accepts 4 ints). So extract the x and y ints from each TwoDPoint and send them on to the other constructor. Keyword this can be used to call one constructor from another. Add code to main() to test this new constructor. So create a new L3 Line bojcet object for this third line with the new constructor. You need only test a successful case here. 3) Add exception creation/throwing, passing and handling. In your set methods in the Line class, instead of setting a coordinate to zero when an invalid value is encountered, now have the appropriate code detect invalid values and throw an Exception that will be caught by the calling code (i.e. the code in main() of TestLine that calls the method). To do so, the setters in Line class will detect the problem and throw the exception (new a generic Exception object with message passed in to its constructor). This Exception you generate is a checked exception. The code in main() of TestLine class will catch and handle the exception: when trying to alter an existing Line's position with an invalid value (example:the driver code calls a set on a current Line object with an invalid value). In this case, simply display in your catch the error message and state that the Line was not moved (but you can still use the Line object later, since it already exists). when creating a new Line (example:the driver program creates a new Line object with an invalid constructor value). In this case, if the constructor fails, the calling (catching) code should just display the message about the failure and terminate the program with return code of 88 (System.exit(88);). **Test this feature last in the driver program, since nothing more will execute. So create a new Line L4 object with bad values for one value being a bad constructor value, and your catch will terminate your pgm after receiving back the exception thrown by the line class. **Thus, when you create any new line, or do a set on a line, this code needs to be inside of a try/catch. The catch will execute if a bad value is trying to be passed in by the driver code that creates the new line or does a bad set. So your driver code will have a series of try/catches where you create a line or alter a line with a set. Most of the catch blocks will not be executed, but some will when an exception is thrown by the Line.class. The other code in the driver program will not need a try/catch. The two catch blocks that will be executed is when you do a catch for a bad set, and then at the very end of your driver code when you construct a line with bad values in the constructor. You do not need to code your gets in a try/catch in the driver. The output of the program should have informational messages after (and/or before) each operation 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. Submit one version of the modified program which implements and tests all the features mentioned above via Blackboard. You only need to submit the java source files (e.g. Line.java; TestLine.java, TwoDPoint.java) along with a screen shot of your output. Remember to apply applicable coding, formatting, and documentation standards! SAMPLE OUTPUT In drawline -draw a line from x of 10 and y of 10 to x of 100 and y of 100 SUCCESS In drawline -draw a line from x of 5 and y of 5 to x of 100 and y of 100 SUCCESS --EXCEPTION: MY TRY CATCH CAUGHT A GENERIC EXCEPTION IN A SET METHOD FOR BAD VALUE OF X1 FOR AN EXISTING LINE java.lang.Exception: Value 3000 Was out of Bounds In drawline -draw a line from x of 5 and y of 5 to x of 100 and y of 100 SUCCESS In drawline -draw a line from x of 100 and y of 100 to x of 400 and y of 400 SUCCESS In drawline -draw a line from x of 100 and y of 100 to x of 400 and y of 479 SUCCESS The angle for line 1 is 0.7853981633974482 The angle for line 2 is 0.9012249669521654 The length for line 1 is 134.35028842544403 The length for line 2 is 483.3642518846424 test 2D point Constructor X1 = 10 (this is output after you create Line L3 with 2d constructor and do gets on the 4 values) test 2D point Constructor X2 = 5 test 2D point Constructor Y1 = 100 test 2D point Constructor Y1 = 400 EXCEPTION:This try catch caught a Generic Exception for a bad constructor -Failed to create a line with 4 invalid values-leaving with rc of 88 (this is the output after you create Line L4 with bad constructor values) Java Result: 88 BUILD SUCCESSFUL (total time: 0 seconds)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started