CPSC 120 Pre-Lab Assignment: Lab #2

  1. Trace the execution of the following program and show the output (write it as close to the way the computer would as possible even if you think it is wrong!):
    
    public class Silly
    {
       /** 
        * Yikes!  No Comments to help!  
        */
       public static void main (String[] args)
       {
          int sideLength = 5;
          int area = sideLength * sideLength;
          int perimeter = 4 * sideLength;
    
          System.out.println ("A square with side of length " + sideLength
               + " has area " + area);
          System.out.println ("The perimeter is " + perimeter);
    
          sideLength = sideLength + 7;
          area = sideLength * sideLength;
          System.out.println ("A square with side of length " + sideLength
               + " has area " + area);
          System.out.println ("The perimeter is " + perimeter);
        }
    }
    
     
    OUTPUT:
    
    
    
    
    
    
    
    
    
    
    
    
    
  2. What value is contained in the integer variable size after the following sequence of statements is executed?
         size = 43;
         size = size + 7;
         size = size * 2;
         size = size / 4;
    
    
    
    
    
    
    
    
    
  3. Write Java code to do each of the following:
    1. Set up storage for (that is, declare) an integer variable count without giving it an initial value.


    2. Set up storage for an integer variable count and give it an initial value of 0.


    3. Set count to 5, assuming that it has already been declared as an integer variable.


    4. Set count to one more than its current value, assuming that it has already been declared as an integer variable and currently has a value. (Write a general statement that will add 1 to the count variable no matter what the current value is - don't just set the variable to 6!)


  4. Given the declarations below, find the result of each expression. int a = 3, b = 10, c = 7; double w = 12.9, y = 3.2;
    1. a + b * c

       

    2. a - b - c

       

    3. a / b

       

    4. b / a

       

    5. a - b / c

       

    6. w / y

       

    7. a + w / b

       

    8. a % b / y

       

    9. b % a

       

    10. w % y

       

    11. (double) a / b

       

    12. b / 100

       

    13. b / 100.

       

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  5. The following program reads three integers and prints the average. Fill in the blanks so that it will work correctly.
     /**
      *  Read three integers from the user and print their average
      *  @author jdoe
      *
      */
    
    import __________________________________________;
    
    public class Average
    {
      public static void main(String[] args)
      {
        int val1, val2, val3;
        double average;
    
        // Create a Scanner object named scan
    
        __________________________________________________  ;
    
        // get three values from user
        System.out.println("Please enter three integers and " +
                           "I will compute their average");
    
        __________________________________________________  ;
    
        __________________________________________________  ;
    
        __________________________________________________  ;
    
        //compute the average and store in the variable average
    
        __________________________________________________  ;
    
        //print the average with appropriate label (not just the number!)
    
        ______________________________________________________ ;
      }
    }