Question

In: Computer Science

COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...

COMPLETE JAVA CODE

public class Point2 {

private double x;
private double y;
  
/**
* Create a point with coordinates <code>(0, 0)</code>.
*/
public Point2() {

complete JAVA code
this.set(0.0, 0.0);

COMPLETE CODE
}
  
/**
* Create a point with coordinates <code>(newX, newY)</code>.
*
* @param newX the x-coordinate of the point
* @param newY the y-coordinate of the point
*/
public Point2(double newX, double newY) {

complete Java code
this.set(newX, newY);
}
  
/**
* Create a point with the same coordinates as <code>other</code>.
*
* @param other another point
*/
public Point2(Point2 other) {

complete Java code

}
  
/**
* Returns the x-coordinate of this point.
*
* @return the x-coordinate of this point
*/
public double getX() {

complete Java code
  
}
  
/**
* Returns the y-coordinate of this point.
*
* @return the y-coordinate of this point
*/
public double getY() {

complete Java code
return this.y;
}
  
/**
* Sets the x-coordinate of this point to <code>newX</code>.
*
* @param newX the new x-coordinate of this point
*/
public void setX(double newX) {

complete Java code
this.x = newX;
}
  
/**
* Sets the y-coordinate of this point to <code>newY</code>.
*
* @param newY the new y-coordinate of this point
*/
public void setY(double newY) {

complete Java code
this.y = newY;
}
  
  
/**
* Sets the x-coordinate and y-coordinate of this point to
* <code>newX</code> and <code>newY</code>, respectively.
*
* @param newX the new x-coordinate of this point
* @param newY the new y-coordinate of this point
*/
public void set(double newX, double newY) {
this.x = newX;
this.y = newY;
}
  
  
/**
* Adds a vector to this point changing the coordinates of this point.
*
* <p>
* Mathematically, if this point is <code>a</code> and the vector is
* <code>v</code> then invoking this method is equivalent to computing
* <code>a + v</code> and assigning the value back to <code>a</code>.
*
* @param v a vector
* @return this <code>Point2</code> object
*/
public Point2 add(Vector2 v) {

complete Java code
this.x += v.getX();
this.y += v.getY();
return this;
}
  
/**
* Returns a new <code>Point2</code> equal to <code>a - b</code>.
*
* @param a
* a point
* @param b
* another point
* @return a new <code>Point2</code> equal to <code>a - b</code>
*/
public static Vector2 subtract(Point2 a, Point2 b) {
Vector2 result = new Vector2(a.getX() - b.getX(), a.getY() - b.getY());
return result;
}
  
  
/**
* Returns a string representation of this point. The string
* representation of this point is the x and y-coordinates
* of this point, separated by a comma and space, inside a pair
* of parentheses.
*
* @return a string representation of this point
* @see java.lang.Object#toString()
*/
@Override
public String toString() {

complete Java code
String s = String.format("(%s, %s)", this.getX(), this.getY());
return s;
}

/**
* Returns a hash code for this point
*
* @return a hash code for this point
*/
@Override
public int hashCode() {

complete Java code
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(this.x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(this.y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}

  
/**
* Compares this point with the given object. The result is
* <code>true</code> if and only if the argument is not <code>null</code>
* and is a <code>Point2</code> object having the same coordinates as this
* object.
*
* @param obj
* the object to compare this vector against
* @return <code>true</code> if the given object represents a
* <code>Point2</code> equivalent to this point,
* <code>false</code> otherwise
*/
@Override
public boolean equals(Object obj) {

complete Java code
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Point2 other = (Point2) obj;
if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
return false;
}
if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
return false;
}
return true;
}
  
/**
* Determines if two points are almost equal (similar). Two points are
* similar if the magnitude of their vector difference is smaller than the
* specified tolerance.
*
* @param other
* the other point to compare
* @param tol
* the threshold length of the vector difference
* <code>(this - other)</code>
* @return <code>true</code> if the length of <code>(this - other)</code> is
* less than <code>tol</code>, and <code>false</code> otherwise
*/
public boolean similarTo(Point2 other, double tol) {
Vector2 delta = Point2.subtract(this, other);
return delta.mag() < Math.abs(tol);
}
}

Solutions

Expert Solution

Most of the code in this class is already complete. I have only completed copy constructor and getX method. Since Vector2 class is not provided, I did not get deep into that, assuming that class works correctly.

Here is the updated 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. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


// Point2.java


public class Point2 {

        private double x;
        private double y;

