CS242: Advanced Programming Concepts in JAVA
Fall 2008
Program 4
Due: 10:00 AM, Monday, 9/22/08
(a) A container class is a class that contains objects. Java has lots of
container classes, but this assignment is to implement some
simple container classes of your own design.
Class Bag is an abstract container class.
| Bag |
| protected static final int maxSize |
| public boolean isEmpty() |
- maxSize is an integer constant that will be used by the derived classes
of Bag
to specify the maximum number of elements that can be stored in any instance
of the class. For this assignment, you should set it to some small value, like 5.
- isEmpty() is an abstract method. It will be implemented by the derived classes to
indicate whether or not an instance contains any elements.
Class Multiset is an extension of Bag. It must implement the method in Bag,
and all the methods that it declares. A multiset is like a set in that the order of its
elements does not matter, but it is different from a set in that it may have
multiple copies of its elements.
| Multiset |
| public Multiset() |
| public Multiset(Object element) |
| public boolean add(Object element) |
| public boolean delete(Object element) |
| public boolean equals(Multiset m) |
- Multiset() is a constructor that initializes the instance of the new
object as an empty multiset.
- Multiset(Object element) is a constructor that initializes the instance
of the new object as as a multiset containing the single element passed to it.
- add(Object element) is a method that adds the element to the multiset,
returning true if successful and false otherwise.
- delete(Object element) is a method that deletes the element from the multiset,
returning true if successful and false otherwise.
- equals(Multiset m) is a method that compares this multiset with the multiset
m, returning true if and only if they are equal. Equal does not mean
they have the same reference; it means every element in either one of the multisets
occurs the same number of times in the other one.
Class Set is an extension of Multiset. It overrides the add method and
inherits all the other methods from Multiset.
| public Set |
| Set() |
| public Set(Object element) |
| public boolean add(Object element) |
- Set() is a constructor that initializes the instance of the new
object as an empty set.
- Set(Object element) is a constructor that initializes the instance
of the new object as as a set containing the single element passed to it.
- add(Object element) is a method that adds the element to the multiset,
returning true if successful and false otherwise. One possibility that
causes it to return false is if the element is already in the set.
You should also create a class TestBag which contains the main method.
It should create several instances each of Multiset and Set that contain
Objects of type Character or Integer (not char or int) and demonstrate
that the class methods work correctly. Each instance
need only contain objects of one type. TestBag does not need to check for user
errors, but depending on how you implement the assignment, your methods may need to
have some checks to prevent abnormal program termination. You don't need to
use try and catch blocks yet.
(b) Add comment documentation to your program (all the classes including TestBag)
and use javadoc to extract the documentation into an HTML file. Include a javadoc
comment for each class and each method. The class comment should include your name
and the date (use the @author tag). Method comments should briefly describe
the purpose of the method, its arguments, and what, if anything, it returns (using
@param and @return as appropriate). Although these are small classes now, and this
may seem like a lot of commenting, we will expand on this assignment at a later date.
The physical location of the javadoc comments is critical. A class comment
must immediately precede the line containing the keyword class, and method
comments must immediately precede the method.
When you have commented your source files, run javadoc to create the .html files
and view them in a browser of your choice.
What to submit:
-
Turn in a hardcopy printout of your java source files Bag.java, Multiset.java,
Set.java, and TestBag.java,
and a printout of your test runs. Do not give me a copy
of the javadoc.html files.
-
All Java programs will be submitted by copying (or ftping) them to a specially
designated directory in AFS. Each person in the class has a subdirectory
which has the same name as your login
in
/afs/cu/class/cs242/fa08
Within this individual directory, create a subdirectory named p4.
Copy only the source files into this directory, not class files or javadoc
files.
So, for example, if your login name was smithjd then the four .java files
should be copied to
/afs/cu/class/cs242/fa08/smithjd/p4. These directories have permissions
set so that no one other than yourself and Prof. Lynch can read the files.
You may, of course, use whatever
operating system and compiler you wish to develop your code,
but the version you turn in, both hardcopy and the files submitted to AFS,
must compile and run under JDK installed in the Clarkson ITL (our lab room).
Your grade on the program will be based on:
- Correct execution. The program must follow all the specifications above.
The hardcopy of your program's execution should clearly
demonstrate that it does. You may want to turn in hardcopy of more than one execution.
- Correct implementation. The program must follow all the design requirements above, in particular the
the structure of the three container classes.
- Readability of the code: commented and well-structured, so that it is
clear what the code is doing.
Use an accepted programming style. The style of the text
is fine, or you can follow the Code Conventions.
Whatever you do, be consistent.
- Thoroughness of testing. TestBag should clearly demonstrate that all the
methods in Multiset and Set work correctly. I may use my own version of
TestBag too.
- Timely submission of your program. All hardcopy must be turned in by the deadline.
Late programs will not be accepted. Printouts submitted electronically will be ignored.
-
Plagiarism (copying all or part of anyone else's
code) is a serious infraction of Clarkson regulations;
the penalty is a grade of 0 for the first infraction,
a final grade of F for further infractions.