CS 1401 Assignment #11

Date Assigned: Wednesday, April 6, 2005

Due Date: Wednesday, April 13, 2005, before the beginning of your lab section.

Goals: to learn how to use switch statements and combined assignment operators, as well as review test plans, tracing, and loops.

Points: This assignment is worth a total of 20 points. No late assignments will be accepted, but credit will be given for partially completed assignments turned in on time.

Assignment:

Part 1.  Test Plan Review

In lab assignment #10, you wrote the code for an application which computed heat index values based upon input temperature and humidity.  In your application, valid input values were checked and exceptions were thrown if the input provided by the user was not capable of being converted to numeric values, or if the values were outside of ranges specified.  Write the test plan for the heat index application.   Place your test plan in a separate text file named testplan.txt, and submit it to your TA with the rest of this assignment. In your test plan, you should show at least 10 distinct test cases, the expected output for each case, and actual output produced by your code.  This kind of testing is called white box testing, because you are familiar with the code.  If you were testing an application without knowing the details of the implementation, that would be considered black box testing.

 

Part 2.  Nested While Loops and Tracing Program Execution

(a)  Write a short application that asks the user for an integer between 1 and 10 and then produces output similar to that displayed below.  Use the variables i and j for your loop counters. For example, if the user enters a 5, the following output would be displayed: 

1
1 2
1 2 3
1 2 3 4   
1 2 3 4 5

          

(b)  Trace the execution of your code by hand, line by line, for a user input of 5.  Show details of how the values of variables change with each iteration.    Place your tracing in a separate text file named trace.txt (an example may be found on p.239 in your textbook), and submit it to your TA with the rest of this assignment.

 

Part 3.  Switch Statement Using Combined Assignment Operator

Background Information:

In Java you can use a single operator such as + =  to combine arithmetic and assignment.  For example, to increment a variable k by 8 and store the result back in the variable, the statement could be written as follows:

k += 8 ;

The statement above is just a shorter, more compact way to write the following statement:

k = k + 8 ;

Refer to pp. 449-450 in your textbook for other examples of combined assignment operators.

 

Assignment:

Write an application which computes and displays the value of a person’s weight on one of the other planets in the solar system.  The user should be prompted to enter an integer weight in Earth pounds and some character or integer value corresponding to the planet of choice.  The application should display the name of the planet chosen and the converted weight for that planet.  The converted weight is found by multiplying the weight on Earth times the relative force of gravity on the other planet.  Use a switch statement to compute the converted weight based upon the planet specified.  Display an error message as your default option in the switch statement. Use the * =  combined assignment operator in your switch statement to multiply and assign the converted weight based upon the specified planet.  Use the Math.round(x) method to round the converted weight to the closest integer.  The relative forces of gravity are listed below.

If Earth is set as 1.00, the other planets have the following relative gravity :

Mercury           0.38

Venus               0.78

Mars                0.39                

Saturn              1.17    

Jupiter              2.65

Neptune           1.23

Uranus             1.05

Pluto                0.05

For example, if a person enters his/her weight as 100 pounds on Earth and selects Saturn as the specified planet, the application would compute the relative weight on Saturn and display the message: “On Saturn, you would weigh 117 pounds.”  If a person enters his/her weight as 159 pounds on Earth and selects Pluto as the specified planet, the application would compute the relative weight on Pluto and display the message: “On Pluto, you would weigh 8 pounds.”  (The weight would be computed as 7.95 and then rounded to 8).

Deliverables: as announced in the labs and explained in the handouts given to you in the labs.