V22.0002
Intro. to Computers & Programming
Summer 2009
Practice Final #2
  1. (10 points) What does the following code produce?
    public class WhatDoIDo {
    
          public static void main(String[] args) {
               print(8);
               print(3);
          }
    
          public static void print(int number) {
               int index, uScores, numToShow;
    
               numToShow = formula(number);
    
               for(index = 1; index <= number; index++) {
                     for(uScores = 1; uScores <= index; uScores ++)
                          System.out.print("_");
                     System.out.println(numToShow);
               }
          }
    
          public static int formula(int value) {
               return (value % 3) + 1;
          }
    }
    

    Answer:

    _3
    __3
    ___3
    ____3
    _____3
    ______3
    _______3
    ________3
    _1
    __1
    ___1
    
  2. (15 points) The following code contains at least 5 errors (syntactic and logical). Identify them (comments and formatting or lack thereof are not errors).
    public class MyMax {
    
          public static void main(String[] args) {
                int max = maximum(5, 7);
                System.out.println("Maximum is: " + maximum(5, 7, 3));
                System.out.println("Maximum is: ", + max);
          }
    
          public static int maximum (int x, int y, int z) {
                int max = x;
                if (y > max);
                      max = y;
    
                if (z > max)
                      max = z;
    
                return max;
          }
    
          public static int maximum (int x, y) {
                if (x > y)
                      return x;
          }
    

    Answer:

  3. (3 points each)
    1. Write a method header for a method that takes three integers as parameters and prints the largest of the three to the screen. (Only write the header)

      Answer:

      public static void printMax(int x, int y, int z)
      
    2. How many times will the following loop execute the System.out.print statement?
      a = 1;
      while (a < 4) {
          System.out.print (a);
          a = a + a%3;
      }
      

      Answer: twice

    3. Given int a = 53, b = 4;

      What will be the output of the following statement?

          System.out.println ( (a % b) + (a / b) );
      

      Answer:

      14
      
    4. What does void mean when seen as the return type in a method? (one sentence answer only)

      Answer: void means that the method does not return a value

    5. The following method header is part of the Math class. It returns the absolute value of its parameter. Write the line of code you would need to assign the absolute value of the variable number to a variable named absoluteValueOfNumber.
      static double abs(double a)
      

      Answer:

      absoluteValueOfNumber = Math.abs(number);
      
  4. (15 points) Complete the following method that places odd numbers found in the string s in consecutive elements of the array x.
    /* places odd numbers in consecutive elements of array */
    public static void assign(String s)
    {
         int len = __________;
    
         int[] x = new int[_________________];
    
         int index = 0;
         int digit;
    
         for(int j = 0; j < len; j++) {
            digit = s.charAt(j) - '0';//convert to int digit
    
            if( ______________________)//test for odd #
            {
                x[____________] = digit;
    
                ____________;
            }
         }
         System.out.println("The odd numbers are");
         for(int j = 0; j < index; j++)
         {
             System.out.println(x[j]);
         }
    }
    

    Answer:

    /* places odd numbers in consecutive elements of array */
    public static void assign(String s)
    {
         int len = s.length();
    
         int[] x = new int[len];
    
         int index = 0;
         int digit;
    
         for(int j = 0; j < len; j++) {
            digit = s.charAt(j) - '0';//convert to int digit
    
            if( digit%2 == 1 )//test for odd #
            {
                x[index] = digit;
                index++;
            }
         }
         System.out.println("The odd numbers are");
         for(int j = 0; j < index; j++)
         {
             System.out.println(x[j]);
         }
    }
    
  5. (20 points) Write a program that reads an integer n from the keyboard and then computes and displays the sum of the numbers from n to 2n if n is nonnegative, or the sum of the numbers from 2n to n if n is negative. The starting and ending points are included in the sum.

    Sample run:

       Enter number : 5
       Sum : 45
    

    Sample run:

       Enter number : -4
       Sum : -30
    

    Hint: You should have only 1 loop to compute the sum; use the sign of n to determine the start point and the end point.

    Answer:

    import javax.swing.JOptionPane;
    
    public class SumNTo2N
    {
        public static void main (String [] args)
        {
            int n, low, high, total = 0;
            String nAsString = JOptionPane.showInputDialog ("Enter number:");
            n = Integer.parseInt (nAsString);
            
            if (n < 0)
            {
                low = 2 * n;
                high = n;
            }
            else 
            {
                low = n;
                high = 2 * n;
            }
            
            for (int i = low; i <= high; i++ )
                total +=i;
                
            System.out.println("Sum: " + total );
            System.exit(0);
        }
    }
    
  6. (25 points) Write a method called CellFiller that takes an array of integers (call it cells) as a parameter and then:
    1. Reads in a positive integer k.
    2. If the element of cells with index k is set to −1, you will store k in that location.
    3. If the element of cells with index k is not set to −1, you will store k in the next available location of cells (i.e., at the next position that contains the value −1).

    So, if cells contains the following values: cells[3] = 3, cells[4] = 4, cells[5] = -1, cells[6] = 6 and cells[7] = -1. If the value read is 7 it will be stored in cells[7]; if the value read is 3, it will be stored in cells[5].

    You can assume that cells is infinitely large (i.e., you need not check the length of cells for out-of-bounds indices).

    Answer:

    import javax.swing.JOptionPane;
    
    public class CellFiller
    {
        public static void main (String [] args)
        {
            int cells[] = new int [100];
            
            for (int i = 0; i < 100; i++)
                cells [i] = -1;
            for (int i = 1; i <= 10; i++)
                cellFiller (cells);
            for (int i = 0; i < 100; i++)
                if (cells [i] != -1)
                    System.out.println ("cells[" + i + "] = " + cells[i]);
                    
            System.exit (0);
        }
        
    
        static void cellFiller(int cells[])
        {
            int k, index = 0;
            
            String input = JOptionPane.showInputDialog("Next number:");
            k = Integer.parseInt (input);
            index = k;
    
            while( cells[index] != -1 )
            {
                index++;
            }
            cells[index] = k;
        }
    }
    

This document was translated from LATEX by HEVEA.