Question

In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...

In Java please

10.9 Lab 6

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

Homework Answers

Answer #1
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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
JAVA. The question mentioned was: Create a class called Point that represents a point in the...
JAVA. The question mentioned was: Create a class called Point that represents a point in the cartesian coordinate system. The class should have fields representing the x coordinate and y coordinate (both are integer type). Question I need an answer to: Write a client class that would create two objects in the Point class above called p1 and p2 and assigns values to the filed in these objects. Print out the x coordinate followed by x comma and last coordinate...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position variable by adding the position to the parameter variable. 2)debug the code public class food { public static void main(String[] args) { Fruits apple = new Fruits(20); // Write the statement to call the method that will increase the instance variable position by 6. Fruits.setPosition(6); apple.getPosition(); } }
(In Java) 1. Create a 6X6 2D Array called numCourses 2. Sum the rows and print...
(In Java) 1. Create a 6X6 2D Array called numCourses 2. Sum the rows and print out the results with text identifying what you are outputting. 3. Sum the columns and print out the results with text identifying what you are outputting. 4. Compare the rows output and calculate and output the minimum value and maximum value. */ public class TwoDArrayExamplesHW{ public static void main(String[] args){ int[][] numCourses = {{2, 3, 2, 0, 0},    {2, 3, 2, 3, 1},...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT