Read the WHOLE handout before you begin to write the assignment.
CBC is planning on launching a gritty new drama series, called The Greases, kind of like As the World Turns. The producers of this show have a problem, however: the Grease family is so large that they have trouble keeping track of who is related to whom, what the relation is between any two characters, and which couples can't get married because they're cousins.
That's where you come in, as the CBC's resident computer expert. They've asked you to devise a system that will keep track of all the family members in this show and allow them to add characters and keep track of characters as the show progresses. Make sure that the Java class you implement to store information on Grease family members is a good one --or they may not renew your job for next season.
For this assignment, you are required to design two classes. Class FamilyMember
is the real one. It has lots of fields and methods, but each method is simple.
If you do one thing at a time, not worrying about the overall picture, you should
have little trouble.
Class FamilyMemberTester
is used to test class FamilyMember
.
We will describe how to do this on 8-10 February. Do not think too much about
it when first reading this handout.
An instance of class FamilyMember
represents a single member
of a family (in this case, one of the Grease family members). It has several
fields that one might use to describe a relative, as well as methods that operate
on these fields.
Class FamilyMember
will have the following 11 fields:
String
)String
)int
)String
)int
)int
)boolean
)FamilyMember
object)FamilyMember
object)int
)int
)
Here are a few specifications about the fields of the
FamilyMember
class:
String
values: "male"
or "female"
. true
if the family member
has a criminal record and false
otherwise. FamilyMember
objects that correspond to this person's parents. They are null
if not known.FamilyMember
object is created, this value should
increase by 1. Class FamilyMember
supports the following methods. Pay close
attention to the parameters and return values of each method. The descriptions,
while informal, are complete.
Constructor | Description |
---|---|
|
Constructor: a new |
|
Constructor: a new |
|
Constructor: a new |
Method | Description |
---|---|
|
= the first name of this family member (a String ) |
|
= the last name of this family member (a String ) |
|
= the full name of this family member (a String ). The
format of the name is: the first name, a blank char., and finally the
last name. E.g.: "John Mac" is correct; "Mac, John" iswrong. |
|
= the age of this family member (an int ) |
|
= the gender of this family member (a String ). |
|
= the day of the month this family member was born (an int ).
|
|
= the integer that represents the family member's month of birth (an
int ). |
|
= (the name of the object representing) the father of this family member
(a FamilyMember ). |
|
= (the name of the object representing) the mother of this family member
(a FamilyMember ). |
|
= the number of children of this person (an int ). |
|
Static method. = the number of people in the entire family --the number
of FamilyMember objects created thus far (an int ).
|
|
= "this family member is a criminal" (a boolean ) |
|
Set the first name of this family member to n . |
|
Set the last name of this family member to n . |
|
Set this family member's age to age . |
|
Set the gender of the family member to g (one of "male"
and "female" ). |
|
Set the day of the month this family member was born to i .
|
|
Set the month of birth for this family member to i . |
|
Set whether this family member is guilty of a crime to b
(true or false ). |
|
Set this person's father to fm . Precondition: This person's
father is null , fm is not null ,
and fm is male. |
|
Set this person's mother to fm . Precondition: This person's
mother is null , fm is not null ,
and fm is female. |
|
= "this family member is older than fm" (a |
|
Static function. = " |
|
Static function. = |
|
= " |
|
= " |
|
Static method. = " |
|
= "this family member is |
|
= "this family member is |
|
= "this family member is |
|
Static method. = " |
|
= "this family member is an evil twin of |
Make sure that the names of your methods match those listed above exactly, including capitalization. The number of parameters and their order must also match. The best way to do this is to copy and paste. Our testing will be expecting those method name names and parameters, so any mismatch will fail during our testing. Parameter names will not be tested --you can change the parameter names if you want.
Each method must be preceded by an appropriate specification, as a
Javadoc comment. The best way to ensure this is to copy and paste. Note that
a precondition should not be tested by the method; it is the responsibility
of the caller to ensure that the precondition is met. As an example, in method
isMotherOf
, the method body should not test whether fm
is null. However, in function areSiblings
, the tests for fm1
and fm2
not null
must be made.
The number of children of a newly created person is 0. Whenever person P is made the mother or father of another person, P's number of children should increase by 1.
It is possible for person P1 to be P2's mother, and visa versa, at the same time. We do not check for such strange occurrences.
Your method bodies should have no if statements. Your method bodies should contain only assignments and return statements. Points will be deducted if if statements are used.
How do you know whether class FamilyMember
that you are designing
is correct? The only way you can be sure is to test it, to see if it does
what it is supposed to do. It is not enough simply to try out your class FamilyMember
in the interactions pane. Every time you write a method for your class
FamilyMember
, you should also write a couple of tests for it.
And run your collection of tests frequently to make sure that everything works
correctly.
The FamilyMemberTester
is the JUnit test suite you'll design,
which performs these testing tasks for you. Make sure that your test suite
adheres to the following principles:
FamilyMember
, your test suite
should have at least one test case that tests that method.
Remember that if you change static variables in an early test, they will retain their values in later tests. Also, the tests are not necessarily run in the order in which you list them in your test suite. So when testing static variables, record their initial value at the beginning of the test, and test that the change in the value is what you expect.
FamilyMember
, compiling
often as you proceed.FamilyMember
.FamilyMemberTester
to make
sure that the first constructor and all the getter methods work.FamilyMemberTester
to test it (do one at
a time).FamilyMemberTester
to test them.FamilyMemberTester
to test them.At each step, make sure all methods are correct before proceeding to the next step. When adding a new method, cut and paste the comment and the header from the assignment handout.
.java
.
Be careful about this, because in the same place as your .java
files you may also have files that end with the .class
suffix,
but otherwise have the same name. In particular:
FamilyMember.java~
". The ~
indicates
that the file is a backup file, which happens to be an older version
of your program. Since
.class
files cannot be read by TAs or run with our testing programs, submitting the wrong files creates havoc with grading your assignment. Every year, a half-dozen students submit the wrong file. Don't do it!
if
statements
when completing this assignment. For boolean expressions the &&
(AND) and ||
(OR) operators are sufficient to implement all
the methods shown above. You will lose points for using if
statements.
FamilyMember
methods can be
implemented easily by using other FamilyMember
methods that
you have already created. Look for these case, and take advantage of them
as much as possible.
toUpperCase
and toLowerCase
in class String
may be useful.
String
literal is enclosed
in double quotation marks and a char
literal is enclosed in
single quotation marks.
.equals
to compare objects
for equality and ==
to compare primitive values for equality.
null
.
So comparisons between primitive types and null
are not legal.
File
->†New JUnit Test Case
, and then replace the testX
method
with many methods that test your FamilyMember
functionality.