Skip to content

  • Home
  • Mathematics
  • English
  • History
  • Chemistry
  • Biology
  • Law
  • Medicine
  • Business
  • Toggle search form

Write a class called Line that represents a line segment between two Points. Your Line objects should have the following methods:

Posted on October 23, 2021 By Sainabou862 2 Comments on Write a class called Line that represents a line segment between two Points. Your Line objects should have the following methods:

Write a class called Line that represents a line segment between two Points. Your Line objects should have the following methods: public Line(Point p1, Point p2) Constructs a new line that contains the given two points. public Point getP1() Returns this line's first endpoint. public Point getP2() Returns this line's second endpoint. public String toString() Returns a string representation of this line, such as "[(22, 3), (4, 7)]"

Computers and Technology

Post navigation

Previous Post: On Wednesday, harry sold 3 time as many candy bars as on Tuesday. On Thursday, he sold 5 times as many bars as on Wednesday. He sold
Next Post: PLEASE HELP IM BEGGING YOU​

Comments (2) on “Write a class called Line that represents a line segment between two Points. Your Line objects should have the following methods:”

  1. morgaaaan651 says:
    October 24, 2021 at 1:35 am

    We can draw lines by using Java Swing. Below is the full example.

    Explanation:

    Explanation of Below code is in comment, line starts with //.

    import java.awt.*;

    import javax.swing.*;

    //Creating Point class which will hold point data

    class Point {

       public int x;

       public int y;

    }

    //MyCanvas is the class used to draw the line

    class MyCanvas extends JComponent {

      //Declaring 2 points variable

       Point p1;

       Point p2;

      // Creating Getter and Setter for above 2 variables

       public Point getP1() {

           return p1;

       }

       public void setP1(Point p1) {

           this.p1 = p1;

       }

       public Point getP2() {

           return p2;

       }

       public void setP2(Point p2) {

           this.p2 = p2;

       }

      //When canvas load, it will call this method and draw a line

       public void paint(Graphics g)

       {

          // draw and display the line

           g.drawLine(p1.x, p1.y, p2.x, p2.y);

       }

      //toString method to show the points data

       @Override

       public String toString() {

           return "[(" +

                   p1.x+","+p1.y+")," +

                   "("+p2.x+","+p2.y+")]";

       }

    }

    //This class will start the program and initialize canvas class

    //to draw new line on canvas

    public class DrawLine {

       public static void main(String[] a)

       {

          // creating object of JFrame

           JFrame window = new JFrame();

           // Operation when window closes

           window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           // setting size of window

           window.setBounds(30, 30, 200, 200);

           // Creating instance of MyCanvas class

           MyCanvas canvas = new MyCanvas();

          // Adding two points in MyCanvas object with its Setter method

           Point p1 = new Point();

           p1.x=10;

           p1.y=10;

           Point p2 = new Point();

           p2.x=40;

           p2.y=40;

           canvas.setP1(p1);

           canvas.setP2(p2);

          // setting canvas to draw new line

           window.getContentPane().add(canvas);

          // visibility to true

           window.setVisible(true);

           //calling toString method to show two point values

           System.out.println(canvas.toString());

       }

    }

    Reply
  2. bear7823 says:
    October 24, 2021 at 6:24 am

    import java.awt.Point;

    public class Line{

    private Point point1, point2;

    public Line(Point p1, Point p2){

    point1 = p1;

    point2 = p2;

    }

    public Point getP1(){

    return point1;

    }

    public Point getP2(){

    return point2;

    }

    public String toString(){

    return "[( The " + point1.x + "," + point1.y + "), (" + point2.x + "," + point2.y +")]";

    }

    public static void main(String args[]){

    Line lineObj = new Line(new Point(7,9), new Point(6,2));

    System.out.println(lineObj.toString());

    }

    }

    Explanation:

    Declare the points inside the class Line.Initialize the getters and setters method for the class.Define a toString method that returns the information in a formatted manner.Inside the main method, initialize an object with some values.Display the information by calling the toString method.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Advanced Placement (AP)
  • Arts
  • Biology
  • Business
  • Chemistry
  • Computers and Technology
  • Engineering
  • English
  • French
  • Geography
  • German
  • Health
  • History
  • Law
  • Mathematics
  • Medicine
  • Physics
  • SAT
  • Social Studies
  • Spanish
  • World Languages

© 2021 studyqas

Powered by PressBook WordPress theme