Java Expressions and Control Structures

Note:  If you do not complete the lab this week you may finish it between lab meetings and demonstrate it to your TA at the beginning of next week’s lab.

1. Introduction

Now that you have reviewed expressions and control structures, it is a good idea to practice using them. In lecture we looked at while loops, for loops and (briefly) do loops in Java. We also looked at if statements and switch statements. Furthermore, we looked at Java’s Scanner class to allow simple input from the console. In this lab your TA will give you a short programming problem for you to solve using Java during the lab session.

2. Background

In mathematics, the sine is a trigonometric function of an angle. The sine of an acute angle is defined in the context of a right triangle: for the specified angle, it is the ratio of the length of the side that is opposite that angle to the length of the longest side of the triangle (the hypotenuse).

The sine function is commonly used to model periodic phenomena such as sound and light waves, the position and velocity of harmonic oscillators, sunlight intensity and day length, and average temperature variations throughout the year.

 

graph of sine wave, from -2pi to +4pi

 

The sine of an angle is easily obtained by using a hand-held calculator. Example is sine(30o) = 0.5. Some times instead of “degrees”, the sine requires “radians”. The relationship between radians and degrees is:

radians = π / 180 * degrees

for 30o we would have 0.5236 radians, and for sine functions that  require radians as input argument

sine(0.5236) = 0.5.

So pay attention on what kind of sine function you are using so you can either provide degrees or radians as input.

Your calculator computes sine function based on the following formula, where x is the angle in radians:

sin(x) = [sum (k=0..inf)] (-1)k x2k+1 / (2k+1)!           (scary hum?)        equation(1)

However the above formula can be rewritten in a nicer format:

sin(x) = x – (1/3!)x3 + (1/5!)x5 – (1/7!)x7 + …          equation(2)

where each subsequent fraction gives a smaller value, and therefore affecting the final precision of the number.  As an example, the table below shows how precise the sin(90o) is for the number of terms used in the above equation:

 

Number  of terms(*) sine(90) difference from the
actual value of 1.0
3 1.0045248555348174  0.004524855534817407
6 0.999999943741051 0.000000056258949054
9 1.0000000000000437 0.0000000000000437
10 1.0000000000000000 < 10-14

 

(*) I am considering the first term “x” is always there, so “2” in the first column of the above table would represent the equation (2) as shown below

sin(x) = x – (1/3!)x3 + (1/5!)x5

Note: 3! in the above equation means the factorial of 3, that is the product of 3*2*1 = 6. Similarly 5! is the value of 5*4*3*2*1.

3. Program

For this lab, you will apply all the knowledge that you have learned so far to develop a program that does the following tasks:

  1. Requests the number of terms in equation (2) to be used in the calculation (requests just once for all angles provided later on).
  2. Requests the precision number to be displayed in the terminal window, such as for a precision number of 2, sin(45) = 0.71, while for a precision number of 4 sin(45) = 0.7071 or 8 digits after the period (our second Question of the Day, remember?).
  3. Requests the angle in degrees. If the user provides -1 here, your program shall finish.
  4. compute the sine value of the given angle.
  5. show the sine value in the terminal window with as many digits after the decimal point as requested by item (b) above (note: it can be less digits than the requested, but never more than what has been requested).

For more details on what is expected from your program, see video #04 on the lab tutorial videos page.

4. General Ideas About Creating Your Program

This program can be done with about 20 lines of code. The best way of doing it is to focus in one task at a time: implement it, run the code, and if it is correctly implemented go ahead and implement other part of this code.

4.1 Scanner

Create a Scanner object and get the first questions in your program. Run it and see if they are working fine.

Enter number of terms in series: 5
What precision to display: 4
Enter the angle in degrees (-1 to exit): 90

4.2 Factorial Calculation

Factorials can be easily computed by using a FOR loop.

4.3 Radians to Degrees

Don’t forget to convert the input given in Degrees to Radians, that is the angle unit that equation (2) uses!

4.4 Keep asking until user enters -1

Since you do not know how many angles the user wants to compute their sine values, the best way is to use a WHILE loop.

4.5 Formatting the final value

Use what we have discussed in class and in the Question of the Day #2 about number formatting to format the sine(x) up to the maximum number of digits requested by the user.

4.6 Number of terms used from equation (2)

You can use a FOR loop to keep adding new terms of equation (2), until the numbers of terms specified by the user. Inside this loop you will place the other FOR loop to compute the factorial value and other multiplications.

5 Test your program

Follow the input as shown below and if you get the same results it means that your code is working as expected. Congratulations. If not, then debug it. I am placing some DEBUG information on the printout below. These lines would not be in the final program that you would deliver to your customer, but they help you during development time.

Enter number of terms in series: 4
What precision to display: 3

Enter the angle in degrees (-1 to exit): 25
sin(25.0) = 0.4363323129985824 – 0.01384525243283278 + 1.3179703351203658E-4 -5.974355994639725E-7

Sine(25.0) = 0.42261826016366216
Sine(25.0) = 0.423

Enter the angle in degrees (-1 to exit): 45
sin(45.0) = 0.7853981633974483 – 0.08074551218828077 + 0.00249039457019272 – 3.657620418217724E-5

Sine(45.0) = 0.70710678118654752
Sine(45.0) = 0.707

Enter the angle in degrees (-1 to exit): 60
sin(60.0) = 1.0471975511965976 -0.19139676963148028 + 0.010494502221717465 -2.740121304621818E-4

Sine(60.0) = 0.8660212716563727
Sine(60.0) = 0.866
Enter the angle in degrees (-1 to exit): -1

6 Improving your program

Your program shall now compute all the angles in a given range, such as -180 to 180. Your program shall accept two additional inputs: initial angle (such as -180 degrees) and final angle (such as 180 degrees). 

It shall compute the sine of 20 equally spaced angles inside that range (hint: the update portion of a FOR loop can take care of this feature) and for each angle it shall print a line such as

angle, sin(angle)

After running your program, you should have a table-like output of angles and sines separated by commas. Import this into MS Excel and plot this table. You should see a sine wave plot!

7 Grading

Once you have completed the program, you should demonstrate your program for your Lab TA. There will be 4 raw points for this lab, broken down in the following way:

  • Show that your program correctly calculates the sine of few values (2 points).
  • Show that it uses the precision the user has asked (1 point).
  • Show that your program exits when select -1 (1 point).

The 4 raw points will be normalized to give this lab equal weight to the other labs in the CS 401 course.

8 Notes and Hints

  • If you need help as you are working on the program, ask your TA.
  • If you do not finish this program during your lab session, you will be given the opportunity to demonstrate it to your TA at the beginning of next week’s lab. However, you will not be allowed to work on this lab during your next lab session, so if you do not finish it during this session, be sure to finish it during the week between sessions. Seek help from your TA or course instructor if necessary.