CS56—Midterm Exam 1
E01, S11, Phill Conrad, UC Santa Barbara
04/28/2011

Name: ___________________________________________________


Umail Address: ______________________________@ umail.ucsb.edu



Circle one:       6:30pm           7:30pm     



Please write your name only on this page.
That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 75 minutes to complete the exam.

A hint for allocating your time—on your first pass through the exam:

If you do that, after you complete your first pass through the exam in 50 minutes, you'll still have 25 minutes to:


  1. All the part of this question, taken together, are worth 60% of the entire exam grade. (See solution)

    Write a complete Java class to represent a RadioStation. A radio station in the US has "call letters" (a code for the station of 3 or 4 letters), anda frequency (a decimal number between 87.8 to 108.0 for FM stations, and a number between 520.0 and 1610.0 for AM stations.)

    For full credit, your class should have all of the following:
    1. (5 pts) Correct syntax and structure of a Java class
    2. (10 pts) Two private instance variables, one for the call letters of the radio station (e.g. KCSB, KTYD, KDB, KTMS) and another for the frequency (e.g. 91.9, 99.9, 93.7, 990.0)
    3. (5 pts) A default constructor that creates a RadioStation object that represents station KCSB, frequency 91.9.
    4. (5 pts) A two-argument constructor that takes arguments for call sign and frequency.
    5. (5 pts) getter methods for call sign and frequency called getCallSign() and getFrequency(). Do not create setters, only getters.
    6. (5 pts) a method isFM() that returns a true or false value. It should return true when frequency is less than or equal to 108.0—this indicates an FM station. If isFM() returns false, the station is assumed to be an AM station.
    7. (5 pts) the method that is implicitly invoked by System.out.println("rs = " + rs); when rs is of type RadioStation.

      For the object created by the default constructor, the result of System.out.println("rs = " + rs); should be:
      rs = [KCSB, FM 91.9].

      For an object created with new RadioStation("KTMS",990), the result should be: rs=[KTYD, AM 990], or rs=[KTYD, AM 990.0] (Don't worry about whether the .0 prints or not—I'll accept your answer either way.)

    8. A main method that
      • (5 pts) has the correct Java syntax for a main method
      • (5 pts) creates a local variable representing station KJEE on frequency 92.9
      • (5 pts) creates a local variable representing KCSB (using the default constructor)
      • (5 pts) uses the method defined in (g) to print out the string representation of the object representing KJEE object.

    There is more room on the next page for your answer if you run out of room here.

    Extra space for your answer to question 1

  2. Assume that we have the RadioStation class that you defined in question 1, with ONLY the constructors and methods that were required for that problem.

    What would the output of the following main program be? In addition to the output, provide a brief explanation.

    public class E01q2 {

    public static void main(String [] args) {
    RadioStation a = new RadioStation();
    RadioStation b = new RadioStation();
    RadioStation c = a;
    System.out.println("red");
    if (a == b)
    System.out.println("orange");
    if (a.equals(b))
    System.out.println("yellow");
    if (a == c)
    System.out.println("green");
    if (a.equals(c))
    System.out.println("blue");

    }
    }

    Output (5 pts):

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    Explanation (5 pts):

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  3. Now, assume, just like in question 2 that we have the RadioStation class, but that this time we have added one additional methods as shown below.

      public boolean  equals(Object o) {
         double tol = 0.01; 
    if (! (o instanceof RadioStation) ) return false; RadioStation other = (RadioStation) o; return (other.getCallSign().equals(this.getCallSign()) &
    Math.abs(other.getFrequency() - this.getFrequency()) < tol ); }

    Here is exactly the same program as question 2. Now, what would the output be?
    In addition to the output, provide a brief explanation, and answer the additional question.
    public class E01q2 {

    public static void main(String [] args) {
    RadioStation a = new RadioStation();
    RadioStation b = new RadioStation();
    RadioStation c = a;
    System.out.println("red");
    if (a == b)
    System.out.println("orange");
    if (a.equals(b))
    System.out.println("yellow");
    if (a == c)
    System.out.println("green");
    if (a.equals(c))
    System.out.println("blue");

    }
    }

    Output (5 pts):

    
    
    
    
    
    
    
    
    
    
    
    
    
    

    Explanation (5 pts):

    
    
    
    
    
    
    
    
    
    
    
    

    Additional Question (5 pts): In the equals method shown above, there is a single & used to indicate boolean and instead of a double && like you may be used to. What is the effect, in this program of using & vs. && ? Explain briefly:

    
    
    
    
    
    
    
    
    
    

    There are more questions on the back page—dont forget them!

  4. (5 pts) Mary and Fred are arguing whether javadoc is a "target" or a "task" in Ant. You want to keep the peace between Mary and Fred, because you share a crowded apartment with them on Del Playa, and when they aren't happy, no-one in the house is happy.

    What can you say to them to settle the argument, but allow both of them to "save face"?



     


     

  5. (2 pts) Multiple Choice: Assume that Foobarable is an interface. Which line of code is correct?

    Circle one:          a                b

    1. public class Geegaw extends Foobarable { ...

    2. public class Geegaw implements Foobarable { ...

  6. (2 pts) Is the following statement true or false?

    "One difference between an abstract class and an interface is that while an abstract class cannot be instantiated, an interface can be instantiated."

    Circle one:          True             False
     

    For the remaining questions, you only need to write individual fragments of java code.
    Don't worry about putting your code inside a method or a class.

  7. (2 pts) Write a line of Java code that declares a variable p that is NOT of a reference type.



     

  8. (2 pts) Write a line of Java code that declares a variable q that is NOT of a primitive type.



     

  9. (2 pts) Suppose Dog is a class that represents a Dog. Declare a variable called kennel that can store more than one reference to a Dog object. Choose a type for kennel so that it can automatically grow as needed if the number of Dog objects grows arbitrarily large.

    If your answer would require an "import" statement at the top of the file, write the import statement in the space indicated.

    // import statement




    // line of code the question is asking for






End of Exam

Total points: ?