0% found this document useful (0 votes)
157 views5 pages

HCT216: Programming 2: Assignment 2: Methods, Encapsulation, Inheritance & Polymorphism, Exceptions

This document provides details for Assignment 2 of the HCT216 Programming 2 course. It includes a case study on a college management system and questions to design classes to model the system. Students must submit their source code and compiled code as a zip file by May 13th, with late submissions penalized. Comments are required to label code answering specific questions. Non-code answers should be on a separate document with clear labeling. The case study models students, courses, and different student types (full-time and part-time). Questions involve designing classes for these with attributes, relationships, methods and polymorphism to calculate fees.

Uploaded by

munashe chibaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views5 pages

HCT216: Programming 2: Assignment 2: Methods, Encapsulation, Inheritance & Polymorphism, Exceptions

This document provides details for Assignment 2 of the HCT216 Programming 2 course. It includes a case study on a college management system and questions to design classes to model the system. Students must submit their source code and compiled code as a zip file by May 13th, with late submissions penalized. Comments are required to label code answering specific questions. Non-code answers should be on a separate document with clear labeling. The case study models students, courses, and different student types (full-time and part-time). Questions involve designing classes for these with attributes, relationships, methods and polymorphism to calculate fees.

Uploaded by

munashe chibaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

HCT216: Programming 2

2019 Semester 1

Assignment 2 : Methods, Encapsulation, Inheritance & Polymorphism, Exceptions

Due Date & Time: Monday, 13 May 2019 23:59:59.

Submission format: Document to be uploaded on Tsime. Submit both source code and compiled
code. All files must be zipped into one ZIP file. Late submissions will be penalised.

Notes
1. You are expected to carefully study the case study and answer all questions that follow in
order to implement a solution for (part of) it using Java code. It is recommended to read the
whole case study and all the questions before you attempt the questions; to ensure that you
understand the full context of the problem and required solution.
2. Ensure that your code has been compiled and run on a computer and is producing the
correct results before submitting it. An IDE like Eclipse may be used for this assignment.
3. Use Java comments to label your code that answers specific questions, e.g. if a question "3
(a)" is asking you to write a method, put a comment with // 3 (a) before the method.
4. Answers to questions that are not code must be on a separate document; and they must be
clearly labeled.

Case Study
A College Management system is for managing students and other aspects of college enrolment. The
system manages students, who have details like name, registration number and date of birth. Each
student selects a number of courses to register for at the start of the semester. Each "Course" has
details like course code, course fee (the amount of money the student must pay for that single
course), and semester code. The total fees paid by students include the total of all course fees for
their Courses. There are two types of students; fulltime and part-time students. Part-time students
have their own unique characteristic / attribute called "parttime type" which indicates the time they
learn, e.g. "weekend", "night" or "block release". Fulltime students have their own unique attributes
called "residence type" and "residence hall code"; which store information about where the
students stay.

1 of 5
Questions

1. Packages and Classes

a) Draw a sketch of the class diagram of the case study, to assist you in having an
understanding of the case study. Please do this only after reading through the whole case
study and all its questions at least 2 times. [2 marks]

b) Course class: Write code for a "Course" class in the “courses” package with information
including: course code, course fee and semester code. Choose the appropriate names and
data types for your class attributes. Ensure correct access protection of class members.
Include getters and setters for all attributes. [5 marks]
In this Course class, also add a constructor that takes all attributes in the class as
parameters; and also write the logic that sets the parameter values to the attributes. [2
marks]

c) Students class:
i. Write code for a Student class in the “students” package with information including:
name, registration number and date of birth. Choose the appropriate names and data
types for your class attributes. Also add an attribute that uses an appropriate data
structure to represent the many "Course" objects that belong to a Student (i.e. since a
Student can register for many courses). The Student class must be such that no instances
of it can be created; but instances can be created only for the classes that represent the
different types of students, that is part-time and fulltime. Ensure correct abstraction and
access protection of class members. Include getters and setters for all attributes. [7
marks]
ii. Write code for a concrete instance method called "calculateFees" inside the Student
class. The method must be implemented in the Student class. It must return the fees
that a student must pay, by simply using logic that adds all the "course fees" for all the
"courses" that the student has registered for. Basically the amount of fees that a student
must pay is a sum of all course fees of the course objects that belong to that student.
Your code must correctly implement this logic. [4 marks]

d) Part-time Student class: Write code for a "Parttime Student" class in the “students” package
that has the appropriate relationship with the Student class so that it has reuses members of
the Student class; and also has an additional attribute "parttime type" which is unique to
only parttime students. Choose the appropriate name and data type for the attribute.
Ensure correct access protection of class members. Include getters and setters for all
attributes. [5 marks]

e) Fulltime Student class:

2 of 5
i. Write code for a "Fulltime Student" class in the “students” package that has the
appropriate relationship with the Student class so that it has reuses members of the
Student class; and also has an additional attributes "residence type" and "residence hall
code" which are unique to only fulltime students. Choose the appropriate names and
data types for the attributes. Ensure correct access protection of class members. Include
getters and setters for all attributes. [5 marks]
ii. Residence fees are 20% of the total fees calculated from course fees. In this question,
you are supposed to ensure that residence fees are added to the fees for Fulltime
students. Inside the "Fulltime Student" class, override the method "calculateFees" of the
Student class. Its logic must simply call the calculateFees method of the Student class
and then add the residence fee (20% of total course fees) on top of course fees; and
then return the new result as the fees. [4 marks]

