in4023 - Assignment 1
Note: This assignment consists of two parts: A and B.
1-A Generic sets
A set will be regarded as a collection of unique objects of the same type. Compared with lists
and trees, sets have no structure in the sense that there are no next and previous elements for a
given element. However, the elements of a set may be ordered. In the assignment further on
we will consider ordered sets. Sets are used in a wide range of applications and are therefore an
important concept.
The Java API contains many classes for programming lists, trees and sets. However, in Java
1.4 and earlier versions there are no possibilities to dene the type of objects that a given set
should contain. Ensuring that the objects are of the same type, and performing operations set,
can therefore be cumbersome programming exercises. Java 1.5 comes with a far cleaner way to
deal with this issue in providing generics.
Study the (web)literature of the generics mechanism in Java 1.5. Write a straightforward library
GenericSet which provides facilities to create ordered sets and to perform the operations dened
at the end of this document. Additionally:
The sets should be suitable to contain any given comparable type1 of objects. You have to
make sure that the methods make use of the type information where possible.
You are free to extend one of the API provided data structures for vectors, lists or trees.
Make sure to include a test class which tests and demonstrates all possibilities of your
GenericSet!
Just as in real life the library should be portable. To this aim you have to provide it as a
package in a directory dierent from your test class.
1-B Reection
Java contains facilities for a programming technique called reection. Reection can be used to
examine, execute or modify classes, attributes and methods dynamically during run-time. In some
special cases, certain programming goals can not be accomplished by regular techniques, while
reection can provide a solution. Though reection is abstract and quite complex, it can be very
powerful.
Study the (web)literature on the reection facilities in Java.
Consider the following case:
1So you may assume the user provides objects which implement the Comparable interface
1
CarPleasure and DirectCar are two separate companies that sell and assemble
dierent parts and accessories for cars. In the past, both companies
have independently hired programmers to create an application
in Java to manage their administration. Both applications run without
problems and satisfy their owners.
At the current moment however, CarPleasure and DirectCar have decided
to team up and merge into one company. The obvious wish of the
company is to merge both administration applications as well. They
hire you to perform this task.
When you inspect the systems, you discover that in both applications
only the .class les are present. Unfortunately, since the original programmers
have escaped respectively to the Gobi Desert and to Malapascua
(a small island in The Philippines), the source les are not available.
All documentation is buried in time.
The company gives you only very limited time to complete your task,
so designing a new system from scratch is not an option.
Afew classes of both mentioned applications can be downloaded from Blackboard as .class les.
1-B.1 ClassInspector
Create a small application ClassInspector that inspects unknown classes using reection2. It
should display all possible information about a class such as its elds, constructors, methods and
facts about inheritance. If elds are declared with a value, this value should as well be displayed.
Use your ClassInspector to restore class diagrams3 of the given classes of the companies and
include the diagrams in your report.
1-B.2 Particle
Since the structure of the classes Part and Article is quite similar, it should be possible to use
them in one application. However, the only superclass that they have in common is Object.
Design a new class Particle to solve this problem. Particle should have a constructor
public Particle(Object item) in which item can be an instance of either Part or Article.
The constructor should check this. Particle should provide the same functionality as both
classes. You should use reection where needed.
1-B.3 ProcessAll
Extend your program of (a) with a method processAll(..., ...) which applies a method
(including its possible arguments) to all elements in a given GenericSet, and returns the result
2You are basically starting to make your own reverse engineering tool here, wow!
3See also the introduction for information about class diagrams.
2
as a new set. Make sure you catch all relevant exceptions and handle them appropriately.
1-B.4 Admin
Create a simple class Admin which contains a set of Particles.
Implement a method changePrice(double fraction) which changes the price of all articles
according to the given fraction4. (If you are smart you make use of your work of the rst parts of
this exercise, of course.)
(Make sure to hand in your solution before the deadline published on Blackboard! Being late involves a
severe penalty on your grade. See the in4023 introduction for all the details.)
4For example fraction = 1.20 means the articles become 20 % more expensive.
3
GenericSet specication
GenericSet initialize
Returns the empty set.
GenericSet insert(GenericSet z, Item i)
The element i is added to z and returned in a new set.
Note: Set elements are to be unique.
GenericSet delete(GenericSet z, Item i)
If element i belongs to z, it is removed from z and the resulting set is returned.
boolean member(GenericSet z, Item i)
If the item i is a member of z , the value true will be returned; the value false otherwise.
boolean equal(GenericSet z1, GenericSet z2)
Atrue value will be returned if z1 and z2 are equal; that is, if they contain the same elements.
boolean empty(GenericSet z)
If z is an empty set, then the value true will be returned.
GenericSet union(GenericSet z1, GenericSet z2)
Returns the union of two sets z1 and z2.
GenericSet intersection(GenericSet z1, GenericSet z2)
Returns the intersection of two sets z1 and z2.
GenericSet difference(GenericSet z1, GenericSet z2)
The resulting set will contain those elements that are in z1, but not in z2.
GenericSet assign(GenericSet z)
z will be assigned to the resulting set.
boolean subset(GenericSet z1, GenericSet z2)
If z1 is a subset of z2, a true value is returned, otherwise a false value is returned. z1 is a
subset of z2 if every element of z1 is also an element of z2.
int cardinality(GenericSet z)
Returns the number of elements in a set. If the set is empty: cardinality(z) = 0.
All of the methods have to be static. Make sure each of the methods leaves its arguments
unchanged! Therefore, after the call z3 = union(z1, z2), both z1 and z2 should be the same as
before the call.
4