Sequence and Calculations (mathops, angles)
CS 161 - Spring 2008 - Dr. Hutchens

Due Monday, February 4 at 10:00 pm

Goals
- to plan a program solution
- to obtain input from the user and use it in your program
- to practice with mathematical operations

Overview
For this lab, you will write two Java programs. One will emphasize the mathematical operations that we've discussed in class. It will read in two integer values and display the results of various mathematical operations using those two values. The second program will read in information about angles and add the angles.

Together these two programs count as one assignment.

Choose good variable names. Write comments you understand. Use appropriate indenting and blank lines. Include your name and the month at the top along with a short description of the problem. Remember that people read your programs. Make them readable.

Math Operations

Input Specification
Use a short prompt to ask the user for two integer values. Prompt for and read each value separately.

Output Specification
Print a short message echoing the input values. Print the result of applying the following math operations to the two input values: addition, multiplication, subtraction (both ways, a-b as well as b-a), division (both ways), and modulus (both ways). For each result, give a complete math equation showing the operands, operator, equal sign and result. All values and results should be integers. No real numbers here. Sections with other professors may have different requirements. We are using integers only.

 

Example Input and Output (user input in bold)

Enter first number: 7 
Enter second number: 3 
The integer math operations
for 7 and 3 are: 
7 + 3 = 10
7 * 3 = 21
7 - 3 = 4
3 - 7 = -4
7 / 3 = 2
3 / 7 = 0
7 % 3 = 1
3 % 7 = 3

Implementation Details
Make the output neat and consistent. You do not know how to check for an input being zero. Even if you personally know how to do that, do not include any checks for zero. Test your program to see what happens when one of the inputs is zero. Is that correct mathematically?

Approaching the Problem
You should develop your programs incrementally. Here are some possible steps:
- Read in one of the numbers and 'echo' it (display it on the screen) to be sure you read it correctly.
- Add statement to read in and echo the second value.
- Perform one of the operations and display the result.
- Write code for the remaining operations.

Delivery
When you are finished, submit your Java source program as the mathops lab.
 
 

Angles

This is essentially homework if you don't finish the mathops part in lab.

Input Specification for Angles
Write a Java program to read in two angle measurements (in degrees. minutes, and seconds). You should include short prompts telling the user what to do (see sample execution below). You may assume that the values are non-negative; that is, you don't have to test for negative values. You haven't learned how to do that yet. Your program will calculate the sum of the two angles and display this sum.

The input portion of the program should look like this (the values entered by the user are shown in bold):
  Enter the first angle (in degrees, minutes, and seconds): 74 29 13
  Enter the second angle (in degrees, minutes, and seconds): 105 8 16

Output Specification for Angles
The output will be of the form:
  The sum of the two angles is: 179 degrees, 37 minutes, and 29 seconds.

You need not worry about improper plurals in the output; '1 degrees' is fine.

Processing
The example above is very straightforward. You can simply sum the two values for degrees, minutes and seconds and obtain the correct result. This will not always be the case! There are 360 degrees (°) in a circle, 60 minutes (') in one degree and 60 (") seconds in one minute. So if a sum of the seconds results in more than 60, this value needs to be 'carried over' into the sum of the minutes. If the sum of the minutes is more than 60, this value needs to be carried over into the degrees. And if the sum of the degrees is more than 360, the result should be truncated (to be ONLY the amount over 360).

Here are a few examples:
   74° 29' 13"   +   105° 8' 16"   =   179° 37' 29"
   7° 14' 55"   +   5° 24' 55"   =  12° 39' 50"   (note how the seconds carry over into the minutes)
   20° 31' 19"   +   0° 31' 30"   =  21° 2' 49"   (note how the minutes carry over into the degrees)
   240° 30' 12"   +   240° 23' 13"   =  120° 53' 25"   (note how the degrees are calculated)
   122° 17' 48"   +   237° 42' 12"   =  0° 0' 0"   (everything carries)

Hint
You'll want to use the truncating effects of integer division to your advantage in this lab! You'll also want to use the modulus/remainder operation. Remember that 7 / 2 gives you 3 while 7 % 2 gives you 1.

Sample Execution for Angles
  Enter the first angle (in degrees, minutes, and seconds): 7 14 55
  Enter the second angle (in degrees, minutes, and seconds): 5 24 55
  The sum of the two angles is: 12 degrees, 39 minutes, and 50 seconds.

Approaching the Problem
You should develop your programs incrementally. Here are some possible steps:
- Write the input statement to read in the degrees, minutes and seconds
   and echo the values to the screen to be sure you've read them properly.
- Write the code to calculate the sum of the angles the 'simple' way (no carryover or truncation)
   and display the result to the screen.
- Add code to properly calculate the sum of the angles.

Delivery
When you are finished, submit your Java source program as the angles lab.