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 )
   if ( x == 5 )
   System.out.println( "@@@@@" );
   else
   System.out.println( "#####" );
   System.out.println( "$$$$$" );
   System.out.println( "&&&&&" );

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;
   int x = 1000;
   int d = 0;
   while ( x > 0 ) {
      d = (n / x) % 10;
     
if ( d < 9 )
        
x = x/10;

      else
         n = n - d*x;
  
}

n: 9293                            
x:   1000                          
d:     0                        

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 );
   System.out.println(“Enter a positive number: “ );
   int n = in.readInt();
   int k = 2;
   boolean isPrime = true;
   while ( k < = n-1 && isPrime ) {
      if ( n%k == 0 )
         isPrime = false;
    k++;
   }
   if ( isPrime )
      System.out.println(n + “ is prime. “);
   else
     
System.out.println(n + “ is not prime.”);

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
  102 is not prime, and is divisible by 3.

Enter this code into the main method of a program called Lab5Part3A.java.  Once you have it working correctly, print it. 

Part B:

A number is "perfect" if it equals the sum of its divisors, including 1 but excluding itself.  For example, 6 is perfect because 6 = 1 + 2 + 3.  Similarly, 28 is perfect because 28 = 1 + 2 + 4 + 7 + 14.  However, 4 is not perfect because 4 is not equal to 1 + 2.

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

  • Lab5Part1.txt
  • Lab5Part2.txt (may be handwritten)
  • Lab5Part3A.java, Lab5Part3B.java

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.