PreLab 10: More Loops    Name ___________________________________________

  1. Trace the execution of the following loop by showing the value of each variable at each iteration in the table next to the code.
          final int LIMIT = 15;                num          sum       
          int num = 1;                         ---          ---  
          int sum = 0; 
       
          do
          {
             num = num + 3;
             sum = sum + num;
          }
          while (num < LIMIT);
    
          System.out.println ("Last number is " +  num + " and sum is " + sum);
    
  2. What is printed by the above code?

     

     

  3. Write a for loop equivalent to the do loop above. Your loop should compute exactly the same sum and print exactly the same message at the end.

     

     

     

     

     

     

  4. Complete the following code to print the string read in backwards. Use a for loop.
          String str;
          int length;
          System.out.print ("Enter a string of characters: ");
          str = scan.nextLine();
    
          // Find the length of str
    
    
          ________________________________________________________________;
    
          // Write a for loop to print the string backwards
    
    
    
    
    
    
    
    
    
    
    
  5. The following nested loop prints a triangle of stars (see page 257 of your text - it shows the whole program and the output; page 254 describes what the program is doing).
          final int MAX_ROWS = 10;
          for (int row = 1; row <= MAX_ROWS; row++)
          {
             for (int star = 1; star <= row;  star++)
                System.out.print ("*");
     
             System.out.println();
         }
    
    1. Trace the following similar nested loop (two loops nested inside of one) filling in the table below of values of the variable AND show the pattern that would be printed.
            final int MAX_ROWS = 4;
            for (int row = 1; row <= MAX_ROWS; row++)
            {
               for (int dash = MAX_ROWS; dash > row; dash--)
                  System.out.print ("-");
      
               for (int star = 1; star <= row;  star++)
                  System.out.print ("*");
       
               System.out.println();
           }
      
            TRACE of Variables
         row      dash       star                  Pattern Printed
         ------------------------
      
      
      
      
      
      
      
      
      
      
      
      
      
        
      
      
      
      
      
      
      
    2. Modify the code to print the triangle in part (b) of Programming Project 5.13 on page 286 of the textbook. (Just write your modifications on the above code.)

       

       

       

  6. The following fragment of code reads in a sequence of numbers and finds the average of the positive values read in. (It assumes a Scanner object scan has already been instantitated.) Add the code needed to find and print the smallest number read in. You may assume the numbers will be in the range -1000 to +1000.
          int num;
          String response;
          int sumPositive = 0;
          int countPositive = 0;
    
    
    
    
          do
            {
              // Prompt for and read in the number
              System.out.print ("Enter a number: ");
              num = scan.nextInt();
    
              // Count and add the number if it is positive
              if (num > 0)
               {
                 countPositive++;
                 sumPositive += num;
               }
    
              // Update the smallest
    
    
    
    
    
    
    
    
    
               // Update the loop control (see if the user has more numbers)
               System.out.println ("Do you have another number?");
               scan.nextLine(); // read the new line still in the input stream
               response = scan.nextLine();  // read the response
            }
            while (response.equalsIgnoreCase ("y"));
    
            // Print results
            if (countPositive > 0)
              System.out.println ("The average of the positive values is " 
                        + (sumPositive/countPositive));
            else
              System.out.println ("There were no positive numbers entered.");