Key Concepts
The purpose of this lab is to familiarize you with
arithmetic ex pressions in Java .
1. Primitive Data Types int and double
2. Ope rations involving primitive data types, especially integer division
3. Constant declaration and usage
4. Variable declaration and usage
5. Valid identifiers
Exercises
1. Complete the Prelab exercises for Wednesday’s class
meeting. You will be asked questions from it
for points.
Create the Lab exercises in the directory lab3 under your
user account (not in any other directory).
This can be d one at the time you create each project.
2. (Exer. 3, Pg 91) Design and implement a Java console
program, Savings, that reads in two numbers :
an account balance and an annual interest rate expressed as a percentage.
Display the balance
at the end of one and two years, assuming no deposits or with drawals — just the
interest payment.
Note that the interest should be compounded annually , i.e., added to the bank
balance at the end
of each year (and then used for the beginning balance the following year). Use
print to display
multiple lines of output in a single dialog box, followed by a println. The
formatting character
’\n’ can be used to force a new line of output.
3. (Exer. 4, Pg 92) Design and implement a Java dialog
program, CircleArea, which asks the user
for the radius of a circle, then computes the area of that circle using the
formula:

There is no ” raise to a power ” operator in Java, so you
will need to explicitly square the radius.
Use 3.14159 as the value for π, created as a constant.
4. Design and implement a Java console program,
ElapsedTime, that will compute the elapsed time,
in hours, minutes, and seconds between a starting time and a finishing time
(also given in hours,
minutes, and seconds) that have been entered by the user.
Keep in mind the following guidelines:
• The overall form of the program should be modeled on the
lab programs. Be sure to include
your name(s) and the purpose of your program in the opening comments, and use
line
comments and appropriate constants.
• Enter times using a 24- hour system .
• Prompt the user separately for the hours, minutes, and seconds for each of the
times you need.
• You may assume that the finishing time is later than the starting time (but
within the same
day).
• Your program should convert both times into seconds, subtract , and then
display the difference
in the correct format. Declare constant int objects which are used to assist
with the time
conversions. For example, SecondsPerMinute, MinutesPerHour, and SecondsPerHour
would
be three useful constants.
• A sample of what your program should produce is shown below.
Calculating the difference between two times
____________________________________________
Enter the initial time:
Hour: 6
Minute: 48
Second: 23
Enter the final time:
Hour: 19
Minute: 31
Second: 52
The elapsed time is 12 hours, 43 minutes, and 29 seconds.
5. Design and implement a Java dialog program,
SpiralCoords, which calculates the coordinates of a
spiral based on the initial length and scaling percentage entered by the user.
Even though external
documentation isn’t required for this project, creating at least a few test
cases would be beneficial
to helping you solve this problem and ensure your program is correct.
The diagram below shows a spiral. Each “leg” of the spiral
gets shorter by a fixed percentage as
you move toward the center. In the example shown, the length of BC is 90% of AB,
the length of
CD is 90% of BC, and so on.

The point A appears at the origin of an arbitrarily large
window. Given the length of the initial
side, AB, and the scaling percentage, we wish to determine the coordinates of
points B through F.
Your Java program should:
• prompt for and obtain the length of the initial side
• prompt for and obtain the scaling percentage
• calculate and display the coordinates of the six points (use print rather than
println, so all
coordinates show up neatly in one dialog box)
6. When you have completed all exercises:
(a) Submit an electronic copy of your work for this lab.
To do this, open a terminal window and
give the commands:
cd
2170submit lab3 sectionX
where X is either 1 (9:00), 2 (11:00), or 3 (3:00),
depending on your section.
(b) Publish all of these programs to your web page, listed
under Lab 3 as Savings, CircleArea,
ElapsedTime, and SpiralCoords.
(c) Complete the Postlab worksheet.
(d) Staple all the program printouts (in order assigned ) to the Postlab
worksheet, and hand them
in at the beginning of Lab 4.
1. Complete the test suite for the external documentation
for the Savings program. Select amounts
and interest rates that will test the correctness of your program. Determine the
correct answers
using a calculator.
Problem Statement: Given a dollar amount and annual
interest rate from the user, determine the
balance of the account at the end of one and two years, assuming annual
compounding of interest.
Specifications:
(a) INPUT: Amount — double, non– negative ; InterestRate —
double, non–negative
(b) OUTPUT: Balance 1 — double, non–negative; Balance 2 —
double, non–negative
Test Suite:
| Amount |
Interest Rate |
Year 1 |
Year 2 |
| 6000 |
4.25 |
6255.0 |
6520.8375 |
| 10000 |
6.75 |
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
General Algorithm:
• Prompt for and get data
• Calculate and display balance for first year
• Calculate and display balance for second year
Detailed Algorithm:
• Prompt for and get data
– Prompt for and get Amount
– Prompt for and get Interest Rate
– Change Interest Rate into decimal equivalent
• Calculate and display balance for first year
– Determine Interest for first year, add to Amount
– Display balance after first year
• Calculate and display balance for second year
– Determine Interest for second year (based on updated value), add to Amount
– Display balance after second year
2. Complete the external documentation for the CircleArea
program by filling in the Problem Statement,
Test Suite table, and Detailed Algorithm. Select various radii to test the
correctness of your
program. Use a calculator to determine the areas in the test suite.
(a) Problem Statement:
(b) Specifications:
i. INPUT: Radius — double, non–negative
ii. OUTPUT: Area — double, non–negative
(c) Test Suite:
| Radius |
Area |
| 10 |
314.159265 |
| |
|
| |
|
| |
|
| |
|
| |
|
(d) General Algorithm:
• Prompt for and get Radius
• Calculate Area
• Display Area
(e) Detailed Algorithm: (Since this is a very
simple program , just give the Java statements you
plan to use.)
• Prompt for and get Radius
• Calculate Area
• Display Area
Staple printouts for all projects from Lab 3 to this sheet
and hand in at the beginning of Lab 4. You may
need to consult the textbook in order to answer the following:
1. What are the two attributes that define a data type?
(a)
(b)
2. Circle each of the following which are legal constants
in Java. For the ones that are legal, indicate
whether they are integers (i) or floating–point (f ) constants on the line
provided.

3. Rewrite the following floating–point constants in
Java’s scientific notation form:

4. Circle each of the following which represent legal Java
variable names:
| a) x |
e) short |
i) 12MonthTotal |
| b) formula1 |
f) tiny |
j) margin-cost |
| c) average_rainfall |
g) total output |
k) b4hand |
| d) %correct |
h) aReasonablyLongName |
l) _stk_depth |
5. Indicate the value and type (i or f ) of each of the
following expressions :

6. By applying the appropriate precedence rules, evaluate
the following expressions:

7. Complete the following table. The object MyNumber is of
type int.

8. Suppose you are putting empty beverage bottles into
six-packs in order to return them to the local
recycling center. If you have MyNumber bottles to recycle, what does the
quantity (MyNumber +
5)/6 represent? Write your answer as a simple English statement involving the
number of bottles
and the number of six-pack containers involved.
9. Using a calculator, determine the number of hours,
minutes, and seconds we can obtain from the
total seconds given:
| Total Seconds |
Hours |
Minutes |
Seconds |
| 302 |
0 |
5 |
2 |
| 11002 |
|
|
|
| 72 |
|
|
|
| 60 |
|
|
|
| 61 |
|
|
|
| 7380 |
|
|
|
| 37394 |
|
|
|