CS 100: Lecture L7
February 16
// Lecture 7
import java.io.*;
public class L7
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
double g = 30.1;
MyTrig.displayDeg(g);
MyTrig.displayDeg(2*g+10);
double theta = 60;
double c = MyTrig.cosD(theta);
System.out.println("cos(60) = " + c);
c = MyTrig.cosD(2*theta-30);
System.out.println("cos(2*60-30) = " + c);
double rads = MyTrig.DEG2RAD*90;
System.out.println("90 degrees = " + rads + " radians");
double a = Math.PI/6;
int b = MyTrig.nearest(a);
System.out.println("Nearest degree to pi/6 = " + b);
double latitude = MyTrig.convert(42,12,39);
double dist = 3950*latitude*MyTrig.DEG2RAD;
System.out.println("Our distance to equator = " + dist + " miles");
double dist2Axis = 3950*MyTrig.cosDMS(42,12,39);
System.out.println("Distance to axis = " + dist2Axis + " miles");
in.waitUntilEnter();
}
}
/*
The angle is 30.1 degrees
The angle is 70.2 degrees
cos(60) = 0.5000000000000001
cos(2*60-30) = 6.123031769111886E-17
90 degrees = 1.5707963267948966 radians
Nearest degree to pi/6 = 30
Our distance to equator = 2910.036185625097 miles
Distance to axis = 2925.676424829559 miles
*/
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;
// 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;
}
}