CS56—Midterm Exam 1—Question 1
E01, Q01, S13, Phill Conrad, UC Santa Barbara 04/25/2013
STAPLE YOUR NOTES SHEET TO THIS ONE, AND HAND IT IN WITH YOUR EXAM.
YOU MAY USE THIS FOR SCRATCH WORK, BUT ALL ANSWERS SHOULD BE ON YOUR EXAM PAPER.
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
- Write the code for the
TeamMember
class, per the instructions below.
A TeamMember object represents a member of a sports team where the players wear numbered jerseys (e.g. Basketball, Soccer, Baseball). Each instance of the class stores the following:
- the name of the player (e.g. "Melissa Zornig", "Cesar Castillo")
- the jersey number of the player, a positive integer (e.g. 22, 7)
For full credit, your class should have all of the following.
Go over this as a "checklist" when you are finished to make sure you have everything needed.
Note that for this exam, you do NOT need to include Javadoc comments. (I may ask you about those on midterm 2).
- (10 pts) Correct syntax and structure of a Java class.
-
(8 pts) A two-argument constructor that takes arguments for name, and jersey number
Example invocation: TeamMember mz = new TeamMember("Melissa Zornig",22);
Example invocation: TeamMember cc = new TeamMember("Cesar Castillo",7);
- (8 pts) Private instance variables for the name, and jersey number.
-
(8 pts) getter methods called
getName()
and getJerseyNum()
. Do not create setters, only getters.
-
(8 pts) a correct
toString()
method. Examples of the string value that should be returned: "Melissa Zornig (#22)"
or "Cesar Castillo (#7)"
.
-
(8 pts) a correct
equals()
method. Two TeamMembers
should be considered equal if they have the same name and jersey
number.
Hint 1: the first three lines of every correct equals() method check for null, check the type, and cast the parameter to an instance of the type of the current object. You may copy the following into your method as the first three "boiler plate" lines of code—just write the line(s) that come after these.
If you want, to save time, just put a comment: // boiler plate here
at the start of your equals method, and I'll know that you want those three lines copied in your answer at that point.
// boiler-plate for an equals method:
if (o==null) {return false};
if (! (o instanceof TeamMember) ) {return false;}
TeamMember tm = (TeamMember) o;
Note: Team members and jersey numbers from http://ucsbgauchos.com
End of Q01 Handout—Total points: ?