        /**
         * Create a point with coordinates <code>(0, 0)</code>.
         */
        public Point2() {
                this.set(0.0, 0.0);
        }

        /**
         * Create a point with coordinates <code>(newX, newY)</code>.
         * 
         * @param newX
         *            the x-coordinate of the point
         * @param newY
         *            the y-coordinate of the point
         */
        public Point2(double newX, double newY) {
                this.set(newX, newY);
        }

        /**
         * Create a point with the same coordinates as <code>other</code>.
         * 
         * @param other
         *            another point
         */
        public Point2(Point2 other) {
                //setting x and y values from other point
                set(other.x, other.y);
        }

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

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

        /**
         * Sets the x-coordinate of this point to <code>newX</code>.
         * 
         * @param newX
         *            the new x-coordinate of this point
         */
        public void setX(double newX) {
                this.x = newX;
        }

        /**
         * Sets the y-coordinate of this point to <code>newY</code>.
         * 
         * @param newY
         *            the new y-coordinate of this point
         */
        public void setY(double newY) {
                this.y = newY;
        }

        /**
         * Sets the x-coordinate and y-coordinate of this point to <code>newX</code>
         * and <code>newY</code>, respectively.
         * 
         * @param newX
         *            the new x-coordinate of this point
         * @param newY
         *            the new y-coordinate of this point
         */
        public void set(double newX, double newY) {
                this.x = newX;
                this.y = newY;
        }

        /**
         * Adds a vector to this point changing the coordinates of this point.
         * 
         * <p>
         * Mathematically, if this point is <code>a</code> and the vector is
         * <code>v</code> then invoking this method is equivalent to computing
         * <code>a + v</code> and assigning the value back to <code>a</code>.
         * 
         * @param v
         *            a vector
         * @return this <code>Point2</code> object
         */
        public Point2 add(Vector2 v) {
                this.x += v.getX();
                this.y += v.getY();
                return this;
        }

        /**
         * Returns a new <code>Point2</code> equal to <code>a - b</code>.
         * 
         * @param a
         *            a point
         * @param b
         *            another point
         * @return a new <code>Point2</code> equal to <code>a - b</code>
         */
        public static Vector2 subtract(Point2 a, Point2 b) {
                Vector2 result = new Vector2(a.getX() - b.getX(), a.getY() - b.getY());
                return result;
        }

        /**
         * Returns a string representation of this point. The string representation
         * of this point is the x and y-coordinates of this point, separated by a
         * comma and space, inside a pair of parentheses.
         * 
         * @return a string representation of this point
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
                String s = String.format("(%s, %s)", this.getX(), this.getY());
                return s;
        }

        /**
         * Returns a hash code for this point
         * 
         * @return a hash code for this point
         */
        @Override
        public int hashCode() {

                final int prime = 31;
                int result = 1;
                long temp;
                temp = Double.doubleToLongBits(this.x);
                result = prime * result + (int) (temp ^ (temp >>> 32));
                temp = Double.doubleToLongBits(this.y);
                result = prime * result + (int) (temp ^ (temp >>> 32));
                return result;
        }

        /**
         * Compares this point with the given object. The result is
         * <code>true</code> if and only if the argument is not <code>null</code>
         * and is a <code>Point2</code> object having the same coordinates as this
         * object.
         * 
         * @param obj
         *            the object to compare this vector against
         * @return <code>true</code> if the given object represents a
         *         <code>Point2</code> equivalent to this point, <code>false</code>
         *         otherwise
         */
        @Override
        public boolean equals(Object obj) {

                if (this == obj) {
                        return true;
                }
                if (obj == null) {
                        return false;
                }
                if (getClass() != obj.getClass()) {
                        return false;
                }
                Point2 other = (Point2) obj;
                if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
                        return false;
                }
                if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
                        return false;
                }
                return true;
        }

        /**
         * Determines if two points are almost equal (similar). Two points are
         * similar if the magnitude of their vector difference is smaller than the
         * specified tolerance.
         * 
         * @param other
         *            the other point to compare
         * @param tol
         *            the threshold length of the vector difference
         *            <code>(this - other)</code>
         * @return <code>true</code> if the length of <code>(this - other)</code> is
         *         less than <code>tol</code>, and <code>false</code> otherwise
         */
        public boolean similarTo(Point2 other, double tol) {
                Vector2 delta = Point2.subtract(this, other);
                return delta.mag() < Math.abs(tol);
        }
}

Related Solutions

Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT