Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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
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, yl, 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) 11 each of these validates its argument - see below. setxOne (One); setyOne (yOne); setXTwo (Two): setyTwo (yTwo); } // end constructor 1/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 //will be displayed. // public void draw() drawt.ine(x1, yi, x2, y2); } 1/method drawLine() simulates drawing of a line for console mode. // It should describe all the important attributes of the line. I/In a graphics mode program, we would delete this and use the 1/system's Graphics library drawLine(). // private void drawLine(int x1, int yi, int x2, int y2) 1 System.out.println("Draw a line from * of " + xl + and y of " + yl); 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. publie void setLine(int xone, int yone, int xivo, int ytvo) setxOne One) set YoneyOne): BetXTWO (TO) setyTwo (To): > 1/ -- the individual set Xxxx methods that prevent 1/ any line's coordinate from being offscreen. In the event of an invalid (oftscreen) value, // -- the individual setXXXX methods that prevent // any line's coordinate from being offscreen. // In the event of an invalid (offscreen) value, W that value is (silently) set to 0. public void set Xone(int xone) if (xOne 639) x1 - 0; else x1 - xone; > public void set Yone(int yOne) if (yone 479) y1 = 0; else yl - yone; public void setXTwo(int xTwo) if (xtwo > 639 || xTwo 479 || yTwo public int get Yone() return yl: public int getXTO() return x2: public int getto) return y2; ) ) // end class Line /********* How we will define a driver program below called Testline with me when neede return y; public int getXTWO() 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; 1/declare 2 instances of Line class 1/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.getYTVO()); //draw it again with new start point 11.draw(); //try to change xOne (xl) to an illegal value 11.setOne(3000); //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: 1/set a new valid ytwo for line 2 12.setTwo (479) 1/draw 2nd line again 12.draw(): > // end of main } // end class TentLine 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.3, public class Line private int xl, yi, x2, y2; //coordinates of the line United States) Facur 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, yi, x2, y2; //coordinates of the line 1/Constructor //Receives 4 integers which are the Line's start and end points. public Line(int xone, int yone, int xTwe, int xTwo) see below. // each of these validates its argument SetXang (xdne); setxone (yane); setXTwe(Two). setXTwe(I). } // end constructor 1/method drawl) calls another method called drawine/), 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..) { drawine xl, yl, x2, y2); 1/method drawing) 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 sh (United States) FOCUS drawLine(x1, yi, x2, y2); //*** 1/method drawLinel) 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 drawLinel). private void drawine(int xi, int yi, int x2, int y2) { System.out.println("Draw a line from x of " + + " and y of yl). System.out.println("to x of " + x2 + and y of " + y2 + " "); } 1/Method setLine.) allows user to change the points of the //already existing Line. // public void setLine(int xong, int xane, int xTwo. int Two) sexOne (One) BeYone(yne): sex (Two). glish (United States) Focus set Xone (xQne... set Xone (yane.. setXTwe(Two). set XIWA (Two): } // the individual setxxxx methods that prevent IL any line's coordinate from being offscreen. 1 In the event of an invalid (offscreen) value, IL that value is (silently) set to 0. //******* public void setxone(int xOne) { if (xOne 639) xl = 0; else x1 = xQner public void setxoneint Yone.) if (yone. 479) yl - Oi. else English (United States) yl = yOne. //**** ***** public void setxone (int YOne) { if (yone 479) yl = 0; else yl = yOne *** public void setXTwoint xTwo) { if (xTwo > 639 || XTug 479 || TwQ // end constructor /method draw() calls another method called drawLine(). 1/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. 1/It should describe all the important attributes of the line. //In a graphics mode program, we would delete this and use the 1/system's Graphics library drawLine(). // 77In a graphics mode program, we would delete this and use the 1/system's Graphics library drawLine(). // private void drawLine(int xl, int yi, int x2, int y2) { System.out.println("Draw a line from * of + xl + and y of + yl); System.out.println("to x of " + x2 + and y of + y2 + " "); 1/Method setLine() allows user to change the points of the 1/already existing Line. public void setLine(int xone, int yone, int xivo, int yTwo) { lethorne xne); setyOne (yOne); setXTWO (XTWO) set YTWO (Two); } 1/ -- the individual setxxxx methods that prevent 11 any line's coordinate from being offscreen. 1/ In the event of an invalid (offscreen) value, // that value is (silently) set to 0. public void setxone(int xone) { if (xOne 639) x1 = 0; else x1 - xOne; } public void setyone(int yone) { if (yone co | yone > 479) yl - 0; else yl - yOne; } public void setXTwo(int xTwo) { if (XTWO > 639 || XTWO 479 11 yTwo public int get XTWO() { return x2; 2 public int get YTWO() { 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; 1/declare 2 instances of Line class //create 1 Line object 11 - new Line (10, 10, 100, 100); //draw it 11.draw(); //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 (x1) to an illegal value 11.set Xone (3000); //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 public int getXTWO() { 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 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; 1/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.getY()); 1/draw it again with new start point 11.draw(); //try to change xOne (x1) to an illegal value 11.setOne(3000); 1/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); 1/draw 2nd line 12.draw(); // set a new valid yTwo for line 2 12.setyTwo (479); //draw 2nd line again 12.draw(); } // end of main 1/ end class TestLine

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Development Of Knowledge Framework For Affective Content Analysis

Authors: Swarnangini Sinha

1st Edition

B0CQJ13WZ1, 979-8223977490

More Books

Students also viewed these Databases questions

Question

=+2. How reliable is this existing information?

Answered: 1 week ago

Question

How does EVA differ from residual income?

Answered: 1 week ago