User Defined Concrete Classes Exercises
User Defined Concrete Classes Exercises
Practice Exercises
Question 1:
Create a new NetBeans project called MemoryApp with a main class.
Create a new Java Class called Memory to the MemoryApp project and complete it as
described below.
Description:
Your life is full of memories that is either a good (‘G’) or bad (‘B’) memory and a memory
usually lasts several days, in our situation, before it starts to fade. A memory can be seen as
an object, so it is expected of you to create the class called Memory by following the UML
class diagram (Table 1.1) and the description (Table 1.2).
Memory
‐ status: boolean
‐ type: char
‐ description: String
‐ lasting: int
+ Memory(char , String)
+ Memory(char , String , int)
+ setLasting(int): void
+ setStatus(boolean): void
+ getStatus(): boolean
+ getType(): char
+ getDescription(): String
+ getLasting(): int
+ fading(int): void
Table 1.1: UML Class Diagram (Memory)
Attribute/Method Description
status Instance data members
type
description
lasting
Memory(char , String) Parameterized constructor that initializes the memory type (good/bad) to the first
parameter value and the description of the memory with the second parameter
value. A memory by default last for only 100 days and it will be active once it is
created.
Memory(char , String, int) Parameterized constructor that initializes the memory type (good/bad) to the first
parameter value and the description of the memory with the second parameter
value. The third parameter will initialize the number of days to memory is set to
last and the memory will be active when created.
setStatus(boolean) Mutator methods
setLasting(int)
getStatus() Accessor methods
getType()
getDescription()
getLasting()
fading(int) As time goes by, memories start to fade. The purpose of the method is to let the
memory fade away. The method will receive as a parameter the number of days
with which the memory should fade, meaning updating the lasting time of the
memory.
Note that a memory cannot have a negative lasting time. Once a memory reach 0,
the memory is not active anymore. Be sure to update the status of the memory
once it reached 0 lasting days.
Table 1.2: Class description (Memory)
In the main() method of the MemoryApp class, complete the following steps:
Create three instances of the Memory class by allowing the user to enter the required
information of each Memory and displaying the details of each memory and let each
memory fade 20 days.
For one of the instances you should use the first parameterized overloaded
constructor and for the second instance use the second constructor to instantiate the
objects.
Question 2:
Create a new NetBeans project called IceCreamApp with a main class.
Create a new Java Class called SteersIceCream to the IceCreamApp project and complete it
as described below.
Create a concrete class called SteersIceCream that represents an ice‐cream you can buy at
Steers. Follow the provided UML class diagram and description below to complete this class.
SteersIceCream
‐ holder: char
‐ size: char
‐ flake: boolean
‐ dipSauce: char
+ SteersIceCream()
+ SteersIceCream(char)
+ SteersIceCream(char, char)
+ SteersIceCream(char, char, boolean, char)
+ setHolder(char): void
+ setSize(char): void
+ setFlake(boolean): void
+ setDipSauce(char): void
+ getHolder(): char
+ getSize(): char
+ getFlake(): boolean
+ getDipSauce(): char
+ price(): double
+ details(): String
Component Description
holder Instance data members.
size
flake
dipSauce
SteersIceCream() The default constructor will initialize the data members to
the following default values:
The standard SteersIceCream comes in a cone (‘c’), is the
general size (‘g’), have no flake (false), and no dip sauce
(‘n’).
SteersIceCream(char) The parameterized overloaded constructor will initialize the
size data member to the parameter value.
By default the holder of the ice‐cream is a cone (‘c’), no
flake (false), and have no dip sauce (‘n’).
SteersIceCream(char, char) The overloaded parameterized constructor will initialize the
size data member to the first parameter, and the holder
data member to the second parameter.
By default the ice‐cream have no flake (false) and have no
dip sauce (‘n’).
SteersIceCream(char, char, boolean, char) The overloaded parameterized constructor will initialize the
size data member to the first parameter, and the holder
data member to the second parameter. It will also initialize
the flake to the third parameter and the dipSauce to the
fourth parameter.
setHolder(char) Mutator methods.
setSize(char)
setFlake(boolean)
setDipSauce(char)
getHolder() Accessor methods.
getSize()
getFlake()
getDipSauce()
price() The method is used to calculate and return the price of the
ice‐cream considering the different options available for the
ice cream.
The size of an ice‐cream is either:
‘g’ for general and the price is R5.50.
‘m’ for medium and the price is R7.65.
‘l’ for large and the price is R10.45.
The holder of an ice‐cream is either:
‘c’ for a cone and the price is R1.25.
‘C’ for a cup and the price is R2.75.
Add R2.00 to the price if the user wanted a flake and add
another R1.75 if the user chooses a dip sauce.
details() The method will return a concatenated String with the
details of the SteersIceCream object, including stating the
price.
The dip sauce can either be:
‘n’ for no dip sauce
‘c’ for chocolate sauce
‘C’ for caramel sauce
In the main() method of the IceCreamApp application class called complete the following
steps:
a. Create the first SteersIceCream object called iceCream1 with the default values.
Display the details of the object.
b. Create the second SteersIceCream object called iceCream2 with the default
values but allow the user to choose the size of the ice‐cream. Display the details
of the object.
c. Create the third SteersIceCream object called iceCream3 with the default values
but allow the user to choose the size and let the holder be a cup. Display the
details of the object.
d. Create the fourth SteersIceCream object called iceCream4 that is large and
comes in a cup, and it has a flake. Let the user choose the dip sauce for this ice‐
cream. Display the details of the object.
Question 3:
Create a new NetBeans project called AssessmentApp with a main class.
Create a new Java Class called Assessment to the AssessmentApp project and complete it as
described below.
Create the following concrete class called Assessment as shown in the UML class diagram
and description table below.
Assessment
‐ code: String
‐ type: char
‐ weight: double
+ Assessment (String)
+ Assessment (String, char, double)
+ setType(char): void
+ setWeight(double): void
+ getCode(): String
+ getType(): char
+ getWeight(): double
+ levelOfDifficulty(): String
Component Description
code The three instance data members represent the code of the assessment (CT1,
type ST2, ASS3), the type of assessment which can be controlled (c), open (p), online
weight (o), or group (g). An assessment will also contribute to the final mark for the year
which is indicated by the weight instance data member.
Assessment (String) Parametrized constructor will initialize the code instance data member with the
parameter value. By default is all assessment controlled assessment.
Assessment (String, char, Overloaded parameterized constructor that will initialize the three instance data
double) members with the three parameter values.
setType(char) Mutator methods
setWeight(double)
getCode() Accessor method.
getType()
getWeight()
levelOfDifficulty() Each assessment will have a level of difficulty. These levels can be “EASY”,
“MODERATE”, or “DIFFICULT”.
The level of difficulty is determined by the weight the assessment contributes as
well as the type of the assessment.
Any assessment that contributes 10% or less will be seen as an EASY assessment.
An assessment that contributes more than 10% but 40% or less will be classified
as EASY if it is a group or open type of assessment. But will be classified as
MODERATE if online type of assessment and DIFFICULT if controlled assessment.
An assessment that contributes more than 40% will be classified as EASY if it is a
group assessment. It will be classified as MODERATE if an open assessment but
will be classified as DIFFICULT if an online or controlled assessment type.
The method will determine and return the level of difficulty.
In the main() method of the AssessmentApp application class complete the following tasks:
Create the first Assessment object using the constructor with the one parameter and
mutator methods with the following values:
o Code is CT1 and the weight is 15% and it is an online assessment.
o Display the code and the level of difficulty for this assessment.
Create the second Assessment object also using the constructor with the one
parameter and mutator methods with the following values:
o Code is CT2 and the weight is 35% and it is group assessment.
o Display the code and the level of difficulty for this assessment.
Create a third Assessment object using the constructor with three parameters with
the following values:
o Code is ST1 and the weight is 45% and it is a controlled assessment.
o Display the code and the level of difficulty for this assessment.
Create a final Assessment object using the constructor with three parameters and
the following values:
o Code is ASS1 and the weight is 5% and it is a group assessment.
o Display the code and the level of difficulty for this assessment.
Question 4:
Create a new NetBeans project called TutorApp with a main class.
Create a new Java Class called TutorialSession to the TutorApp project and complete it as
described below.
Students who passed a subject in the faculty of ICT at TUT can enrol as a mentor for the new
students the following semester. As a mentor the student will present tutorial sessions and
will get paid based on the number of students that attended the session as well as the
duration of the session.
It is expected of you to create a class called TutorialSession by following the UML class
diagram (Table 1.1) and the description table (Table 1.2).
TutorialSession
‐ mentor: String
‐ topic: String
‐ subjectCode: String
‐ duration: short
‐ numOfStudents: short
‐ repeatedSession: boolean
+ TutorialSession(String, String, String)
+ TutorialSession(String, String, String, boolean)
+ setDuration(short): void
+ setNumOfStudents(short): void
+ getDuration(): short
+ getNumOfStudents(): short
+ getRepeatedSession(): boolean
+ calculateCost(): double
Table 1.1: UML Class Diagram
Attribute/Method Description
mentor With a tutorial session there must be a topic that is presented by a
topic mentor for a specific subject (subjectCode).
subjectCode The session will take a number of minutes (duration) and will be
duration attended by a number of students (numOfStudents).
numOfStudents It is also possible that the mentor is repeating this topic which must be
repeatedSession stated (repeatedSession).
These are all instance data members.
TutorialSession(String, String, String) The overloaded parameterized constructor will receive the student
number of the mentor as well as the topic and the subject code as
parameter values to assign to the corresponding instance data
members. By default it is the first time the session is being presented
by the mentor and any session will be at least 30 minutes.
TutorialSession(String, String, String, The overloaded parameterized constructor will receive the student
boolean) number of the mentor as well as the topic and the subject code as
parameter values to assign to the corresponding instance data
members. The 4th parameter will state if it is the first time the topic is
being presented. By default, all tutorial sessions should be at least 30
minutes.
setDuration(short) Mutator methods.
setNumOfStudents(short)
getDuration() Accessor methods.
getNumOfStudents()
getRepeatedSession()
calculateCost() The mentor must get paid for the tutorial session. This method will
calculate and return the cost of the tutorial session.
The cost of a tutorial session is based on the number of students that
attend the session as well as the duration of the session. See the 2
tables below for these specifications.
Number of Students The cost
10 or less R12.35 per student
11 to 15 Additional R50 to 10 or less
students cost
More than 15 Fix rate of R250.00
Duration Additional Cost
Less than 60 min Add R60 to cost.
Or 60 to 90 min Add R100 to cost.
Or longer than 90 min Add R125 to cost.
Lastly, if it is a repeated session, then the cost will be 35% less than the
original calculated cost.
Table 1.1: Class description
In the main() method of the TutorApp application class complete the following tasks:
Making use of the two constructors, create 5 TutorialSessions objects that is
presented by you, the student.
Make use of random numbers (40 to 100) for the duration of the sessions as well as
for the number of students that is attending the sessions (5 to 20).
You must also generate random values for the status of each session if it is repeating
or not.
This means you will the user will only enter the topics and subject codes of each
session.
Display the cost of each session as well as the total cost of all the sessions.