Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6:55 Safari E QE CSCI 470/502E - Assignment 2- Enhancing a Line.class 100 points This assignment practices writing Java classes, packages, adding variables and methods

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
6:55 Safari E QE 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 "outlastnamclinepackage" (examplesmithlinepackags", and cut and move the Line class code into the subdirectory package as Line.java, (you will also need to write a Two Point.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 Two Point.java Compile and test your new version of Line class that you create and hand at 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 1... 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 xl, y1, x2, y2; //coordinates of the line //Constructor //Receives 4 integers which are the Line's start and end points. public Line(int xe, int yone, int two. Ist you) 1/ each of these validates ita argument - see below. Betons. One) astxasiyOne) Beto) 5.XIYRKYTVO) 71 end constructor //method draw() calls another pethod called drawne). // which is assumed de on the 1/systen 6:55 . Safari 1 // each of these validates its argument - see below. setxongi One) sets. One) BetXZIXTO): SSXRYR) >77 end constructor // /ethod draw() calls another method called drawne). // which is assumed to be a graphics primitive on the system. However, since this program will be /run in console node, a text description of the Line in be displayed. 11 public void draw drawLine(x1, yi, x2, y2) //method drawine() simalates drawing of a line for console mode. //It should describe all the portant attributes of the Iine. //In a graphics mode program, we would delete this and one the //system's Graphics library dravine). private void drautine(int xl, int yi. int x2, int y2) 1 SYARIKA"Draw a line from * of Xandy of !): Sxste gledala to x of * * *2* and yo + y2 + //Method setline() allows user to change the points of the // already existing Line 1/ public void settine(int xone, int yone, int xtvo, int yu) Bet One): BIRBIYOne) Bet Two); Setely wo): > // -- the individual set.XXXX methods that prevent // any line's coordinate from being of Escreen. // In the event of an invalid (offscreen) value. W that value to (silently set to o. //*** publie void see int xone) if (xone 639) xi - 0 else x1 - xoner //**** publie void stoint yone) it cyone 479) yl - 0 else y1 - yOne: ********** publie void it TWO) 4 TO 600 6:55 al Safari ..SU yi - 0 else y1 - yone; ************ publle void beint TO) 14 (Two > 639 two //*** ... public void LIDIR yo) Ittyevo > 479 11 yawo 1/ Now for some get access methods to get individual values //****** publie inte) { return x;) *** public int getXARRI return yl; = public int 01 return x2;) public int RX return y2;) 11 end class Line *********** Now we will code a driver program (see below) called Testine with main) where execution will begin. It is this class, and this code, that will create instances of the time and call its methods. As a test module, this code would be improved with additional Systetubbio) statements that explain what is being attempted and what the results should be, for example: *About to change 11 to an invalid value and then redraw it. Line position should not change: class TestLine public static void main(String args[]) { Line 11. null, 12 null; //declare 2 instances of Line class 11 - new Line (10, 10, 100, 100) //draw it 11.draw(); //change start point with valid values 11.setLine(5, 5, HALD). XD)); //draw it again with new start point 11. draw) 7/try to change One (xl) to an 12 legal value WA SEX3000) 7/draw the line...X1 should now be sero 11.dra) //create a second Line Instance, or object 12 - new Line (100, 200, 400, 400) //draw and line 12.draw() ALXO3000) 6:55 Safari the line...I should now be zero 12.draw) //create a second Line Instance, or object 22 - new Line[100, 100, 400, 400) 1/draw and line 12.draw(): //drav 12.draw ) Il end of main >1/ end class TestLine Viset a new valid yevo for line 2 12.SEKER(479) 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 il type javas Linejava 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 sor(x1-x2)*(x1-x2)+(yl-y2)*1-x2). You can use the java.lang. Math class for calculating the square root (The method in Math is saxl)you need to check the API doc for its usage) b. Add a method called getAngle(). Calculate and tcturn (get) the angle of a Line, which is defined as the angle between that line and the horizontal line that starts from (xl, yl) and goes to right. Do this in a single return statement Return value is between-pi2 and pi 2. eg. If a line is from (10,10) to (20,20), the angle is p4 (45 degree: If a line is from (10.10) to (15. 5). then the angle is -p:4. Math class is again used. One way of calculating the angle is asially2-yl length). Add code to maint) of TestLineDriver java to test these new features, 2) Write another class and add another constructor in the Line class The class is called Tweat and should be in its own file called TwoDPaint.java, It is very simple: It has 2 public data members called 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 Zaxo Datut 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 Test and send them on to the other constructor. Keyword "This can be used to call one constructor from another. 6:55 Safari 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 weens 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. You need only testa 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 (ie the code in maini) of Testl.ine 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 valuc). 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 program, since nothing more will execute **Thus, when you create a new line orde a set on a line, this code needs to be inside of a try/catch. The catch will executed a bad value is trying to be passed in the driver code that creates the new line or does a badet 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) cach 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 cach exception object's message. You will lose some credit if this is done poorly, Submit ons version of ins Serm w implsments and its This program demonstrate wimple Lisa e, Line is defined with its properties and Interfece 11.1., lts methods) Anaia method in Test the create it of this Line elass and calls on the methods to date its behavior lise Laport java.io. public classe private int xl. y. 2.yalcoordinates of the //constructor Receives integer wich are the lines stars pable Lineant , int yone, in the styl each of these validates its - store setronelyset SEXTO setrvoly 3 // end constructor // method draw calls another sethod called dealint). I/which is ud to be a graphics peisitive the 1/system. However, since this program will be l/run is console mode, a text desespeto eta Line 11111 be displayed. public void drew detineti. y., method drawiet simulates drawing of line for console mode // It should describe all the important attributes of the line 1/1n graphic node provela delete this and was the 7/systeme Graphies athracy dv.inet) !! private void dravinenta, intys. las 22. se 72) Syetem.out.printlnova line free of and yoy) System.out.prints of /Method set it allows to change the points Halready existing Line publie voi ottanecint 100, Sne you see the int ytvo setxone (one Bet Yonel setxoto net to you: 11 -- the individual action authods that pro 11. aay Dine coordinate from being offccoce In the event of levels ocea) wat 1 that value is silently set to public void setOne(t ) it One One > 679) -: else > puble voldstonist you 439 10 2-01 else x-xo > public void netheint ytus) it tyto > 479 11 ytvo) y20 2 yo Now for sotet Access methods to get individual value publie inte return; i be in your fetara ya puble Intext { rea > public inte returnya; > end class Line How we will detine a driver program below called Test with maint where section will begin to this elars, and this code that will evente instances of the Lee and all Le nethods. As a teet modele, this code would be improved with additional Hystem.out.printing statements that explain what is bets attempted and what the culte should be for example an invalid value and then we is. Eine peittee who not changer / 7/ elas Bestane > public static void main(String arga111 Line 1 - ul. 32 - //declare 2 instances of Lire les Ilerate 1 Liguet 11 -ww Line (10.10, 100, 100); Ideas 11.dra Ich wart point with valid values 11.seline. 5. 11. otvo), 11. Get thot) //day it again with a point 11. dravi //try to change One (xto an illegal value 11.3000) /draw the line....devare 11. da Mercate & co Line Instance, or object 12 - LL 100, 100, 400, 4001 Hideaw 2 iset ate vella ytvo tor tine //draw and line again 12. drav land of } // and class Teatti 0 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 TestLine) 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, yi, x2, y2; //coordinates of the line //Constructor 1/Receives 4 integers which are the Line's start and end points. public Line(int xone, int yone, int xivo, int ytvo) { 11 each of these validates its argument - see below. set\One ( xOne); set Yone (yOne); set XTWO (XTWO); set YTWO (YTWO); } // end constructor //method draw() calls another method called drawLine() 1/which is assumed to be a graphics primitive on the 1/system. However, since this program will be //run in console mode, a text description of the Line 1/will be displayed. // public void draw() drawLine(x1, y1, x2, y2); > 1/method drawLine() simulates drawing of a line for console mode. I/It should describe all the important attributes of the line. 1/In a graphics mode program, we would delete this and use the Ilevetom'ecranhine 1 har drautinen ( 22 639) x1 = 0; else x1 = xOne ; > public void set Yone(int yone) if (yone 479) yl - 0; else yl - yOne; } public void setXTwo(int xTwo) if (xTWO > 639 || XTWO 479 || yTwo public int getYTWO() { return y2; } } // end class Line ************* Now we will define a driver program 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 11 to an invalid value and then redraw it. Line position should not change: "*/ //*********** class TestLine { public static void main(String args[]) { Line 11 = null, 12 = null; //declare 2 instances of Line class //create 1 Line object 11 = new Line (10, 10, 100, 100); //draw it 11.draw(); 1/change start point with valid values 11.setLine(5, 5, 11.getXTwo(), 11.getYTWO()); //draw it again with new start point 11.draw(); 1/try to change xone (xl) to an illegal value 11.setxone (3000); 1/draw the line...xl should now be zero 11.draw(); //create a second Line instance, or object 12 = new Line (100, 100, 400, 400); //draw 2nd line 12.draw. return x2; } public int getYTWO() { return y2; } } // end class Line ** Now we will define a driver program 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 11 to an invalid value and then redraw it. Line position should not change: **/ //*********** ******** class TestLine { public static void main(String args[]) { Line 11 - null, 12 = null; //declare 2 instances of Line class //create 1 Line object 11 - new Line (10, 10, 100, 100); //draw it 11.draw(); 1/change start point with valid values 11.setLine(5, 5, 11.getXTwo(), 11.getYTWO(); //draw it again with new start point 11.draw(); //try to change xone (xl) to an illegal value 11.set Xone (3000); //draw the line...xl should now be zero 11.draw(); 1/create a second Line instance, or object 12 - new Line(100, 100, 400, 400); //draw 2nd line 12.draw(); // set a new valid yTwo for line 2 12. set YTWO (479); //draw 2nd line again 12.draw(); } // end of main } // end class TestLine 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 xl, yl, x2, y2; //coordinates of the line I/Constructor //Receives 4 integers which are the Line's start and end points. public Line(int xone, int yone, int xTvo, int yTWO) { 1/ each of these validates its argument - see below. setxone ( xOne); setyOne (yOne); set XTWO (Two); set YTWO( YTWO) } // end constructor //method draw() calls another method called drawin hah is assumed + //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(xl, yi, x2, y2); 1/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 xl, int yl, int x2, int y2) { System.out.println("Draw a line from x of " + x1 + and y of " + yi); System.out.println("to x of " + x2 + and y of " + y2 + " "); } // //Method setLine() allows user to change the points of the 1/already existing Line. // public void setLine(int xone, int yone, int xTwo, int yTwo) { setXOne ( xone) setyOne (yOne); setXTwo (Two); set YTWO (Two); // -- the individual setxxxx methods that prevent // -- 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 set Xone(int xone) if (xOne 639) x1 = 0; else x1 - xOne; } //******* ***** public void setyOne (int yone) { if (yOne 479) yl = 0; else yl = yOne; ****** public void setXTwo(int xTwo) { if (xTwo > 639 || xTwo 479 || yTwo

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions