CS 100: Section Assignment S4
Solutions
import java.io.*; public class S4 { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); // Question 1. double cValue; for(int d=0;d<=90;d++) { cValue = MyTrig.cosD(d); System.out.println(d + " " + cValue); } // Question 2. (For implementation of sinD, see below.) System.out.println("\n sin(30 degrees) = " + MyTrig.sinD(30)); // Question 3. (For implementation of randomAngle see below.) for(int k=1;k<=3;k++) {System.out.println("A random angle = " + MyTrig.randomAngle());} // Question 4. System.out.println("\nQuestion 4 Output:\n"); double t = 20.1; int s=30; int m=0; int d=15; double z = MyTrig.convert(s,d,m); System.out.println(z + " " + t); z = MyTrig.convert(m,s,m); System.out.println(z + " " + t); in.waitUntilEnter(); } } public class MyTrig { // Constants for converting degrees to radians and vice versa. public static final double DEG2RAD = Math.PI/180; public static final double RAD2DEG = 180/Math.PI; // Yields the sine of x assuming x is given in degrees. public static double sinD(double x) { return cosD(90-x); } // Yields a random angle between 0 and 90 public static double randomAngle() { return 90*Math.random(); } // Displays the value of x, assumed to be in degrees public static void displayDeg(double x) { System.out.println("The angle is " + x + " degrees"); } // Yields the cosine of x assuming x given in degrees. public static double cosD(double x) { double y; y = Math.cos(DEG2RAD*x); return y; } // Yields the value of x (radians) to the nearest degree. public static int nearest(double x) { int a; a = (int) Math.round(RAD2DEG*x); return a; } // Yields the degree equivalent of d degrees + m minutes + s seconds. public static double convert(int d, int m, int s) { double z; z = d + (double)m/60 + (double)s/3600; return z; } // Yields the cosine of d degrees + m minutes + s seconds. public static double cosDMS(int d, int m, int s) { double t; t = cosD(convert(d,m,s)); return t; } }