Monday July 3, 2000


An Example

In order to help you with your current programming assignment consider the following problem.

You are asked to create a class which will compute the values of the trigonometric functions for an angle.

Your class will have seven instance variables:
  1. angle - the value of the original angle
  2. sine - the value of the sine of the angle
  3. cosine - the value of the cosine of the angle
  4. tangent - the value of the tangent of the angle
  5. cotangent - the value of the cotangent of the angle
  6. secant - the value of the secant of the angle
  7. cosecant - the value of the cosecant of the angle
It will have six public instance methods:
  1. sin() - returns the sine of the angle
  2. cos() - returns the cosine of the angle
  3. tan() - returns the tangent of the angle
  4. cot() - returns the cotangent of the angle
  5. sec() - returns the secant of the angle
  6. csc() - returns the cosecant of the angle
It will have one private instance method:
  1. factorial(int n) - returns the factorial of an integer
It will have one class method:
  1. equiv(double angle) - returns the radian equivalent of the angle
When you are asked to design and write this class you immediately go to a Calculus book and find that if an angle is given in radian measure then to calculate the values of the sine and cosine of the angle you use the following series.
sin x = x - x3/3! + x5/5! - x7/7! + ...

cos x = 1 - x2/2! + x4/4! - x6/6! + ...
You decide that terms smaller than 0.00005 are insignificant.

The constructor of your class should accept as input the value of the angle measured in degrees. It will then make a call to the six public instance methods to give values to the six other instance variables.

Your class should have a toString method which produce an easy to read output giving the original angle, its radian equivalent, and the values of the six trigonometric fuctions of the angle.

You may assume you will not be given an angle for which one of the trigonmetric functions doesn't exist.







































import java.io.*;

public class Trig {
   double angle;
   double sine;
   double cosine;
   double tangent;
   double cotangent;
   double secant;
   double cosecant;

   public Trig(double angle) {
      this.angle = angle;
      sine = sin();
      cosine = cos();
      tangent = tan();
      cotangent = cot();
      secant = sec();
      cosecant = csc();
   }

   public static double equiv(double angle) {
      return(angle*3.1415926535/180);
   }

   private int factorial(int n) {
      int product = 1;

      for (int counter=2;counter <= n;counter++)
         product *= counter;

      return(product);
   }

   public double sin() {
      double sum = Trig.equiv(angle);

      int counter = 3;

      double nextTerm = Math.pow(Trig.equiv(angle),counter);
      nextTerm /= factorial(counter);

      while (nextTerm >= 0.00005) {
         if (counter % 4 == 1)
            sum += nextTerm;
         else
            sum -= nextTerm;
         counter += 2;
         nextTerm = Math.pow(Trig.equiv(angle),counter);
         nextTerm /= factorial(counter);
      }

      return(sum);
   }

   public double cos() {
      double sum = 1;

      int counter = 2;

      double nextTerm = Math.pow(Trig.equiv(angle),counter);
      nextTerm /= factorial(counter);

      while (nextTerm >= 0.00005) {
         if (counter % 4 == 0)
            sum += nextTerm;
         else
            sum -= nextTerm;
         counter += 2;
         nextTerm = Math.pow(Trig.equiv(angle),counter);
         nextTerm /= factorial(counter);
      }

      return(sum);
   }

   public double tan() {
      return(sin()/cos());
   }

   public double cot() {
      return(cos()/sin());
   }

   public double sec() {
      return(1/cos());
   }

   public double csc() {
      return(1/sin());
   }

   public String toString() {
      String outputLine = "\n";
      outputLine += "Original Angle:" + "\t\t" + angle + "\n";
      outputLine += "Radian Equivalent:" + "\t" + Trig.equiv(angle) + "\n";
      outputLine += "Sine:" + "\t\t\t" + sine + "\n";
      outputLine += "Cosine:" + "\t\t\t" + cosine + "\n";
      outputLine += "Tangent:" + "\t\t" + tangent + "\n";
      outputLine += "Cotangent:" + "\t\t" + cotangent + "\n";
      outputLine += "Secant:" + "\t\t\t" + secant + "\n";
      outputLine += "Cosecant:" + "\t\t" + cosecant + "\n";

      return(outputLine);
   }

   public static void main(String[] args) throws IOException {
      InputStreamReader stream = new InputStreamReader(System.in);
      BufferedReader keyboard = new BufferedReader(stream);

      System.out.println("Please enter your angle in degrees.");

      double angle = new Double(keyboard.readLine()).doubleValue();

      System.out.println("\n" + "Here is the information I've computed about your angle.");

      Trig myTrig = new Trig(angle);

      System.out.println(myTrig);
   }

}
Trig.java

Please enter your angle in degrees.
30

Here is the information I've computed about your angle.

Original Angle:		30.0
Radian Equivalent:	0.5235987755833333
Sine:			0.5000021325758316
Cosine:			0.866053883423225
Tangent:		0.5773337457936085
Cotangent:		1.7321003791756369
Secant:			1.1546625667762498
Cosecant:		1.9999914697330565

Please enter your angle in degrees.
45

Here is the information I've computed about your angle.

Original Angle:		45.0
Radian Equivalent:	0.785398163375
Sine:			0.7071430457634796
Cosine:			0.70710321483872
Tangent:		1.0000563297181002
Cotangent:		0.9999436734547581
Secant:			1.4142206951047247
Cosecant:		1.41414103693876

Please enter your angle in degrees.
60

Here is the information I've computed about your angle.

Original Angle:		60.0
Radian Equivalent:	1.0471975511666667
Sine:			0.8660212716414083
Cosine:			0.4999645653548418
Tangent:		1.7321653006083815
Cotangent:		0.5773121073657197
Secant:			2.000141748626257
Cosecant:		1.15470604792958

Please enter your angle in degrees.
75

Here is the information I've computed about your angle.

Original Angle:		75.0
Radian Equivalent:	1.3089969389583334
Sine:			0.9658952107869837
Cosine:			0.25882306306750547
Tangent:		3.731874583893097
Cotangent:		0.2679618453192493
Secant:			3.863643325089553
Cosecant:		1.0353089950463972

Please enter your angle in degrees.
15

Here is the information I've computed about your angle.

Original Angle:		15.0
Radian Equivalent:	0.26179938779166667
Sine:			0.25880881326643124
Cosine:			0.9659262729209173
Tangent:		0.26793847576358504
Cotangent:		3.7322000774623643
Secant:			1.0352757017117313
Cosecant:		3.8638560541234277