Second Summer 2002


Lab Assignment Two: A Simple Converter
You are to write a program that calculates the trigonometric functions sine and cosine of any angle expressed in sexagesimal degrees. There are 90 sexagesimal degrees in a right angle, and 360 sexagesimal degrees in a full circle. You are being assisted by a mathematician, for which you write this program, which knows these things, and many more.

You've read Chapter 2 (page 72) and looked up Appendix A2 (page 713) so you decide to use the Math class in the java.lang page. You therefore check the

java.lang.Math
class in the Java API and determine that there are two functions that implement what you need: but as the document says, they work on radians. Fortunately the mathematician gives you a formula for converting sexagesimal degrees (d ) into radians (r ), as follows:

That makes you quickly want to look up the value of

Math.PI
but once you find it, and other than that everything seems to be taken care of with this formula.

You are supposed to use this information to write a program that reads angles, expressed in integer values of sexagesimal degrees, converts them in radians, and calculates the sine and cosine of the original angles and reports them. Here's an example of how the program could behave. Everything that the computer prints is in color.

frilled.cs.indiana.edu%java Two
Hello, and welcome to the conversion program.
Please enter the number of degrees: 0
I will transform 0 degrees in radians.
Answer is: 0.0 radians.
The sine of that is: 0.0
The cosine of that is: 1.0
frilled.cs.indiana.edu%java Two
Hello, and welcome to the conversion program.
Please enter the number of degrees: 90
I will transform 90 degrees in radians.
Answer is: 1.5707963267948966 radians.
The sine of that is: 1.0
The cosine of that is: 6.123233995736766E-17
frilled.cs.indiana.edu%java Two
Hello, and welcome to the conversion program.
Please enter the number of degrees: 60
I will transform 60 degrees in radians.
Answer is: 1.0471975511965976 radians.
The sine of that is: 0.8660254037844386
The cosine of that is: 0.5000000000000001
frilled.cs.indiana.edu%java Two
Hello, and welcome to the conversion program.
Please enter the number of degrees: 30
I will transform 30 degrees in radians.
Answer is: 0.5235987755982988 radians.
The sine of that is: 0.49999999999999994
The cosine of that is: 0.8660254037844387
frilled.cs.indiana.edu%java Two
Hello, and welcome to the conversion program.
Please enter the number of degrees: -90
I will transform -90 degrees in radians.
Answer is: -1.5707963267948966 radians.
The sine of that is: -1.0
The cosine of that is: 6.123233995736766E-17
frilled.cs.indiana.edu%

Please note:

  1. There is a toRadians() method in class Math.
  2. You can use it to only to check your calculations.
  3. You need to make the conversion yourself, by implementing the formula.
  4. Write your program as described above, and have it ready next week.
  5. Also review the first set of warmups and their solutions and expect questions from them.
In a subsequent installment we are going to look at the programming exercises (not just the warmups) too. But for now your focus should be on writing the program and reviewing the warmups.

And in case you are reading this offline, here's your friend the Mathematician. He likes Fractions, and is pouring in some tea (he doesn't know Java, that's why he had to ask for your help). But he knows tea fairly well.


Last updated: Jun 16, 2002 by Adrian German for A201