Question: public class Util { // return a random number between min and max inclusive public static int getRandom(int min, int max) {} // round the

public class Util { // return a random number between min and max inclusive public static int getRandom(int min, int max) {} // round the double d to the nearest int. public static int round(double d) {} // return a random color where red is in the range 0..maxR etc. Handle illegal parameter values (< 0 or >255) by constraining to nearest legal value. public static Color getColor(int maxR, int maxG, int maxB) {} // return the absolute difference between m and n public static int absoluteDifference(int m, int n) {} // return radian value corresponding to degrees 
 public static double getRadians(double degrees) {} // return degree value corresponding to radians public static double getDegrees(double radians) {} // round to numDigits number of digits after the decimal point e.g. 2.356 --> 2.4 if numDigits is 1 public static double round(double d, int numDigits) { } Then implement the Vect class below. A Vect object is similar to a mathematical vector with a horizontal component vx and a vertical component vy. (Except that visually, a positive value for vy mean the object points downward rather than upward. That's how Java coordinates work. Also, angles are measured in a clockwise direction rather than counterclockwise like on the unit circle. ) 

Meanings of each method should be clear from the method name. Use the attached VectorTester.java program to test your methods. (You can run the attached Program to see desired output.)

// For now this all we need for the Point class. class Point { public double x,y; }

public class Vect { public double vx, vy; public Vect(double vx, double vy) { this.vx = vx; this.vy = vy; } public Vect() { vx = vy = 0; } public Vect(Point p, Point q) { this.vx = q.x-p.x; this.vy = q.y-p.y; } // keep the same length but change the direction public void setAngle(double angle) {} // keep same direction but change the length public void setLength(double length) {} public void multiplyBy(double x) {} public void setLengthAndAngle(double length, double angle) {} // Reflect the vector across a line with the given angle. // For example, if vx = vy = 10 and angle = PI/2 then new vx = -10 and new vy = 10 public void reflect(double angle) {} public double getLength() {} // return angle in radians from horizontal axis: so x = y = 1 would give angle = PI/4. public double getAngle() {} public void negateX() {} // change vx to -vx public void negateY() {} // change vy to -vy // negate both x and y public void negate() {} // keep the same length but rotate by angle public void rotateClockwise(double angle) {} public double getAngleBetween(Vect other) {} 
 // return true if angle between this and other is strictly between -pi/2 and pi/2 // or if angle is measured positively, between 0 and pi/2 or between 3pi/2 and 2pi. 
 public boolean formsAcuteAngle(Vect other) {} // draw with a small circle at the base, so it's clear what the direction is. // Also draw an arrow at the other end. (harder!). public void draw(Graphics g, int x, int y) {} 
 // return the absolute difference between m and n 
 public static double absoluteDifference(double m, double n) {} } 

Find here attached the VectorTester.java

 import java.awt.*; import java.applet.Applet; import java.util.Scanner; public class VectorTester extends Applet{ Scanner in = new Scanner(System.in); Vect v1, v2; private void sop(String s) {System.out.print(s);} private void sopl(String s) {System.out.println(s);} public void init() { this.setBackground(Color.BLACK); sop("Enter vx, vy to create vector: "); double vx,vy; vx = in.nextInt(); vy = in.nextInt(); v1 = new Vect(vx,vy); v2 = new Vect(vx,vy); } int getChoice () { sopl(""); sopl("\t 0. set vx and vy"); sopl("\t 1. set length and angle"); sopl("\t 2. set length"); sopl("\t 3. set angle"); sopl("\t 4. rotate"); sopl("\t 5. negate x"); sopl("\t 6. negate y"); sopl("\t 7. negate"); sopl("\t 8. reflect"); sopl("\t 9. get length"); sopl("\t10. get angle"); sopl("\t11. get angle between"); sopl("\t12. forms acute angle"); sopl("\t13. exit"); sop("Enter choice: "); return Math.min(13, Math.max(0,in.nextInt())); } public void updateVectors() { int ch = getChoice(); Vect v1Save = new Vect(v1.vx, v1.vy); switch (ch) { case 0: sop("Enter vy and vy: "); v1.vx = in.nextDouble(); v1.vy = in.nextDouble(); break; case 1: sop("Enter length and degrees: "); v1.setLengthAndAngle(in.nextDouble(),in.nextDouble()/180*Math.PI); break; case 2: sop("Enter length: "); v1.setLength(in.nextDouble()); break; case 3: sop("Enter degrees: "); v1.setAngle(Util.getRadians(in.nextDouble())); break; case 4: sop("Enter degrees: "); v1.rotateClockwise(Util.getRadians(in.nextDouble())); break; case 5: v1.negateX(); break; case 6: v1.negateY(); break; case 7: v1.negate(); break; case 8: sop("Enter degrees: "); v1.reflect(Util.getRadians(in.nextDouble())); break; case 9: sopl("Length = " + Util.round(v1.getLength(),2)); break; case 10: sopl("Angle = " + Util.round(Util.getDegrees(v1.getAngle()),2)); break; case 11: sopl("Angle between = " + Util.round(Util.getDegrees(v1.getAngleBetween(v2)),2)); break; case 12: sopl("Forms acute angle is = " + v1.formsAcuteAngle(v2)); break; case 13: System.exit(0); } if (ch < 9) // These actions change v1, so v2 becomes the old v1. { v2.vx = v1Save.vx; v2.vy = v1Save.vy; } } public void paint(Graphics g) { g.setColor(Color.LIGHT_GRAY); v2.draw(g, 200, 200); g.setColor(Color.MAGENTA.darker()); v1.draw(g, 200, 200); updateVectors(); repaint(); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!