For this assignment, start with your A1 classes Virus and VirusTester, which should be correct. Assignment A3 asks you to add to Virus of A1: (1) a static variable and (2) more comparison methods. For this assignment, you can group all your extra test cases in one test procedure.
Keep track of the time you spend on this assignment. When you submit it, tell us how many hours you spent on it.
You may work in groups of 2, but you must do all the work sitting at the computer together. For example, for one person to write functions and the other person to write test cases, with no interaction, is dishonest. If you want to form a group, do it immediately. Both partners must take action in the CMS before the group will be formed.
Because you have a lot of work to do, including studying for the prelim, we have considerably reduced the work involved in this assignment by removing 5-6 functions from previous years.
In assignment A1, if you made a mistake, we asked you to fix it and resubmit. In this assignment, if you make a mistake, points will be deducted. Points may be deducted for lateness. Your new functions must have suitable javadoc comments, there must be appropriate test cases, and all methods should be correct. You can achieve all this most easily by writing and testing one method at a time.
Step 1. Static variable. Add to class Virus
a private static variable that contains the number of Virus
objects that have
been created so far. YOUR CLASS MUST MAINTAIN THIS FIELD --whenever a new virus is created, this field has to be increased. Provide suitable test cases to check that this is done,
and provide appropriate comments.
Add a static int function getPopulation
, with no parameters, that will return the value of the
static variable
just described.
Change the constructors in the class so that they properly maintain the value
of the static variable. Finally, add test cases to class VirusTester
to test
whether the static variable is properly maintained.
VirusTester
. Then proceed to the next function. Method | Description |
---|---|
isPredecessorOf(Virus v) |
= "v is not null, and virus v was generated from this one". |
isPredecessor(Virus v) |
= "v is not null, and this virus was generated from virus v". |
isSibling(Virus v) |
= "v and this virus are siblings". |
The names of your methods much match those listed above exactly, including capitalization. The number of parameters and their order and types must also match. The best way to ensure this is to copy and paste. Our testing will expect those method 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 from this handout. After you have pasted, be sure to do any necessary editing.
Note carefully the definition of sibling in the specs. Use this definition when writing the methods.
• In testing whether two viruses v1
and v2
are the same object, do not use the field that contains their names. Instead, use the names on the tabs of their objects, like this: v1 == v2
. But to test two String objects s1
and s2
to see whether they contain the same sequence of characters, use s1.equals(s2)
.
• Write isSibling
so that it calls one of the other two functions. We want you to begin seeing how a program can call methods already written instead of doing duplicate work themselves. Points will be deducted if isSibling
does not call one of the other two functions appropriately.
• Boolean
expressions, using the operators &&
(AND), ||
(OR), and ! (NOT), are sufficient to implement
the three new functions. You will lose points for using if
statements or conditional expressions in them.
• When you are done, add a comment to the beginning of file Virus.java that states how many hours you spent on this assigment. We will report the min, average, mean, and max.
Check these points before you submit your assignment:
null
did you provide a test case for it? Submit only files that end with .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.