In: Computer Science
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
Information for finding Quadrants Information for finding distance ???????? ??????? ??? ?????? = √(?2 − ?1 ) 2 + (?2 − ?1 ) 2 You may also need the method in the Math class: Math.sqrt(num) and/or Math.pow(base, exponent)
Code: LineSegment.java
package demo;
public class LineSegment
{
private int x1;
private int y1;
private int x2;
private int y2;
// default constructor
public LineSegment()
{
}
// setter and getter to get and set the values
// sets x1 and y1 value
public void setPoint1(int x, int y)
{
this.x1 = x;
this.y1 = y;
}
// sets x2 and y2 value
public void setPoint2(int x, int y)
{
this.x2 = x;
this.y2 = y;
}
// return x1 value
public int getPoint1X()
{
return x1;
}
// return y1
public int getPoint1Y()
{
return y1;
}
// return x2 value
public int getPoint2X()
{
return x2;
}
// return y2 value
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 + ")");
}
}
Code: Main.java
package demo;
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
do {
// Declare an object of the Line Segment class
// Use the constructor with no formal parameters
LineSegment p1 = new LineSegment();
// call the method to read values
enterLineSegmentEndPoints(p1);
// call the calculateDistance to calculate distance
double distace =(float)calculateDistance(p1);
System.out.printf("The length of the line segment is : %.2f
",distace);
System.out.println();
// checks if the two points lies in the same Quadrant or
not
if(checkQuadrant(p1.getPoint1X(), p1.getPoint1Y()) ==
checkQuadrant(p1.getPoint2X(), p1.getPoint2Y())) {
System.out.println("The line segment is completely in
"+ checkQuadrant(p1.getPoint1X(), p1.getPoint1Y()));
System.out.println();
}
// else if points do not lie on same quadrant
else {
System.out.println("The line segment Crosses an
Axis");
System.out.println();
}
// ask user want to enter another points
System.out.println("Would you like to enter another line segment
(y/n):");
// read yes or no from user
input = kb.next();
}while(input.equals("Y") || input.equals("y"));
}
// method to read values
public static void enterLineSegmentEndPoints(LineSegment p1)
{
int x1,y1,x2,y2;
System.out.println("Enter the x and y coordinate of
the first end point: ");
// read values from user
x1 = kb.nextInt();
y1 = kb.nextInt();
// set the values
p1.setPoint1(x1, y1);
System.out.println("Enter the x and y coordinate of the second end
point: ");
// read values from user
x2 = kb.nextInt();
y2 = kb.nextInt();
// set values
p1.setPoint2(x2, y2);
System.out.println();
// call the print function
p1.print();
System.out.println();
}
// this method will calculate distance and return value
public static double calculateDistance(LineSegment p1) {
// get the values
float x1 = p1.getPoint1X();
float x2 = p1.getPoint2X();
float y1 = p1.getPoint1Y();
float y2 = p1.getPoint2Y();
// distance calculation
double distance =
Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
return distance;
}
// this method checks if the both points lies in same quadrant or
not
public static String checkQuadrant(float x , float y) {
// checks for Q1
if(x>0 && y>0) {
return "Quadrant 1";
}
// checks for Q2
else if(x<0 && y>0) {
return "Quadrant 2";
}
// checks for Q4
else if(x>0 && y<0) {
return "Quadrant 4";
}
// checks for Q3
else if(x<0 && y<0) {
return "Quadrant 3";
}
return null;
}
}