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...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
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 +=...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x);...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x); void setY(double y); void setZ(double z); double getX() const; double getY() const; double getZ() const; point3d& operator=(const point3d& rhs); private: double x; double y; double z; }; Given the Point class above, complete the following: 1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two point3d objects. 2. What is the output of the...
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...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get started with the constructors, implement the class variables and    // =====   the accessor methods (getX,getY). The tester for the constructors relies on these    //           methods being implemented. After this, move ahead with the constructors          // class variables here       COMPLETE JAVA CODE: /** * Creates the vector <code>(0.0, 0.0)</code>. */ public Vector2() {       COMPLETE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT