Website http://msnbc.msn.com/id/10877076/ says that, "the rhinoceros population has declined by 90 percent since 1970, with five species remaining in the world today, all of which are endangered. The white and black rhinos are the only species left in Africa... . "
Lately, Kenya (with less than 500 rhinos left!) relocated 33 rhinos to Meru National Park, where they hope to protect them from poachers.
Poachers kill the rhinos for their horns.
Rhinos are not the only endangered species. Web page http://www.redlist.org/ will tell you that the number of endangered vertebrates (mammals, birds, reptiles, amphibians, and fishes) grew from 3,314 in 1996/98 to 5,188 in 2004. Of the 22,733 evaluated species 23% were endangered. See http://www.worldwildlife.org/endangered for more info on endangered species. Some of the rhino pictures in this assignment were downloaded from www.birdingafrica.net.
When an animal population is small, the animals can be monitored. Sometimes they will be captured and tagged. Some tags emit a signal, so that one can track the animal. Even in populated places, animals are tagged. Here in Ithaca, one can see deer with tags on their ears wandering in the fields. Gries sees them often in his back yard near Community Corners.
This assignment is the first of two that, together, illustrate how Java's
classes and objects can be used to maintain data about a collection of
things —like individual rhinos in a park. Read the WHOLE handout before
you begin to do the assignment. For this assignment, you will
design and implement a class Rhino
, which is used by to keep
track of rhinos, and a class RhinoTester
, which is a JUnit
class to test the methods of class Rhino
.
Pay careful attention to the order in which you write and test class Rhino
.
Follow the instructions given below in order to complete the assignment
as quickly and efficiently as possible.
You may do this assignment with one other person. If you are going to work together, then, as soon as possible, get on the CMS for the course and do what is required to form a group. If you do this assignment with another person, you must work together. It is against the rules for one person to do some programming on this assignment without the other person sitting nearby and helping. You should take turns "driving" —using the keyboard and mouse.
If you don't know where to start, if you don't understand testing, if you are lost, SEE SOMEONE IMMEDIATELY. Gries, a TA, a consultant. Do not wait. Over 50 of you have never programmed before, and it is reasonable to expect that you may not fully grasp everything immediately. But a little one-on-one help can do wonder.
An instance of class Rhino
represents a single rhino. It has several
fields that one might use to describe a rhino, as well as methods that operate
on these fields. Here are the fields, all of which should be private (you can choose the names of these fields).
String
) 'F'
for female and 'M'
for male)int
)int
)int
)Rhino
object —actually, the name on the folder of
a Rhino
object)Rhino
object —actually, the name on the folder of
a Rhino
object)Rhino
Here are some details about these fields:
Rhino
objects (manila folders) that correspond to this rhino's parents. They
are not Strings.
They are null
if not known.Accompanying the declarations of these fields should
be comments that describe what each field means —what it contains.
For example, on the declaration of field tag
, state in
a comment that the field is –1 if the rhino is untagged and the tag number
itself (≥ 0) if the rhino is tagged. The collection of these fields is
called the "class
invariant".
Whenever you write a method (see below), look through the class invariant and convince yourself that the class invariant is correct when the method ends.
Class Rhino
has the following methods. Pay close
attention to the parameters and return values of each method. The descriptions,
while informal, are complete.
Constructor | Description |
---|---|
Rhino(String name, int month, int year, char gender) |
Constructor: a new |
When you write the constructor body, be sure to set all the fields to appropriate values.
Method | Description |
---|---|
getName() |
= the name of this rhino (a String ) |
getGender() |
= the gender of this rhino (a char ). |
getMOB() |
= the month in which this rhino was born, in the range 1..12 (an
int ). |
getYOB() |
= the year in which this rhino was born (an
int ). |
getFather() |
= (the name of the object representing) the father of this rhino
(a Rhino ). |
getMother() |
= (the name of the object representing) the mother of this rhino
(a Rhino ). |
getTag() |
= this rhino's tag (–1 if none) (an int ) |
getNumberChildren | = the number of children of this Rhino . |
toString() |
= a String representation of this Rhino . It has to be in a precise format discussed below. |
Method | Description |
---|---|
setName(String s) |
Set the name of this rhino to s . |
setGender(char g) |
Set the gender of the rhino to g . |
setMOB(int i) |
Set the month of birth of this rhino to i . |
setYOB(int i) |
Set the year of birth of this rhino to i . |
setTag(int t) |
Set this rhino's tag to t. Precondition: t ≥ 0 and the tag is currently –1. |
setFather(Rhino r) |
Set this rhino's father to r. Precondition: This rhino's father is null , r is not null , and r is male. |
setMother(Rhino r) |
Set this rhino's mother to r. Precondition: This rhino's mother is null , r is not null , and r is female. |
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 ensure this is to copy and paste. Our testing
will expect those method names, 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,or "spec", as a Javadoc comment. The best way to ensure this is to copy and paste. After you have pasted, be sure to do any necessary editing. For example, the spec of a function does not have to say that the function yields a boolean or int or anything else, because that is known from the header of the method.
A precondition should not be tested by the method; it is
the responsibility of the caller to ensure that the precondition is met.
For example, it is a mistake to call method setFather
with null
for the father argument.
It is possible for person R1
to be R2
's mother, and visa versa, at the same
time. We do not check for such strange occurrences.
In writing methods setFather
(and setMother
), be careful! If F
is becoming
the father of this Rhino
, then F
has one more child, and its field that contains
its number of children has to be updated accordingly.
Function toString
Here is an example of output from function toString:
"M rhino Fatso. Tag 34. Born 6/2005. Has 2 children."
Here are some points about this output.
How do you know whether your class Rhino
is correct? The only way to 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 Rhino
in
the interactions pane. Every time you write a method for your class Rhino
,
you should also write a couple of tests for it. Further, you should run your
collection of tests frequently to make sure that everything works correctly.
Class RhinoTester
will contain your JUnit test suite; it will
perform these testing tasks for you. Make sure that your test suite adheres
to the following principles:
Rhino
, your test suite should
have at least one test case that tests that method. null
,
there should be a test case that has that argument as null
. Here is an example of a test method, with a test case for one getter method. Put all test cases for getter methods in one such test method.
/**Test first Rhino constructor and getter methods*/
public void testGetters() {
Rhino r1= new Rhino("R1Fatso", 5, 2005, 'M');
assertEquals("R1Fatso", r1.getName());
}
Rhino
using DrJava. In it, declare the fields in class Rhino
, compiling
often as you proceed. Write comments that specify
what these fields mean. Rhino
.
r= new Rhino("John", 1, 2007, 'M');
and then write calls on all the getter methods in folder
r
to be sure that they produce what is expected.
toString
and write a method in RhinoTester
to
test it thoroughly.Rhino
to
test them, and test them. We suggest writing and testing one method
at a time —write a method, put tests for it in class RhinoTester
,
and test it thoroughly; then move on to the next method. 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 and then edit the comment.
if
statements.
String
literal is enclosed
in double quotation marks and a char
literal is enclosed in
single quotation marks.RhinoTester
will probably have three test methods in it: one
for the constructor and getter methods, one for method toString
, and
a third for the setter methods. 10 points for suitable definitions of variables on the fields of class Rhino
15 points for suitable Javadoc specifications on methods in class Rhino
25 points for the constructor and getter methods in class Rhino and 10 points for testcases for them in class RhinoTester.
10 points for method toString in class Rhino and 5 points for suitable testcases for it in class RhinoTester.
15 points for the setter methods in class Rhino and 10 points for testcases for them in class RhinoTester.
Submit only files that end with the .java
. Be careful about this, because in the same place as your .java
files you may also have files that end with .class
or .java~
. but otherwise have the same name.