Description
Objectives:
Practice java programming with classes and objects, constructors, copy
constructors, enum type, array, ArrayList (generic version), overloaded
methods, passing by reference and etc.
Task (4 marks)
In mathematics, a set is a collection of distinct elements and elements in a set are not
in order. Here are some examples of sets:
(a) A set of integers, e.g. integerSet = {3, 1, 4, 2}
(b) A set of fruits, e.g. fruitSet = {apple, orange, papaya}
(c) A set of characters, e.g. charSet = {‘A’, ‘m’, ‘&’}
We always enclose elements of sets inside a pair of { }.
Here are some other properties on sets:
(1) A set can be empty, i.e. no element. We call it empty set. In mathematics, we have
a special symbol to denote empty set. Convenient to our design later, we will use { }
to denote an empty set.
(2) When checking an element is inside a set. We call it “belong to”.
(3) If a set contains in another set, we call it subset. For example, {1, 2, 3} is a subset
of {2, 3, 4, 6, 1}. Therefore empty set is a subset of every set.
(4) The cardinal number of a set is the number of elements in a set.
2
(5) The union of two sets A and B are all the elements belong to A and B, minus the
duplications. For example, if A = {1, 2, 3} and B = {2, 3, 4, 5}, the union is {1, 2,
3, 4, 5}.
(6) The intersection of two sets A and B are the common elements of A and B. Using
the example quoted in (5), the intersection is {2, 3}.
(7) The equality of two sets A and B are all the elements of A are in B and all the
elements of B are in A. Or alternatively, A is the subset of B and vice versa.
(8) The difference of two sets A and B, for example A – B, is those elements in B
should not be in A. For example, if A = {1, 2, 3} and B = {2, 3, 4, 5}, A – B = {1}
and B – A = {4, 5}.
We have all the required properties for our task.
In this assignment, our universal set is the 12 enumeration constants of calendar
months. They are
Jan, Feb, Mar, …, Dec
Data validation is not necessary in this design, when “I” want to add in a month, you
can assume that this is a valid month in the enumeration constants; i.e. we have
restricted ourselves in a subset of the above 12 months.
The best way to test your design is to develop an educational system to teach some
basic set theory. All the sets used in your design should be randomly generated by the
system i.e. the sizes (also known as cardinal number, from 0 to 12 elements) and the
elements.
Let us explore the following UML diagram for the whole task:
3
Let us look at each of the classes
(a) Enumeration class Month
This is the universal set that a subset is constructed. Each enum constant has two
descriptions: an integer to denote the month, for example 1 for Jan; and a string to spell
out the month in full, for example, January for Jan
(b) Class Set
We use an array list to represent a set, which is an instance variable defined inside the
class. The normal set operations: belong to, contains, union, intersection, complement,
difference, subset, equality are some of the set’s operations. The toString method
returns a String of enum constants enclosed between braces; getString_1 returns its
equivalent in integers for elements of set and getString_2 returns its equivalent in string
objects for elements of set.
(c) Main class
We now ready to present the whole system. You are required to design an educational
system to teach basic set theory. We propose the following interactions for your system:
When you execute your program, the system will display the info for the universal set
(a call to displayMonthInfo method, display once only) and followed by a menu
(the display of menu is repeated after an operation)
4
When you enter the option 0, you will see the following interactions:
A set is generated and is displayed. You can now try a few simple set operations, add
an element, belong to operation, display the cardinal number and various ways of
displays. Note that the sub-menu will be repeated after each operation.
5
Let us enter the option 1 in the submenu,
You can see in the above interactions, adding an element which is already inside the set,
the final set remains unchanged; otherwise this distinct element is added to the set.
Let us explore option = 2
For option 3, the system just simply displays the cardinal number:
6
For option = 4:
You can continue to stay in the submenu or enter 9 to go back to the main menu.
Let us choose 9 to go back to the main screen to test other operations.
Before returning to the main menu, as all the sets used in this assignment are
dynamically created, you should help the compiler to do some garbage collections.
Let us now explore each of the options in the main menu:
In the main menu, you choose option 1:
7
In option 1, the system randomly generates two sets and displays the union of these two
sets.
The same is done for option 2, but evaluate the intersection of the two sets:
In the above screen shot, you see the notation of an empty set. Let us see another screen
shot for some non-empty intersections:
For option 3, the subset operation:
8
For option 4, the difference of two sets;
Now, option 5, the complement of a set is done with the universal set. Our universal set
is the set of calendar months. The following shows some of the interactions and
displays:
Option 6 is the set equality. The following shows some of the possible interactions and
display:
9
IMPORTANT TO NOTE
IMPORTANT
Put all your classes in a file called YourName_A1.java and make sure
that this file can be compiled and can be executed. Upload ONLY this file
to Moodle. ALL ZIP FILE SUBMISSION WILL BE REJECTED
No re-submission will be allowed after grading.
In the above file, remember to put down your name and also the following
declaration (some similar contents):
// Tell me if it is your own work, and whether you have passed your
// program to your friends etc etc etc
// and willing to accept whatever penalty given to you.
– Wrong file name -0.5 mark
– No declaration, no name etc -0.5 mark
– Failing to demo -1 mark
– Programs indentations and alignment of statements -0.5 mark
– Late penalty: -0.1 mark per hour