Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please use below the codes for class Point and class PointMain 1. Class Point // A Point object represents a pair of (x, y) coordinates.

image text in transcribed

please use below the codes for class Point and class PointMain

1. Class Point

// A Point object represents a pair of (x, y) coordinates. // Class invariant: x >= 0 && y >= 0. (Quadrant I only)

public class Point { private int x; private int y;

// Constructs a new point at the given (x, y) location. // pre: x >= 0 && y >= 0 public Point(int x, int y) { if (x

// Constructs a new point at the origin, (0, 0). public Point() { this(0, 0); // calls Point(int, int) constructor }

// Returns the distance between this Point and (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); }

// Returns whether o refers to a point with the same (x, y) // coordinates as this point. Robust version. public boolean equals(Object o) { if (o instanceof Point) { Point other = (Point) o; return this.x == other.getX() && this.y == other.getY(); } else { // not a Point object return false; } }

// Returns the x-coordinate of this point. public int getX() { return this.x; }

// Returns the y-coordinate of this point. public int getY() { return this.y; }

// Returns a String representation of this point. public String toString() { return "(" + this.x + ", " + this.y + ")"; }

// Returns a new point, shifted from this one by dx and dy. // pre: x + dx >= 0 && y + dy >= 0 public Point translate(int dx, int dy) { return new Point(this.x + dx, this.y + dy); } }

2. class PointMainMin

// A client program that deals with simple points. // Minimal version, to accompany immutable Point class.

public class PointMainMin { public static void main(String[] args) { // create two Point objects Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); // print each point and its distance from the origin System.out.println("p1 is " + p1); System.out.printf("distance from origin = %3.2f ", p1.distanceFromOrigin()); System.out.println("p2 is " + p2); System.out.printf("distance from origin = %3.2f ", p2.distanceFromOrigin()); } }

19. Modify the point class from Chapter 8 so that it defines a natural ordering by implementing the Comparable interface. Compare the points by y-major order; that is, points with smaller y-coordinate values should come before those with higher y-coordinate values. Break ties by comparing X-coordinate values

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MFDBS 91 3rd Symposium On Mathematical Fundamentals Of Database And Knowledge Base Systems Rostock Germany May 6 9 1991

Authors: Bernhard Thalheim ,Janos Demetrovics ,Hans-Detlef Gerhardt

1991st Edition

3540540091, 978-3540540090

More Books

Students also viewed these Databases questions

Question

2. List the advantages of listening well

Answered: 1 week ago