100 points

4/8/2009
CS 110 Exam 3
Page 1
Computer Science I
Spring 2009, Wednesday, 4/8/09
100 points
Name _____________________________
1. Fill in the blanks for a class PersonEntry that represent an entry in an address book.
The contents of an entry in the book are:
 The first name of the person
 The surname of the person
 The e-mail address of the person
 The telephone number of the person
The methods are able to
 Access each attribute
 Change the email address
 Change the telephone number
 Test whether two instances are equal based solely on the full name.
[35 pts]
public class PersonEntry {
private String
firstName;
private ___________ surname;
_______ ___________ email;
_______ ___________ phoneNum;
//constructor
public ______________ (String fn, String sn, _______ em, _______ pn){
__________ = fn;
surname = ______;
email = ______;
_______ = ____________;
}
//accessors
public ____________ getFirstName() {return ____________;}
public ____________ getSurname()
{return ____________;}
public ____________ getEmail()
{return ____________;}
public ____________ getPhoneNum()
{return ____________;}
//mutators and equals
________ void
setEmail(___________ em){ ____________ = ______;}
________ ______ setPhoneNum( _____________ pn) { ___________ = pn;}
________ _________ equals(PersonEntry second){
return (this.firstName.equals(second.____________) ____
______.surname.________(__________ .surname));
}
}
4/8/2009
CS 110 Exam 3
Page 2
2. Show what is printed from the following application segment that uses the class from question #1.
[10 pts]
PersonEntry p1 = new PersonEntry (“John”,”Doe”,
”[email protected]”,”643-1234”);
PersonEntry p2 = new PersonEntry (“Jane”,”Doe”,
“[email protected]”,”643-1234”);
PersonEntry p3 = new PersonEntry (“John”,”Doe”,
“[email protected]”,”643-4321”);
PersonEntry p4 = p1;
if(p1==p3){
System.out.println(“1 = 3”);
}
if(p1.getSurname().equals(p2.getSurname())){
System.out.println(“1 = 2”);
}
if(p1.equals(p3)){
System.out.println(“1 equals 3”);
}
if(p1==p4){
System.out.println(“1 = 4”);
}
String e = p3.getEmail();
p4.setEmail(e);
System.out.println(p1.getEmail());
3. Assume we want to keep a running count of the address book entries in the class defined in question #1 with a
static variable called count.
[2 pts each = 10]
a. Should count be private or public? _________________
b. Should count be an int or double? _________________
c. What should be count’s starting value? ___________
d. In what method (accessor, mutator or constructor) would we increment count?
The method name is _________________
e. If we wanted a static accessor method written called getCount(),
what should prefix this method name to call it? _________________.getCount()
4/8/2009
CS 110 Exam 3
Page 3
4. True/false on objects and classes.
[10 pts]
_____ Objects model real life objects or concepts.
_____ A class represents a template for any number of object instantiations.
_____ Every object instance has a set of its own instance variables.
_____ Instance variables should be declared public so that the user can see how a class is implemented.
_____ The types of instance variables are limited to be primitive types and Strings.
_____ You must provide one or more constructors for a class or use the default constructor.
_____ A mutator method is typically a void method.
_____ An accessor method must return a value of some type (must not void).
_____ A static variable will not exist until at least one instance of the class is created.
_____ A reference variable holds the entire object contents inside the variable.
5. Graphics and event-driven programming true/false.
[10 pts]
_____ A button, label, text field are regarded as objects.
_____ For a button or label to appear on the screen, it must be added to the content pane.
_____ The placement of the button or label in FlowLayout is based on the order of widgets added to the content
pane, left-to-right, bottom to top.
_____ For the button to react to a click event, it has to be registered to the system using the addActionListener()
method.
_____ The actionPerformed() method is called when an event occurs and you have to discern which widget was
involved with the event.
_____ The methods paint() and actionPerformed() are methods called by Java and not from within the program.
_____ The init() method is called upon only once as the main starting point of an applet
_____ You must use a call to repaint() in order for paint() to be called upon by the program.
_____ The paint() method is also called when the window becomes the “focus” of the computer system.
_____ For actionPerformed() to communicate with paint(), we should use only local variables of init()
6. Information Hiding
[10 pts]
a. To use a class’s methods, a(n) ________________ is provided only what he needs to ______________ them.
b. To implement a class’s methods, a(n) ____________________ is provided only what she needs to
_____________________________them.
c. What is a pre-condition?
d. What is a post-condition?
4/8/2009
CS 110 Exam 3
Page 4
7. Design a simple class that represents a DCB card. We want to track an ID number, account holder name, and the
current balance for each instance. Be as complete as possible with methods to be included in the class.
[15 pts]
public class DCB {
//instance variables
//Constructor
//Accessors/getters for each instance variable
//Mutators/setters, methods that update accounts
}