Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This concerns the constructionof a java class to represent a vector in two dimen

ID: 668767 • Letter: T

Question

This concerns the constructionof a java class to represent a vector in two dimensions. A vector has magnitude and direction. The following is a graphical depiction of a vector, v:

A vector can be represented by its horizontal and vertical components i.e. v=(vx,vy).

Implement the Vector class Test2K class uses such that the code been given runs without modification.

Sample I/O:

Make a selection and press return:

(0) Quit, (1) Test accessors, (2) Test scalar multiply(), (3) Test dotProduct(), (4) Test equals()

1

Enter the x component and y component (separated by a space):

5 -7

Created a Vector object with the given values for vx and vy.

Result of calling accessors: getVX()=5.00, getVY()=-7.00.

(0) Quit, (1) Test accessors, (2) Test scalar multiply(), (3) Test dotProduct(), (4) Test equals()

2

Enter the x component and y component (separated by a space):

Created Vector object: v=(vx=-3.00,vy=3.00)

Enter multiplier:

.5

New Vector: v= (vx=-1.50, vy=1.50)

(0) Quit, (1) Test accessors, (2) Test scalar multiply(), (3) Test dotProduct(), (4) Test equals()

3

Enter the x component and y component (separated by a space):

-3 3

Created Vector object: v=(vx=-3.00,vy=3.00)

Enter the x component and y component (separated by a space):

4 2

Created Vector object: v=(vx=4.00,vy=2.00)

Result of dot product of the vectors: -6.0

(0) Quit, (1) Test accessors, (2) Test scalar multiply(), (3) Test dotProduct(), (4) Test equals()

4

Enter the x component and y component (separated by a space):

12 -35

Created Vector object: v=(vx=12.00,vy=-35.00)

Enter the x component and y component (separated by a space):

12 -35

Created Vector object: v=(vx=12.00,vy=-35.00)

Testing Vector objects for equality: true

(0) Quit, (1) Test accessors, (2) Test scalar multiply(), (3) Test dotProduct(), (4) Test equals()

0

Note:

*the multiplication of vector v=(vx,vy) by a value m produces a vector v'=(mvx,mvy).

*given two vactors v=(vx,vy) and v'=(v'x,v'y) their dot product is the value of (vx x v'x, vy x v'y).

*The string class format method must be used to produced a string representation of a real number rounded to 2 decimal places e.g String.format("%.2f",4.896) produces the result "4.90".

import java.util.Scanner;

public class Test2K {

private Test2K() {}
  
private static int getSelection(Scanner scanner) {
System.out.println(" Make a selection and press return:");
System.out.print("(0) Quit, ");
System.out.print("(1) Test accessors, ");
System.out.println("(2) Test scalar multiply(), ");
System.out.print("(3) Test dotProduct(), ");
System.out.println("(4) Test equals() ");
return scanner.nextInt();

}
  
private static Vector makeVector(Scanner scanner) {
System.out.println("Enter x component and y component (separated by a space):");
return new Vector(scanner.nextInt(), scanner.nextInt());
}

public static void main(String[] args) {
  
final Scanner scanner = new Scanner(System.in);
  
int selection = getSelection(scanner);
  
while (selection!=0) {
  
if (selection==1) {
final Vector vOne = Test2K.makeVector(scanner);
System.out.println("Created a Vector object with the given values for vx and vy.");
System.out.printf("Result of calling accessors: getVX()=%.2f, getVY()=%.2f. ", vOne.getVX(), vOne.getVY());
}
else if (selection==2){
final Vector vOne = Test2K.makeVector(scanner);
System.out.println("Created Vector object: "+vOne);
System.out.println("Enter multiplier:");
final double multiplier = scanner.nextDouble();
final Vector vTwo = vOne.multiply(multiplier);
System.out.println("New Vector: "+vTwo);
}
else if (selection==3||selection==4) {
final Vector vOne = Test2K.makeVector(scanner);
System.out.println("Created Vector object: "+vOne);
final Vector vTwo = Test2K.makeVector(scanner);
System.out.println("Created Vector object: "+vTwo);
  
if (selection==3) {
final double result = vOne.dotProduct(vTwo);
System.out.println("Result of dot product of the vectors: "+result);
}
else { // selection==4
Object oTwo = vTwo;
System.out.println("Testing Vector objects for equality: "+vOne.equals(oTwo));
}
}
else {
System.out.println("That selection was not recognised.");
}

selection = getSelection(scanner);
}
  
scanner.close();
}


}

Explanation / Answer

// Vector.java

public class Vector{
   double vx, vy;
   public Vector(double d, double e){
       vx = d;
       vy = e;
   }
   public Object getVX(){
       return vx;
   }
   public Object getVY(){
       return vy;
   }
   public Vector multiply(double multiplier){
       return new Vector(vx * multiplier, vy * multiplier);
   }
   public double dotProduct(Vector v){
       return vx * v.vx + vy * v.vy;
   }
}

// Test2K.java

import java.util.Scanner;

public class Test2K {
   private Test2K() {}
   private static int getSelection(Scanner scanner) {
       System.out.print("(0) Quit, ");
       System.out.print("(1) Test accessors, ");
       System.out.print("(2) Test scalar multiply(), ");
       System.out.print("(3) Test dotProduct(), ");
       System.out.println("(4) Test equals() ");
       return scanner.nextInt();
   }
  
   private static Vector makeVector(Scanner scanner) {
       System.out.println("Enter x component and y component (separated by a space):");
       return new Vector(scanner.nextInt(), scanner.nextInt());
   }
   public static void main(String[] args) {
       final Scanner scanner = new Scanner(System.in);  
       System.out.println("Make a selection and press return:");
       int selection = getSelection(scanner);
       while (selection!=0) {
           if (selection==1) {
               final Vector vOne = Test2K.makeVector(scanner);
               System.out.println("Created a Vector object with the given values for vx and vy.");
               System.out.printf("Result of calling accessors: getVX()=%.2f, getVY()=%.2f. ", vOne.getVX(), vOne.getVY());
           }
           else if (selection==2){
               final Vector vOne = Test2K.makeVector(scanner);
               System.out.println("Created Vector object: "+vOne);
               System.out.println("Enter multiplier:");
               final double multiplier = scanner.nextDouble();
               final Vector vTwo = vOne.multiply(multiplier);
               System.out.println("New Vector: "+vTwo);
           }
           else if (selection==3||selection==4) {
               final Vector vOne = Test2K.makeVector(scanner);
               System.out.println("Created Vector object: "+vOne);
               final Vector vTwo = Test2K.makeVector(scanner);
               System.out.println("Created Vector object: "+vTwo);
              
               if (selection==3) {
                   final double result = vOne.dotProduct(vTwo);
                   System.out.println("Result of dot product of the vectors: "+result);
               }
               else { // selection==4
                   Object oTwo = vTwo;
                   System.out.println("Testing Vector objects for equality: "+vOne.equals(oTwo));
               }
           }
           else {
               System.out.println("That selection was not recognised.");
           }
           selection = getSelection(scanner);
       }
      
       scanner.close();
   }
}