CS99 |
Fundamental Programming Concepts
Summer 2001 |
|
Lab
5
Note: this lab is a little more challenging than previous labs. |
||||||||||||||||||||||||||||||||||||||||||||||||
Overview | ||||||||||||||||||||||||||||||||||||||||||||||||
The goals of this lab are to: ·
Use braces to modify an if-else
construct to produce different outputs ·
Trace the output of a combined while and if-else program segment ·
Modify the prime number algorithm from class; write a new algorithm
that determines if a number is perfect. |
||||||||||||||||||||||||||||||||||||||||||||||||
Part I: Dangling Else | ||||||||||||||||||||||||||||||||||||||||||||||||
Modify the following code to produce the output shown.
You may not make any changes other than inserting braces.
We have eliminated the indentation from the following code to make the
problem more challenging. Note: It
is possible that no modification is necessary and some may be impossible to do
with just braces! Once you get each part to work correctly, indent each part properly, and
cut and paste your modified code into a file called Lab5Part1.txt; label the
part to which it belongs. As usual,
put your name, ID, and date at the top of the file. if
( y == 8 ) a) If x = 5
and y = 8, the following output is produced:
@@@@@ b) If x = 5
and y = 8, the following output is produced @@@@@ c) If x = 5
and y = 8, the following output is produced
@@@@@ d) If x = 5
and y = 8, the following output is produced #####
|
||||||||||||||||||||||||||||||||||||||||||||||||
Part II: Tracing a program segment | ||||||||||||||||||||||||||||||||||||||||||||||||
Trace the code below. Fill
in the table below, using the format of one column per assignment, one entry
(for the variable assigned to) per column.
The first three assignments have been done for you.
The table is exactly the right size.
Remember that integer division truncates the decimal portions of
results, so that if int
x = 9
and int y
= 7,
x/y
returns 1. int
n = 9293;
You may do this portion by hand, but make sure you write your responses neatly; put your name and student ID, as well as the date, at the top. Call it Lab5Part2.txt (Yes, even if you’re doing it by hand). |
||||||||||||||||||||||||||||||||||||||||||||||||
Part III: Prime and Perfect Numbers | ||||||||||||||||||||||||||||||||||||||||||||||||
Part A: In class, we wrote a program segment that determined if a number
entered by the user was prime. That
segment you may recall was: TokenReader
in = new TokenReader( System.in ); Modify the code so that when the number is not prime, it
outputs the number’s smallest factor (other than 1) as well:
Enter a positive number: 102 Enter this
code into the main method of a program called Lab5Part3A.java. Once you
have it working correctly, print it. Write a program that
determines if a number is perfect. Save it in a file called
Lab5Part3B.java and print it after you have it working correctly.
|
||||||||||||||||||||||||||||||||||||||||||||||||
Part IV: Submitting Your Lab | ||||||||||||||||||||||||||||||||||||||||||||||||
Hand in printouts for
Don't forget to put your name and net ID at the top of each file. The lab is due Thursday, 12 July 2001, at the beginning of lab session. Hand it in to Siddarth. |