[34 marks]

2. Constructors
a) Create a constructor for the Student class; which has the following parameters: name,
registration number, date of birth. [3 marks]

Try to compile all your classes again. What errors do you observe? Why are those errors
appearing? How can you fix them? Don't fix them yet!! [2 marks]

b) Create constructors for both the FulltimeStudent and ParttimeStudent classes. They must
have the same signature as the constructor of their superclass; but ensure they have an
empty body (i.e. no single line of code). [2 marks]

Try to compile all your classes again. What errors do you observe? Why are those errors
appearing? How can you fix them? Don't fix them yet!! [2 marks]

c) Inside the constructors created in (c) above; add a single line of code that ensures that the
constructor initialises the instance by simply calling the constructor of the superclass classes.
[2 marks]

Compile all your classes again. The compile errors should then go away. [2 marks]

[13 marks]

3. Methods and Polymorphism


a) Write code for a College class in the “college” package, which has the following:

3 of 5
i. Add a class method called "printStudentNames" which does not return anything; but
must have a single parameter: a data structure that represents a collection of Students.
Choose an appropriate data structure class that can store a collection of students of any
type; whether part-time of fulltime. The method, whenever it is called, must print all the
names of the students and their types, one per line, e.g “John Doe (full time)”. The
method must use the "for ... each" loop construct to achieve this. [5 marks]
ii. Add a "main" method to the College class which will be run whenever the class is run.
The method must have a correct "main" method signature. [1 mark]

Inside the main method, write the following code (questions iii to vi):

iii. Instantiate two (2) Course objects and set any values of your choice to their attributes.
Name these variables "cs1" and "cs2". Instantiate a Part-time Student and set any values
of your choice to its attributes. Set values for all attributes of this part-time student,
including attributes in the Student class. Also associate the 2 course objects cs1 and cs2
with the created part-time student object, i.e. make the course objects cs1 and cs2
belong to the part-time student. [4 marks]
iv. Instantiate two (2) Course objects and set any values of your choice to their attributes.
Name these variables "cs3" and "cs4". Instantiate a Fulltime Student and set any values
of your choice to its attributes. Set values for all attributes of this fulltime student,
including attributes in the Student class. Also associate the 2 course objects cs3 and cs4
with the created fulltime student object, i.e. make the course objects cs3 and cs4 belong
to the fulltime student. [4 marks]
v. Create a collection of Student objects (of the same data structure or class type as the
parameter for method "printStudentNames" in (i). Add the two students; one created in
(iii) and the other created in (iv) to the collection of Students. [2 marks]
vi. Write a line of code that calls the method "printStudentNames" by passing the collection
of Students in (v) to it. [1 mark]
b) Write code for a method called "calculateStudentFees" in the College class. The method
must have a single parameter: a collection of Students with same type as the one used in (i).
The method must return the total fees for students in the collection that has been passed
into the method as an argument. Use a "while ... " loop construct for you logic. Add a line in
the main method of the College class that calls this method using the collection created in
(a) (v). [5 marks]

[22 marks]

4. Exceptions and Other


a) Create a new checked exception called "InvalidCoursesOperationException" in the
“students” package. [4 marks]

b) Add a method called “removeCourse” in the Student class. The method should take a single
Course object and return nothing. The logic of the method should be such that when called,

4 of 5
it will look for a course with the course code as the course passed as an argument and
remove it from the courses being taken by the student. The method must throw the
“InvalidCoursesOperationException” exception if a course with the course code as the one
passed as an argument is not found in the courses that are currently being taken by the
student. [5 marks]

c) Instantiate a Course object called cs5 and set any values of your choice to their attributes,
but making sure that the course code used in this case is different from any of the course
codes used. Add code in the main class “College” that calls the method “removeCourse”
method on the fulltime Student object instantiated earlier in question 3 (a) (iv); while
passing the Course object cs5. Compile the College class and do what is necessary for it to
compile successfully. Run the College class and observe what happens. [7 marks]

d) Comment out the lines of code written in question 4 (c) above that calls the “removeCourse”
method. Add code in the main class “College” that calls the method “removeCourse”
method on the fulltime Student object instantiated earlier in question 3 (a) (iv); while
passing the Course object cs4 created in the same question. Compile the College class and
do what is necessary for it to compile successfully. Run the College class and observe what
happens. [4 marks]

e) Add Java class documentation that will form part of "Java docs" for all your code. For every
Java class, method and constructor that is in this assignment, write a single documentation
line that briefly describes what the class does. Now use the tool for creating Java docs for all
your classes; and ZIP the HTML Java docs created for your classes and include the ZIP file in
your submission. The documentation must be a set of HTML files that can be opened so that
what you have documented can be read just like the official “Java docs”. [8 marks]

f) Bonus Question: Print out a Student object like "student1" object by using
System.out.println(student1). What output do you get? Is it meaningful? In the class
Student, How can you override the method "toString()" in the Object class (check it out in
Java docs) to ensure that you can be able to easily and meaningfully print out an object of
the Student class so that it produces more friendly output like “John Doe (full time)”?
Overwrite the method and add a line that prints any of the student objects. [3 marks]

[31 marks]

[100 marks]

5 of 5

You might also like