Question

Must be in Java.

Show proper reasoning with step by step process.

Comment on the code please and show proper indentation.

Use simplicity.

Must match the exact Output.

You are developing a painting application for tablet computers. One of the features you want to implement is a line draw mode

3. Implement a test program which will: a. Read x and y coordinates for two points from the console. b. Create two new Point

CODE GIVEN:

LineSegment.java

LineSegmentTest.java

Point.java

public class Point {

private double x, y; // x and y coordinates of point

/**
* Creates an instance of Point with the provided coordinates
* @param inX the x coordinate
* @param inY the y coordinate
*/
public Point (double inX, double inY) {
this.x = inX;
this.y = inY;
}

/**
* Creates a duplicate Point
* @param toCopy the Point to duplicate
*/
public Point (Point toCopy) {
this (toCopy.x, toCopy.y);
  
}

// GETTERS
public double getX () { return x; }
public double getY () { return y; }

// SETTERS
public void setX (double newX) { x = newX; }
public void setY (double newY) { y = newY; }

/**
* Returns the Euclidean distance between this Point and the provided one.
* @param other the other Point
* @return the Euclidean distance
*/
public double distance (Point other) {
   double result = Math.sqrt (Math.pow (this.x-other.x, 2) + Math.pow (this.y-other.y,2));
   return result;
}

public String toString () {
   return "(" + x + "," + y + ")";
}

public boolean equals (Object o) {
   if (this == o) return true;
   if (!(o instanceof Point)) return false;
   Point p = (Point) o;
   return (this.x == p.x && this.y == p.y);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// LineSegment.java

public class LineSegment {

      // two end points

      private Point p1;

      private Point p2;

      // constructor taking two end points

      public LineSegment(Point p1, Point p2) {

            this.p1 = p1;

            this.p2 = p2;

      }

      // returns true if two line segments are equal

      public boolean equals(Object o) {

            // checking if o is a LineSegment

            if (o instanceof LineSegment) {

                  // safe type casting

                  LineSegment other = (LineSegment) o;

                  // if this.p1=other.p1 & this.p2=other.p2 or this.p1=other.p2 &

                  // this.p2=other.p1, line segments are equal

                  if ((this.p1.equals(other.p1) && this.p2.equals(other.p2))

                              || (this.p1.equals(other.p2) && this.p2.equals(other.p1))) {

                        return true;

                  }

            }

            // not equal

            return false;

      }

      // returns a String in proper format containing end points info

      public String toString() {

            return "LineSegment " + p1 + " " + p2;

      }

}

// LineSegmentTest.java

import java.util.Scanner;

public class LineSegmentTest {

      public static void main(String[] args) {

            //scanner for user input

            Scanner scanner = new Scanner(System.in);

            //reading two double values, creating first point

            Point p1 = new Point(scanner.nextDouble(), scanner.nextDouble());

            //reading two double values, creating second point

            Point p2 = new Point(scanner.nextDouble(), scanner.nextDouble());

            //creating a LineSegement using these points

            LineSegment line1 = new LineSegment(p1, p2);

            //doing the same for creating another line segment

            Point p3 = new Point(scanner.nextDouble(), scanner.nextDouble());

            Point p4 = new Point(scanner.nextDouble(), scanner.nextDouble());

            LineSegment line2 = new LineSegment(p3, p4);

            //displaying both

            System.out.println(line1);

            System.out.println(line2);

            //displaying a message if they are equal

            if (line1.equals(line2)) {

                  System.out.println("The line segments are equal.");

            }

      }

}

/*OUTPUT*/

0 0 10 0

10 0 0 0

LineSegment (0.0,0.0) (10.0,0.0)

LineSegment (10.0,0.0) (0.0,0.0)

The line segments are equal.

Add a comment
Know the answer?
Add Answer to:
Must be in Java. Show proper reasoning with step by step process. Comment on the code...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...

    // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a 2D point. */ public class Point { private double x; private double y; /** * Construct the point (<code>x</code>, <code>y</code>). * @param x the <code>Point</code>'s x coordinate * @param y the <code>Point</code>'s y coordinate */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Move the point to (<code>newX</code>, <code>newY</code>). * @param newX the new x coordinate for...

  • Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** *...

    Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** * A class that models a bounding ball */ public class Ball {    // Minimum and maximum x and y values for the screen    private static double minX;    private static double minY;    private static double maxX;    private static double maxY;    private Point center;    private double radius;    // Every time the ball moves, its x position changes by...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • Create a UML diagram with 3 lines per class/interface including all constructors. public class Point {...

    Create a UML diagram with 3 lines per class/interface including all constructors. public class Point { public double X, Y; public Point() { this(0, 0); } public Point(double newX, double newY) { X = newX; Y = newY; } public static double distance(Point A, Point B) { return Math.sqrt(Math.pow(A.X-B.X, 2) + Math.pow(A.Y-B.Y, 2)); } } public interface Polygon { public int getNumberOfSides();    public double getPerimeter();    public double getArea();    } public abstract class Simple_polygon implements Polygon{ public Point...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...

  • Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public...

    Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public void setImmune(); public boolean isImmune(); public Point getPosition(); } Movable.java interface Movable { public void move(int step); } public class Point { private int xCoordinate; private int yCoordinate; /** * Creates a point at the origin (0,0) and colour set to black */ public Point(){ xCoordinate = 0; yCoordinate = 0; } /** * Creates a new point at a given coordinate and colour...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Question 11 (20 points) Given the class Point below, define a class Circle that represent a...

    Question 11 (20 points) Given the class Point below, define a class Circle that represent a circle with a given center and radius. The circle class should have a center attribute named center as well as a floating point radius attribute. The center is a point object, defined by the class Point. The class should also have these members: the constructor of the class, which should take parameters to initialize all attributes - a getter for center a setter for...

  • Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you w...

    Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you will build the below hierarchy: Overview: You will implement the supplier code: Shape, Square, TSquare, and Triangle, which will be used by the Homework7Main program. This program will use the DrawingPanel to draw these objects onto a canvas. Specification: You will take advantage of inheritance with using a super class, Shape.java. (below) Shape is an abstract class, which means it can be extended, but...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT