Question

In: Computer Science

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 JAVA CODE:
  
}

/**
* Creates the vector <code>(x, y)</code>.
*
* @param x
* the x-component of the vector
* @param y
* the y-component of the vector
*/
public Vector2(double x, double y) {
  
   COMPLETE JAVA CODE:
  
}

/**
* Creates a vector with the same components as another vector.
*
* @param other
* a vector to copy the components from
*/
public Vector2(Vector2 other) {

  
   COMPLETE JAVA CODE:
  
}

/**
* Returns the x component of the vector.
*
* @return the x component of the vector.
*/
public double getX() {
  
   COMPLETE JAVA CODE:
  
  
}

/**
* Sets the x component of the vector.
*
* @param x
* the new value of the x component.
*/
public void setX(double x) {
  
  
   COMPLETE JAVA CODE:
  
}

/**
* Returns the y component of the vector.
*
* @return the y component of the vector.
*/
public double getY() {
  
  
   COMPLETE JAVA CODE:
  
  
}

/**
* Sets the y component of the vector.
*
* @param y
* the new value of the y component.
*/
public void setY(double y) {

  
  
   COMPLETE JAVA CODE:
  
}

/**
* Sets the x and y component of the vector.
*
* @param x
* the new value of the x component.
* @param y
* the new value of the y component.
*/
public void set(double x, double y) {

  
   COMPLETE JAVA CODE:
  
  
}

/**
* Add a vector to this vector changing the components of this vector.
*
* <p>
* Mathematically, if this vector is <code>a</code> and the other vector is
* <code>b</code> then invoking this method is equivalent to computing
* <code>a + b</code> and assigning the value back to <code>a</code>.
*
* @param other
* the vector to add to this vector.
* @return this <code>Vector2D</code> object
*/
public Vector2 add(Vector2 other) {
  
   COMPLETE JAVA CODE:
  
  
  
}

/**
* Subtract a vector from this vector changing the components of this
* vector.
*
* <p>
* Mathematically, if this vector is <code>a</code> and the other vector is
* <code>b</code> then invoking this method is equivalent to computing
* <code>a - b</code> and assigning the value back to <code>a</code>.
*
* @param other
* the vector to subtract this vector.
* @return this <code>Vector2D</code> object
*/
public Vector2 subtract(Vector2 other) {
  
   COMPLETE JAVA CODE:
  
  
  
}

/**
* Multiply this vector by a scalar amount changing the components of this
* vector.
*
* <p>
* Mathematically, if this vector is <code>a</code> and the scalor is
* <code>s</code> then invoking this method is equivalent to computing
* <code>s * a</code> and assigning the value back to <code>a</code>.
*
* @param s
* the scalar value to multiply the vector by
* @return this <code>Vector2D</code> object
*/
public Vector2 multiply(double s) {
  
   COMPLETE JAVA CODE:
  
  
  
}

/**
* Returns the magnitude of this vector.
*
* @return the magnitude of this vector.
*/
public double mag() {
  
  
   COMPLETE JAVA CODE:
  
}

/**
* Returns a new <code>Vector2D</code> equal to <code>a + b</code>.
*
* @param a
* a vector
* @param b
* another vector
* @return a new <code>Vector2D</code> equal to <code>a + b</code>
*/
public static Vector2 add(Vector2 a, Vector2 b) {
  
  
  
   COMPLETE JAVA CODE:
  
}

/**
* Returns a new <code>Vector2D</code> equal to <code>a - b</code>.
*
* @param a
* a vector
* @param b
* another vector
* @return a new <code>Vector2D</code> equal to <code>a - b</code>
*/
public static Vector2 subtract(Vector2 a, Vector2 b) {
  
  
   COMPLETE JAVA CODE:
  
  
}

/**
* Returns a new <code>Vector2D</code> equal to <code>s * a</code>.
*
* @param s
* a scalar
* @param a
* a vector
* @return a new <code>Vector2D</code> equal to <code>s * a</code>
*/
public static Vector2 multiply(double s, Vector2 a) {
  

  
   COMPLETE JAVA CODE:
  
}

/**
* Returns the vector having magnitude one pointing in the direction
* <code>theta</code> degrees from the x axis.
*
* <p>
* The components of the vector are equal to
* <code>(Math.cos(rad), Math.sin(rad))</code> where <code>rad</code> is
* <code>theta</code> expressed in radians.
*
* @param theta
* the direction that the vector is pointing in measured in
* degrees from the x axis
* @return the unit vector pointing in the given direction
*/
public static Vector2 dirVector(double theta) {
  

  
   COMPLETE JAVA CODE:
  
}

/**
* Returns a string representation of the vector. The string is the name of
* the vector, followed by the comma separated components of the vector
* inside parentheses.
*
* @return a string representation of the vector
*/
@Override
public String toString() {
  

   COMPLETE JAVA CODE:
  
}

/**
* Determines if two vectors are almost equal (similar). Two vectors are
* similar if the magnitude of their vector difference is smaller than the
* specified tolerance.
*
* @param other
* the other vector 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(Vector2 other, double tol) {
  
  
   COMPLETE JAVA CODE:
  
  
  
}
  
  
}

Solutions

Expert Solution

I have implement a small Test class to try out the functions,  

import java.math.*;

//implemented Vector2 class
class Vector2 {

  private double x, y;

  public Vector2() {
    x = 0;
    y = 0;
  }

  public Vector2(double x, double y) {
    this.x = x;
    this.y = y;
  }

  public Vector2(Vector2 other) {
    this.x = other.x;
    this.y = other.y;
  }

  public double getX() {
    return x;
  }

  public void setX(double x) {
    this.x = x;
  }

  public double getY() {
    return y;
  }

  public void setY(double y) {
    this.y = y;
  }

  public void set(double x, double y) {
    this.x = x;
    this.y = y;
  }

  public Vector2 add(Vector2 other) {
    this.x += other.x;
    this.y += other.y;

    return this;
  }

  public Vector2 subtract(Vector2 other) {
    this.x -= other.x;
    this.y -= other.y;

    return this;
  }

  public Vector2 multiply(double s) {
    this.x *= s;
    this.y *= s;

    return this;
  }

  public double mag() {
    return Math.sqrt(x * x + y * y);
  }

  public static Vector2 add(Vector2 a, Vector2 b) {
    return new Vector2(a.x + b.x, a.y + b.y);
  }

  public static Vector2 subtract(Vector2 a, Vector2 b) {
    return new Vector2(a.x - b.x, a.y - b.y);
  }

  public static Vector2 multiply(double s, Vector2 a) {
    return new Vector2(s * a.x, s * a.y);
  }

  public static Vector2 dirVector(double theta) {
    return new Vector2(Math.cos(theta), Math.sin(theta));
  }

  @Override
  public String toString() {
    return "Vector2_" + Integer.toHexString(System.identityHashCode(this)) + "  [x=" + x + ", y=" + y + "]";
  }

  public boolean similarTo(Vector2 other, double tol) {

    Vector2 c = Vector2.subtract(this, other);
    double cLength = c.mag();

    if (cLength < tol)
      return true;
    else
      return false;

  }

}

//example test class code
public class Test {
  public static void main(String args[]) {

    Vector2 a = new Vector2(3, 4);
    Vector2 b = new Vector2(12, 5);
    Vector2 c = Vector2.subtract(a, b);
    System.out.println(a.mag());
    System.out.println(b.mag());
    System.out.println(c.mag());
    System.out.println(a.similarTo(b, 10));
  }
}

s


Related Solutions

Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
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...
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...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
I needv pseudocode and a flowchart for the following java code public class AcmePay { public...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int hours, shift, retirement = 0; do { System.out.print("Enter the number of hours worked (>0): "); hours = scanner.nextInt(); } while (hours <= 0); do { System.out.print("Enter shift [1 2 or 3]: "); shift = scanner.nextInt(); } while (shift < 1 || shift > 3); if (shift == 2 || shift ==...
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
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;   ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT