CS10 Final Exam
E03, 09S, Phill Conrad, UC Santa Barbara
06/10/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:

A hint for allocating your time:

You have three hours for this exam.
However, it is designed to be completed in 100 minutes—i.e. 1 minute per "point".

So I recommend, in your "first pass" through the exam:

That way, when you are finished, you'll still have over an hour to come back and work on those problems you didn't finish, and/or to check your answers.


  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 these two questions:

    In Java, some things are stored on the stack, and other things are stored on the heap.|

    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. (5 pts) Still answering briefly but reflecting deep understanding—when we say that something in Java is a reference, what does that really mean?






  3. (10 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.

    public interface Copyrightable // a thing that can be copyrighted
    {
    public int getCopyrightYear();
    public String getCopyrightHolder();
    }
    public class Book
    {
        private String title;
        private String publisher; // publisher holds the copyright for books
        private int year;
    
        /**
         * Constructor for objects of class Book
         */
        public Book(String title, String publisher, int year)
        {
           this.title = title;
           this.publisher = publisher;
           this.year = year;
        }
    
        public String getTitle() { return this.title;}
        public String getPublisher() { return this.publisher;}
        public int getYear() { return this.year;}
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }          
    
           

    public class Movie { private String title; private String studio; // studio holds the copyright for movies private int year; /** * Constructor for objects of class Movie */ public Movie(String title, String studio, int year) { this.title = title; this.studio = studio; this.year = year; } public String getTitle() { return this.title;} public String getStudio() { return this.studio;} public int getYear() { return this.year;} }
  4. (10 pts) Fill in the code for the method called "ordinal. Test cases for this method appear below the StringUtils class


    public class StringUtils
    {
    /** Return string such as 1st, 2nd, 3rd, 4th, 20th, 21st, etc.
    * @param i the integer that will form the basis of value returned
    * @return string formatted as an ordinal number
    */

    public static String ordinal(int i)
    {
    if (i < 1)
    throw new IllegalArgumentException("i must be >= 1");

    // Hints: Be sure you can handle 1st, 2nd, 3rd, 4th...
    // 21st, 22nd, 23rd, 24th..
    // Note that the 11th through the 19th are exceptions
    // 111th through 119th work the same way.
    // Use i % 10 to get the last digit, and i % 100 to get the last two.



















    }
    } public class StringUtilsTest extends junit.framework.TestCase { public void testOrdinal1() { assertEquals("1st",StringUtils.ordinal(1));} public void testOrdinal2() { assertEquals("2nd",StringUtils.ordinal(2));} public void testOrdinal3() { assertEquals("3rd",StringUtils.ordinal(3));} public void testOrdinal4() { assertEquals("4th",StringUtils.ordinal(4));} public void testOrdinal11() { assertEquals("11th",StringUtils.ordinal(11));} public void testOrdinal12() { assertEquals("12th",StringUtils.ordinal(12));} public void testOrdinal19() { assertEquals("19th",StringUtils.ordinal(19));} public void testOrdinal21() { assertEquals("21st",StringUtils.ordinal(21));} public void testOrdinal22() { assertEquals("22nd",StringUtils.ordinal(22));} public void testOrdinal23() { assertEquals("23rd",StringUtils.ordinal(23));} public void testOrdinal101() { assertEquals("101st",StringUtils.ordinal(101));} }
  5. (5 pts) Write a method that takes an array of ints, return true if the array is length 1 or more, and the first element and the last element are the same.


    sameFirstLast({1, 2, 3}) → false
    sameFirstLast({1, 2, 3, 1}) → true
    sameFirstLast({1, 2, 1}) → true

    public boolean sameFirstLast(int[] nums) {
















    }

  6. (5 pts) Write a method that takes an array of ints, and a variable count that indicates how many of the elements of the array have been initialized with useful values (e.g. because those value were read in from the user, or a file, using a sentinel.)


    Assume that the portion of the array that has been initialized starts at element 0, and goes up to element count - 1.

    Return true if all the elements of the array that are "in use" (i.e. the ones from 0 through count - 1) have the same value.


    sameValue( { 13, 13, 13, 13, 0, 0, 0 }, 4 ) → true
    sameValue( { 13, 13, 14, 13, 0, 0, 0 }, 4 ) → false
    sameValue( { 17, 17, 0, 0, 0, 0, 0 }, 2 ) → true


    public boolean sameValue(int[] nums, int count) {
















    }



  7. (10 pts) Answer the number conversion questions below:


    Convert: Answer

    1101 1100 from binary to base 10


     

    0110 0000 0000 1101 from binary to hexadecimal


     

    Convert 73 from octal to binary

     

     

    Convert 001 111 011 from base 2 to octal

     

     

    Convert 147 from base 10 to base 2

     

     

    All the remaining questions on this exam pertain to the code on the separate handout.

  8. Find the code for the Date class on the separate handout. Look it over briefly.
    Then, find the code for the DateTest class, immediately afterwards.


    1. (5 pts) The first line of this class reads public class DateTest extends junit.framework.TestCase

      What does the word extends mean in this context?

      Answer the question HERE on this sheet:



    2. (5 pts) The method testConstructorAndGetters() tests whether the constructor correctly sets the three instance variables, and whether the getters correctly retrieve their values. But there is one important feature of the Date constructor that is not being tested by any test case in DateTest. What is that feature, and how would you go about testing it?

      Answer the question HERE on this sheet:

      (Note: I'm not asking you to write the Java code for that test case—it is enough for you to describe briefly in English what that test case would have to check for.)





    3. (5 pts) The method testConstructorAndGetters() has three calls to assertEquals().

      What would be the advantage to breaking up this method into three separate methods, like this:

      Answer the question HERE on this sheet:

      public void testConstructorAndGetters1a() 
      {
              Date d = new Date(12,25,2009);
              assertEquals(12,d.getMonth());
      }
      
      public void testConstructorAndGetter1b() 
      {
              Date d = new Date(12,25,2009);
      
              assertEquals(25,d.getDay());
      
      }
      
      public void testConstructorAndGetters1c() 
      {
              Date d = new Date(12,25,2009);
      
              assertEquals(2009,d.getYear());
      }                                             
  9. The remainder of the questions are answered
    THERE on the separate handout,
    not here on this sheet.

  10. Find the code for class Time on the separate handout. You may also want to look at the TimeTest class below.

    1. (10 pts) THERE on the handout, in the code for the Time class, fill in the code for the constructor. For full credit, you should include code that checks to make sure the value of each parameter is valid—and if it is not, you should handle that in an appropriate way.

      Hint: you may want to look at the Date class as a model.

    2. (10 pts)THERE on the handout in the code for the Time class, fill in the code for the toString() method.

      BEFORE YOU DO—look back at the TimeTest class below to see what the toString() method is supposed to return for various values.

      Code for the TestTime class—a test class for the Time class (which is on the separate handout).

      
      public class TimeTest extends junit.framework.TestCase
      {
          public void testConstructorAndSetters()
          {      
              Time t = new Time(9,30);
              assertEquals(9,t.getHour());
              assertEquals(30,t.getMin());        
          }
      
          public void testToString1()
          {      
              Time t = new Time(0,0);
              assertEquals("12:00AM",t.toString());
          }
      
          public void testToString2()
          {      
              Time t = new Time(9,30);
              assertEquals("09:30AM",t.toString());
          }
      
          public void testToString3()
          {      
              Time t = new Time(12,01);
              assertEquals("12:01PM",t.toString());
          }
      
          public void testToString4()
          {      
              Time t = new Time(13,59);
              assertEquals("01:59PM",t.toString());
          }
      
          public void testToString5()
          {      
              Time t = new Time(16,00);
              assertEquals("04:00PM",t.toString());
          }
          
      }
      
      
      
      
      
      

  11. On the separate sheet, find the class BirthdayParty. This class uses inheritance to extend the Event class listed below.
    1. (5 pts)THERE on the handout Fill in the constructor for the Birthday Party class
    2. (5 pts)THERE on the handout Fill in the toString method.

    Code for the Event class, and a test class for Event.

    public class Event
    {
        private Date date;
        private Time time;
        private String what;
    
        /**
         * Constructor for objects of class Event
         * 
         * @param date Date of the event
         * @param time Time of the event
         * @param what What is taking place
         */
        public Event(Date date, Time time, String what)
        {
            this.date = date; 
            this.time = time;
            this.what = what;
        }
    
        public Date getDate()
          { return this.date; }
    
        public Time getTime()
          { return this.time; }
    
        public String getWhat()
          { return this.what; }
      
        public String toString()
          { return what + " (" + date.toString() + " at " + time.toString() + ")"; }
          
    }   
        

    public class EventTest extends junit.framework.TestCase
    {
        public void testConstructorAndSetters()
        {
          Date d = new Date(6,10,2009);
          Time t = new Time(16,00);
          Event e = new Event(d,t,"CS10 Final");
          assertEquals("06/10/09",e.getDate().toString());
          assertEquals("04:00PM",e.getTime().toString());
          assertEquals("CS10 Final",e.getWhat());
            
        }
    
        public void testToString1()
        {
          Date d = new Date(6,10,2009);
          Time t = new Time(16,00);
          Event e = new Event(d,t,"CS10 Final");
    
          assertEquals("CS10 Final (06/10/09 at 04:00PM)",e.toString());
        }
        
    }
        

End of Exam

Total points: ?


CS10 Final Exam
Programming Questions
E03, 09S, Phill Conrad, UC Santa Barbara
06/10/2009

Name: ________________________________________________________


Umail Address: __________________________________@ umail.ucsb.edu


Circle Lab section:        4PM             10AM           11AM             noon


Here is code for a class that encapsulates the idea of a "date" with month, day and year.
Look it over. Then look over the test cases for this code on the next page---and then
answer the questions on the main exam about these two classes.

import java.text.DecimalFormat;

public class Date { private int month; // between 1 and 12 private int day; // between 0 and 31 private int year; // all four digits, e.g 2009, between 2000 and 2099 public Date (int month, int day, int year) { if ( month < 1 || month > 12) throw new IllegalArgumentException("month is out of range"); if ( day < 1 || day > 31) throw new IllegalArgumentException("day is out of range"); if ( year < 2000 || year > 2099) throw new IllegalArgumentException("year is out of range"); this.month = month; this.day = day; this.year = year; } public int getMonth() { return this.month; } public int getDay() { return this.day; } public int getYear() { return this.year; } public String toString() { DecimalFormat twoDigits = new DecimalFormat("00"); // pad with zeros return twoDigits.format(month) + "/" + twoDigits.format(day) + "/" + twoDigits.format(year % 100); } }

Here is the code for a test class for Date. Answer the questions on the main exam about this code.



public class DateTest extends junit.framework.TestCase     // @@@ See question 8a
{

// @@@ See question 8b and 8c public void testConstructorAndGetters1() { Date d = new Date(12,25,2009); assertEquals(12,d.getMonth()); assertEquals(25,d.getDay()); assertEquals(2009,d.getYear()); }

// @@@ See question 8d

public void testToString1()
{ Date d = new Date(12,25,2010); assertEquals("12/25/10", d.toString()); } }

Code for the Time class---there are questions on the exam
that you are supposed to answer on THIS sheet.

import java.text.DecimalFormat;

public class Time { private int hour; // between 0 and 23 private int min; // between 0 and 59

//@@@ Question 9a: Fill in the code for the constructor

public Time (int hour, int min) {




}

public int getHour() { return this.hour; } public int getMin() { return this.min; } public String toString() {


//@@@ Question 9b: Fill in the code for the toString method.
// See the test cases in TimeTest for what should be returned

// Hints: you need to consider four cases: // when hour is zero (i.e. 12am) // when hour is between 1 and 11 inclusive (1am to 11am) // when hour is twelve (i.e. 12 pm) // when hour is between 13 and 23 inclusive (i.e. 1pm to 11pm) // Also, for easy way to format numbers between 1 and 9 with a // leading zero, see the code for the toString method of the Date // class, which was given to you earlier on this exam.















} }

Code for the BirthdayParty class---you need to fill in some extra code here as part of a question on the exam.

public class BirthdayParty extends Event
{
 
// @@@ FILL IN THE INSTANCE VARIABLES NEEDED
// @@@ KEEP IN MIND HOW INHERITANCE WORKS
// @@@ LOOK AHEAD AT THE GETTERS AND THE UNIT TESTS
// @@@ TO SEE WHAT IS NEEDED




/** * Constructor for objects of class BirthdayParty */ public BirthdayParty(Date date, Time time, String who, int howOld) { // For "what" we will use the hard coded string "Birthday Party" // the other parameters to the constructor are handled // in the normal way.
// @@@ Question 10a: FILL IN THE CODE FOR THE CONSTRUCTOR,
// KEEPING INHERITANCE IN MIND







}
/** * getter for who * @return who the birthday party is for */
public String getWho() { return this.who; }

/** getter for how old * @return how old the birthday girl or birthday boy will be */ public int getHowOld() { return this.howOld; }

/** print event as a string formatted like: * "Birthday Party (06/09/2009, 04:00PM), Fred's 10th" * @return formatted string for this event */

public String toString() { // @@@ Question 10b: Fill in the toString method

// Hints: Use the StringUtils.ordinal method from // Question 4 to to the conversion to 1st, 2nd, 3rd, etc.






}