Question

In: Computer Science

In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as...

In BlueJ, create a project called Lab6

  • Create a class called LineSegment –Note: test changes as you go
  • Copy the code for LineSegment given below into the class.
  • Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y)
  • You will have to write two methods in the LineSegment class. There is information on black board under this week to help with the following methods.
    • determineQuadrant - this method determines if the line segment is completely in a quadrant and returns a String (“Quadrant 1”, “Quadrant 2”, “Quadrant 3”, or “Quadrant 4”) representing the quadrant. If it is not completely in a quadrant it returns the string “Crosses an Axis”. There are no formal parameters.
    • computeLength– returns the length of the line segment created by the points. There are no formal parameters
  • • Test the line segment class to make sure the methods you added work correctly.

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

  • Create a class called Main – copy the code below into the class
  • Follow the comments to write the main method and the enterLineSegmentEndPoints in the Main class.
  • The program, when finished, should create the output given
  • When you have it running correctly, upload the LineSegment and Main class to zybook and submit it for grading.

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)

Solutions

Expert Solution

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;
     
}

}


Related Solutions

Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements. Add the four fields to the Headphone class. Create a constructor that takes two parameters....
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Part Fields: id (int) description (String) price (double) onSale (boolean) 1 Constructor: takes parameters partId, partDescrip, and partPrice to initialize fields id, description, and price; sets onSale field to false. Methods: Write get-methods (getters) for all four fields. The getters should be named getId, getDescription, getPrice, and isOnSale.
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final grade for the course. Assume the following (you can hard-code these values in your program). Assignments are worth 30% Labs are worth 40% Mid-term is worth 15% Final is worth 15% Ask for the grades in each category. Store them in a double variable. Print out the final percentage grade.           For example, Final Grade Calculation Enter percentage grades in the following order. Assignments,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT