CS10 Midterm Exam 1
E01, 09S, Phill Conrad, UC Santa Barbara
04/24/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) Assume you have a Java class for a Track on a CD with instance variables as 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.

    Fill in the code for a two argument construtor as described in the Javadoc comment.

    ...
    class Track
    {
    private String name;
    private int trackNum;
    ...
    /** 2-argument constructor, to be used like this:
    * Track t1 = new Track("Sgt. Pepper's Lonely Hearts Club Band",1);
    * Track t2 = new Track("With a Little Help from My Friends",2); * * @param name name of the track * @param trackNum number of the track */

    ...
    }
  2. (10 pts) Assume you have a Java class for a Date with instance variables as shown.

    Fill in the code for a no-argument (no-arg) construtor as described in the Javadoc comment.

    ...
    class Date
    {
    private int month;
    private int day;
    ...
    /** no-arg constructor to be used like this: * Date d = new Date(); // sets date to January 1st */
    ...
    }
  3. Assume you have a Java class for a Vehicle with instance variables as shown.

    As described in the Javadoc comments, fill in:
    ...
    class Vehicle
    {
    private int mileage;
    private String licensePlate;
    ...
    /** getter for mileage (I'm leaving out the @param and/or @return in the
    Javadoc comment so as not to give away which one you need) ... */








    ...
    /** setter for mileage (I'm leaving out the @param and/or @return in the
    Javadoc comment so as not to give away which one you need) ... */










    ...
    }
  4. (10 pts) Assume you have a Java class for a Movie with instance variables as shown below. I've provided the javadoc comment for a constructor as well—you can assume that the constructor works as shown.

    You just tried the following code in a main program:

      Movie classic = new Movie ("The Godfather", "Coppola", 1974);
      System.out.println("classic = " + classic);


    What you got as output was this: classic = Movie@34a890
    What you wanted as output was this: classic = The Godfather (Coppola, 1974)

    Add a method to the class below so that you'll get the output you want—without changing your main program?


    ...
    class Movie
    {
    private int title;
    private int directorLName;
    private int year;
    ...
    /** 3-arg constructor * * @param theTitle the title of the movie * @param directorLName the last name of the director * @param year year the film was released */ ...
    /* You DON'T need to write the constructor for this exam question; assume it has already been written. Add something else
    so that you get the output indicated above. */
















    ...
    }
  5. (10 pts) Assume you have a Java class for a Time with instance variables for hour and minute of the day, as shown below.

    Important: assume that the hours field is specified in military time—i.e. 0 indicates midnight, 10 indicates 10am, 12 indicates noon, 14 indicates 2pm, etc.

    Add a predicate method called isAM that returns whether or not the time object on which the method is in the morning, i.e. whether the hours field is less than 12.

    For full credit:


    ...
    class Time
    {
    private int hour;
    private int min;
    ...















    ...
    }
  6. (5 pts) Rewrite this Java expression as an equivalent one that doesn't use the ! operator:

    ! ( (x < 0) || (x >= 10) )




  7. (5 pts) Write a line of code that declares a variable normTemp and initializes it to the value 98.6 (choose an appropriate data type.)




  8. What is printed by each of the following:

    1. (5 pts) System.out.println(4 + 2);



    2. (5 pts) System.out.println("4" + 2);



    3. (5 pts) System.out.println (2 + 3 * 4);



  9. (5 pts) The following two lines of code are supposed to print TGIF whenever the String variable day refers to a string value "Friday".
    However, this code contains a common Java coding error—it will compile, but it won't always work.
    Rewrite this code so that it works properly.

    if (today == "Friday")
       System.out.println("TGIF");






End of Exam

Total points: ?