CS 130
Beck                                                    Test 1
Fall 99
                                                                                               Name _______________________

Each problem is worth 15 points.  Spelling your name correctly is worth 25 points.

1.  Find 3 syntax errors in the following class.  (Syntax errors are any errors found by the compiler.)

    public class Summing {

        public static void main(String args[]); {
              int  num1;
              int  num2;
 
              num1 = 15;
              num2 = 20;
 
              sum = num1 + num2;
 
              if num1 > num2
                     System.out.print("The sum of + num1+ and "+num2+" is "+sum);
              else
             System.out.println( "The sum is " + num1);

        }
 
 

2.    Find 5 methods and 5 variables for the class Car that models a real automobile.
 
3.   2 methods are given below.  One works properly, the other does not.  Determine which one does not work
      correctly and modify the code so that it will work correctly.  Mark the correct method as correct.
 

a)   The following method receives 3 integers and should determine if all integers are distinct.  If the 3 integers
      are distinct, then a 1 should be returned.  If there are at least 2 of the integers that are the same, then a 0 should be returned.

      public static int compare( int  numb1,
             int  numb2,
             int  numb3) {
 
             int different;
 
            different = 0;
 
            if (numb1 != numb2){
                if (numb2 != numb3)
                    different = 1;
           }
          return (different);
     }

b)     The following method receives 2 integers.  If the 2 integers have the same sign, then a 1 is
        returned.  If the 2 integers have opposite signs, then a -1 is returned.  If at least one of the
        numbers is 0 then a 0 is returned.

  public static int sign( int  apple,
                                 int  pear) {
        int result;
 
        if ( apple*pear > 0)
              result = 1;
        else {
              if (apple * pear < 0)
                   result = -1;
              else
                   result = 0;
        }
        return(result);
    }

 4. Determine the output on the screen when the following program is executed.

public class OnScreen {

      public static void main(String args[]){

      int floor;
      int motion;
 
      floor = 1;
      motion = 0;
 
      showStatus(floor, motion);

      motion = up();
      showStatus(floor, motion);

      floor = newFloor(floor, motion);
      showStatus(floor, motion);

      motion = up();
      floor = newFloor(floor, motion);
      motion = stop();
      showStatus(floor, motion);

      }
 
      public static void showStatus(int flo, int mot){
     System.out.print("floor = " + flo);
 
    if (mot < 0)
            System.out.println("  direction = down");
    else {
             if (mot > 0)
                   System.out.println("  direction = up");
             else
                  System.out.println("  stopped");
                       }
      }
 
      public static int up() {
            return(1);
      }
 
      public static int stop() {
            return(0);
      }
 
      public static int newFloor( int fl, int mo) {
              int  newfl;
 
             newfl = fl;
             if (mo > 0)
                   newfl = fl + 1;
            else if (mo < 0)
                   newfl = fl - 1;
            return(newfl);
       }
}

5. Do one of the following.

        a) Write a method that receives 3 integers and returns the middle integer.

        b) Write an applet method that puts a large blue M on the screen.  The method is passed
             the graphics object.