CS56—Midterm Exam 1
E01, W11, Phill Conrad, UC Santa Barbara
01/31/2011

Name: ___________________________________________________


Umail Address: ______________________________@ umail.ucsb.edu



Circle one:       5pm           6pm     



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 50% of the entire exam grade.

    Write a complete Java class to represent a Coin. For full credit, your class should have all of the following:
    1. (6 pts) Correct syntax and structure of a Java class
    2. (6 pts) Two private instance variables, one for the name of the coin (e.g. "quarter" or "nickel") and another which represents the value of the coin in cents (as an integer, e.g. 25 or 5).
    3. (6 pts) A default constructor that creates a Coin object that represents a penny (value 1 cent).
    4. (6 pts) A two-argument constructor that takes arguments for name of coin and value
    5. (6 pts) getter methods for name and value. Do not create setters, only getters.
    6. (8 pts) the method that is implicitly invoked by System.out.println("c = " + c); when c is of type Coin.

      For that method, the string value of a quarter should be returned as "25 cents", a nickel should return "5 cents", and a penny should return "1 cent".

      (Don't restrict yourself to legal US denominations—for coins with value 2 or 20 or 42, this method should return the result "2 cents", "20 cents" or "42 cents". A dollar coin should return "100 cents". Only the value "1 cent" should be treated as a special case (no s.)
    7. A main method that
      • (4 pts) has the correct Java syntax for a main method
      • (2 pts) creates a local variable representing a dime (using the two argument constructor)
      • (2 pts) creates a local variable representing a penny (using the default constructor)
      • (2 pts) uses the method defined in (f) to print out the string representation of the dime object.
      • (2 pts) uses the methods defined in (e) to print out the value of the name and denomination of the penny object, with appropriate labelling of each field.



    See answer

    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. (30 pts) Assume that we have a class Book with attributes title and year (representing copyright year). We have a two-argument constructor that takes title and year, and default constructor that gives title the value "" (empty string) and year the value 0.

    Assume that we have overridden the equals() method as follows:

    public boolean equals(Object o)
    {
       if (! o instanceof Book) { return false;}
       Book b = (Book) o;
       return (b.title.equals(this.title)) && (b.year == this.year);
    }  
    

    Assume then that we have the following code inside a main method. The spot marked _____ is a blank where we will fill in an expression.

    Book t = new Book();
    Book u = new Book();
    Book v = u;
    Book w = new Book("Moby Dick",1851); 
    Book x = new Book("Moby Dick",1851); 
    Book y = w;
    System.out.println("result =" + ________ ); // we will fill in the blank!
    

    As we demonstrated in class, if we fill in the blank with a boolean expression, when that expression is concatenated with a String, Java will automatically convert it to either the String literal "true" or "false".

    A table of boolean expressions appears below, each one involving the Book objects t,u,v,w,x, and y we've constructed above. For each one, indicate if the boolean expression would evaluate to true or false (i.e. whether true or false would be printed after result =)

    t == u Circle one:                          true                     false               
    t.equals(u) Circle one:                          true                     false               
    t.equals(v) Circle one:                          true                     false               
    u.equals(v) Circle one:                          true                     false               
    w.equals(w) Circle one:                          true                     false               
    y == w Circle one:                          true                     false               
    w.equals(y) Circle one:                          true                     false               
    w.equals(x) Circle one:                          true                     false               
    y == x Circle one:                          true                     false               
    y.equals(u) Circle one:                          true                     false               
  3. (2 pts) Write two lines of Java code that declare two variables x and y, each of which has a primitive type.

     

  4. (2 pts) Write two lines of Java code that declare two variables a and b, each of which ends up being a reference.

     

    See answer

  5. (3 pts) Briefly explain the purpose of the Ant utility
    (Optional: If it helps, you can make reference to a similar utility that is used for C/C++ programming.

     

    See answer

  6. (3 pts) Briefly distinguish between targets and tasks in Ant.

    (Optional: If it helps, you can write your answer by comparing Ant to a similar utility that is used for C/C++ programming, drawing a comparison between targets and tasks in Ant, and the analogous concepts in that utility.)

     

    Questions 7-10 are on the back page—dont forget them!

    See answer

  7. (3 pts) Briefly explain: what is the role of JUnit in Java software development?

     

    See answer

  8. (3 pts) What special challenge arises in Java when using the formal "test-driven development" methodology on a method of the primitive type boolean?

     

    See answer

  9. (2 pts) In Java software development, what is the purpose of a JAR file?

    See answer

  10.  

  11. (2 pts) What is the role of a "manifest" in a JAR file?

     

    See answer


End of Exam

Total points: ?