Add the following method to the Point class: public boolean isVertical (Point ot
ID: 3657106 • Letter: A
Question
Add the following method to the Point class: public boolean isVertical (Point other) Returns true if the given Point lines up vertically with this Point, that is, if their x-coordinate are the same.Explanation / Answer
// A Point object represents a pair of (x, y) coordinates. public class Point { //instance variables public int x; public int y; // Constructs a new point at the origin, (0, 0). public Point() { this(0, 0); // calls Point(int, int) constructor } // Constructs a new point with the given (x, y) location. public Point(Point p) { setLocation(p.x, p.y); } // Constructs a new point with the given (x, y) location. // pre: x >= 0 && y >= 0 public Point(int x, int y) { setLocation(x, y); } // Returns the distance between this Point and (0, 0). public double distanceFromOrigin() { return distance(new Point()); } // Returns the distance between this Point and the given other Point. public double distance(Point p) { int dx = x - p.x; int dy = y - p.y; return Math.sqrt(dx * dx + dy * dy); } // Returns whether o refers to a point with the same (x, y) // coordinates as this point. public final boolean equals(Object o) { if (o instanceof Point) { Point other = (Point) o; return x == other.x && y == other.y; } else { // not a Point object return false; } } // Returns the x-coordinate of this point. public final int getX() { return x; } // Returns the y-coordinate of this point. public final int getY() { return y; } // Sets this point's (x, y) location to the given values. // pre: x >= 0 && y >= 0 public void setLocation(int x, int y) { // if (x < 0 || y < 0) { // throw new IllegalArgumentException(); // } this.x = x; this.y = y; } // Sets the x-coordinate of this point to the given value. public void setX(int x) { this.x = x; } // Sets the y-coordinate of this point to the given value. public void setY(int y) { this.y = y; } // Returns a String representation of this point. public String toString() { return "(" + x + ", " + y + ")"; } // Shifts this point's location by the given amount. // pre: x + dx >= 0 && y + dy >= 0 public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } // YOUR CODE GOES HERE: manhattanDistance, isVertical, slope, and isCollinear }//end of Point classRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.