In Java please
10.9 Lab 6
In BlueJ, create a project called Lab6
Code for class LineSegment
public class LineSegment { private int x1; private int y1; private int x2; private int y2; public LineSegment() { } public LineSegment(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public void setPoint1(int x, int y) { this.x1 = x; this.y1 = y; } public void setPoint2(int x, int y) { this.x2 = x; this.y2 = y; } public int getPoint1X() { return x1; } public int getPoint1Y() { return y1; } public int getPoint2X() { return x2; } public int getPoint2Y() { return y2; } //Prints the point in the format (x1, y1) to (x2, y2) public void print() { System.out.print("(" + x1 + ", " + y1 + ") to (" + x2 + ", " + y2 + ")"); } }
Once you have the class working
Code for class Main
import java.util.Scanner; public class Main { public static Scanner kb = new Scanner(System.in); public static void main(String [] args) { String input; // this variable used to allow the // use to type Y or N when asked about // changing point // As you develop your program, add any variables // you may need here // Declare an object of the Line Segment class // Use the constructor with no formal parameters // You are to finish the rest of main to create the output given. // This program will also have a method called enterLineSegmentEndPoints // that allows the user to enter the end points of a line segment. The header // for the method is given below main. // Declare any variables you may need in the section above // Some suggestions might be: // First get the loop going that will continue asking if there is // another line segment until the user types in anything other than Y // Next get the method to enter the endpoints working - see output for formatting // Then compute the length of the line segment using the method you wrote in the class // Next for the quadrant, you might want to store the string returned from the method // class and then check the first character to determine what to print // Finally put in the code that will print the total number of line segments // entered after the user indicates they are done. // NOTE: Get little pieces to work at a time - do NOT type it all in at once } // Add the code to this method wrapper so that it allows the user to enter the // end points of the line segments as shown on the output public static void enterLineSegmentEndPoints(LineSegment line) { } }
A sample output is given
Enter the x and y coordinate of the first end point: 2 9 Enter the x and y coordinate of the second end point: 14 6 Line Segment: (2, 9) to (14, 6) The length of the line segment is: 12.37 The line segment is completely in Quadrant 1 Would you like to enter another line segment (y/n): Y Enter the x and y coordinate of the first end point: -4 8 Enter the x and y coordinate of the second end point: -7 -3 Line Segment: (-4, 8) to (-7, -3) The length of the line segment is: 11.40 The line segment Crosses an Axis Would you like to enter another line segment (y/n): y Enter the x and y coordinate of the first end point: 3 -7 Enter the x and y coordinate of the second end point: 21 -14 Line Segment: (3, -7) to (21, -14) The length of the line segment is: 19.31 The line segment is completely in Quadrant 4 Would you like to enter another line segment (y/n): n You entered a total of 3 line segments
Main.java ------------------------------------------------------- import java.util.Scanner; public class Main { public static Scanner kb = new Scanner(System.in); public static void main(String [] args) { // count variable will keep track of how many times user entered ending points int count =1; // input takes either yes or no String input; while(true) { LineSegment a = new LineSegment(); enterLineSegmentEndPoints(a); a.print(); a.lengthOfLine(); a.findQuadrant(); System.out.print("Would you like to enter another line segment (y/n): "); input = kb.nextLine().toLowerCase(); if(!input.equals("y")) { System.out.println("You entered a total of "+ count+" line segments"); break; } count++; } // END of While LOOP } // END of MAIN // Add the code to this method wrapper so that it allows the user to enter the // end points of the line segments as shown on the output public static void enterLineSegmentEndPoints(LineSegment line) { System.out.print("Enter the x and y coordinate of the first end point: "); String str = kb.nextLine(); // splits the input ( with spaces in between ) String[] input = str.split(" "); line.setPoint1(Integer.parseInt(input[0]) , Integer.parseInt(input[1])); System.out.print("Enter the x and y coordinate of the second end point: "); str = kb.nextLine(); input = str.split(" "); line.setPoint2(Integer.parseInt(input[0]) , Integer.parseInt(input[1])); } // END of enterLineSegmentEndPoints method. }// END of MAIN class
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LineSegment.java ------------------------------------------ import java.lang.Math; public class LineSegment { private int x1; private int y1; private int x2; private int y2; // constructor public LineSegment() { } // parameterized constructor public LineSegment(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } //setters public void setPoint1(int x, int y) { this.x1 = x; this.y1 = y; } public void setPoint2(int x, int y) { this.x2 = x; this.y2 = y; } // getters public int getPoint1X() { return x1; } public int getPoint1Y() { return y1; } public int getPoint2X() { return x2; } public int getPoint2Y() { return y2; } //Prints the point in the format (x1, y1) to (x2, y2) public void print() { System.out.print("Line Segment: "+"(" + x1 + ", " + y1 + ") to (" + x2 + ", " + y2 + ")\n"); } public void lengthOfLine() { int x1 = this.getPoint1X(); int y1 = this.getPoint1Y(); int x2 = this.getPoint2X(); int y2 = this.getPoint2Y(); // sqrt[ (x2 - x1)^2 + (y2-y1)^2 ] float length = (float)Math.sqrt(Math.abs(x2-x1)*Math.abs(x2-x1) + Math.abs(y2-y1)*Math.abs(y2-y1)); System.out.printf("The length of the line segment is: %.2f\n",(length)); } // This method helps us to find in which quadrant the line segment lies. // if it lies in more than one quadrant ,it prints crosses an Axis public void findQuadrant() { int x1 = this.getPoint1X(); int y1 = this.getPoint1Y(); int quad1 = quadHelper(x1,y1); int x2 = this.getPoint2X(); int y2 = this.getPoint2Y(); int quad2 = quadHelper(x2,y2); if(quad1 == quad2) { System.out.println("The line segment is completely in Quadrant "+quad1); } else { System.out.println("The line segment Crosses an Axis"); } } // this takes a point (x,y) values // and decides in which quadrant the point lies public int quadHelper(int x, int y) { // if x is +ve and y is +ve , point is in quadrant 1. if( x >0 && y >0 ) { return 1; } else if( x>0 && y<0 ) { return 4; } else if(x <0 && y>0) { return 2; } else { return 3; } } } // END OF LineSegment class
============================================================
OUTPUT:
THANK YOU!! If you have any doubt , please ask in comment section .. i will definitely clarify.
Get Answers For Free
Most questions answered within 1 hours.