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.
public interface Copyrightable // a thing that can be copyrighted |
|
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 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));} }
public boolean sameFirstLast(int[] nums) {
}
public boolean sameValue(int[] nums, int count) {
}
| 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.
public class DateTest extends junit.framework.TestCaseextends mean in this context?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? testConstructorAndGetters() has three calls to assertEquals(). 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());
}
The remainder of the questions are answered
THERE on the separate handout,
not here on this sheet.
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());
}
}
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());
}
}
Total points: ?
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.
}