CS10 Midterm Exam 2
E02, 09S, Phill Conrad, UC Santa Barbara
05/20/2009

Name: ________________________________________________________


Umail Address: __________________________________@ umail.ucsb.edu


Circle Lab section:        4PM             10AM           11AM             noon

 


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 50 minutes to complete the exam.

A hint for allocating your time:


  1. (10 pts) A friend of yours works at a software company, and has gotten you a job interview there. The job involves Java programming.

    Your interview is with Mary Pranks, who is lead developer. She wants to test your knowledge of Java, and so she asks you this question:

    What is the difference between these two lines of Java code?

    int x = 5;      // line 1
    Integer y = 5;  // line 2


    Your friend has warned you that Mary dislikes long-winded explanations—she wants a short answer, but she also wants an answer that reflects deep understanding.

    How do you answer Mary's question?




  2. (20 pts) Here is some code for two classes, and a Java interface.

    Add code to the two classes, so that each of them implements the Java interface shown.


    Note: Througout this entire exam, the notation ... indicates where other parts of the code listing have been left out for sake of space.
    The ... is not part of the Java code.
    interface ShoppingCartItem {

    DollarAmt getPrice();
    double getShipWeight();

    }

    class Book
    {
    private String title;
    private String isbn; // a code number for books private int numPages;
    private DollarAmt price;
    private double shippingWeightInOunces;

    public Book(String title, String isbn, int numPages, DollarAmt price,
    double shippingWeightInOunces)
    { ... }
    public String getTitle() { return this.title ;} public String getISBN() { return this.isbn; } public int getNumPages() { return this.numPages; }



















    }

    class ClothingItem { private String color; private String size; private int itemNumber; private DollarAmt price; private double shippingWeightInOunces;
    public ClothingItem(String color, String size, int itemNumber, DollarAmt price, double shippingWeightInOunces) { ... }
    public String getColor() { return color; } public String getSize() { return size; } public int getItemNumber() { return itemNumber; } public double getShippingWeightInOunces() { return shippingWeightInOunces; }



















    }
  3. (20 pts) For each of the for loops below, indicate what the output will be.

    Be sure to consider these special cases:
  4. code
    write the output here
    for (int i=0; i<4; i++)
      System.out.println(i);
    
        



                                                     
    for (int i=4; i>1; i--)
       System.out.println(i);
    
             



                                                
    for (int i=1; i<3; i--)
       System.out.println(i);
    





    for (int i=1; i>3; i--)
      System.out.println(i);
    






  5. On the next page, there is some code for a Java class that encapsulates an array of people's ages in a class of students.

    This code is incomplete, and you need to finish it by adding code at various places. The list below is your check list of what needs to be done.

    IMPORTANT: Write your answers to these questions IN THE CODE ON THE NEXT PAGE (not in the space below!)

    1. (10 pts) Find the point marked:

      // (a) allocate the array

      Add code that allocates space for the array called ages

    2. (10 pts) Find the point marked:
      // @@@ (b) Now add code that adds the age to the list
      // @@@ (d) (optional) add code to keep track of max age
      Write code that adds the age to the list. Also adjust any other instance variable as needed.
      (Don't worry about item (d) yet—we'll get to that shortly.)

    3. (5 pts) Find the line of code marked:

      // @@@ (c) add code to throw an exception if there aren't any students
      //    in the list yet
      Add code according to the instructions in the comment.

    4. (10 pts) Now, find the comment marked

      // @@@ (d) add code that returns the age of the oldest student


      Add code according to the instructions in the comment.

      Note that there at least two different ways to do this:
      • One way only requires you to add code inside getAgeOfOldest()
      • The other way requires you to add code at all the places marked (d), including the ones marked (d) (optional)

      Either way is acceptable for full credit—but your answer must be complete.

    Code for question 4—answer question 4 ON THIS PAGE!!!
    ... // opening javadoc comments omitted for reasons of space
    public class StudentAges 
    {
        private final int MAX = 30;
        private int [] ages;
        private int count;
    
        //@@@ (d) (optional)  You may add instance variables if you need them
    
        
        
        /**
         * Constructor for objects of class StudentAges
         */
        public StudentAges()
        {    
                          // (a) allocate the array
        
            count = 0;           
        }
    
        /**
         * Add a student's age to the list 
         * @param  age  The age to add
         */
        public void add(int age) 
         throws Exception             
        {        
    
            if (age < 0)
               throw new IllegalArgumentException("Age can't be negative");
    
            if (count >= MAX)
              throw new Exception("The list is full!");

    // @@@ (b) Now add code that adds the age to the list // @@@ (d) (optional) perhaps also add code to keep track of max age } /** Age of oldest student in the course * @returns age of oldest student in the course * @throws Exception if there is no student in the list yet */ public int getAgeOfOldest() throws Exception { // @@@ (c) add code to throw an exception if there aren't any students in the list yet // @@@ (d) add code that returns the age of the oldest student // If needed, add code to the other places marked (d) (optional) also. // (There is more than one way to do this---depending on how you // tackle the problem, you might not need to add code anywhere else.) } // end method getAgeOfOldest } // end class
  6. (10 pts) Fill in the two missing lines of code in this test class for the StudentAges class you worked on in Question 4 (on the previous page).

    public class StudentAgesTest extends junit.framework.TestCase
    { public void testGetAgeOfOldest() { // @@@ fill in a missing line of code try { a.add(18); a.add(19); a.add(20); a.add(17); // @@@ add the missing argument(s) to the method call on the next line of code

    assertEquals( ); } catch (Exception e) { fail("I didn't expect an Exception in this test case!"); } } }
  7. (5 pts) If you don't use a try/catch necessary in the previous test case you get an error message from the compiler:
    java.lang.Exception must be caught or declared to be thrown
    What does this error message mean? Explain it the way you would at a job interview with Mary Pranks (see Question 1).

End of Exam

Total points: ?