This one hour, 2022 OOP exercise comes from an exam paper set by M Walker and moderated by H Peuckert and R Viljoen from the Pretoria 480 IEB IT cluster. The mark allocations are also shown.
Concepts in this exam. Reads from a text file. Creates an array of objects. Instantiates both parent and child objects. Sorts the array. Queries the array for information. Updates the array. Displays the updated array
Click here to download the exam paper complete with additional notes and guidelines
Scenario
The Winter Olympic Games organizing committee needs a program to manage the volunteers assisting them during the games. Some volunteers are only going to be involved in meet and greet and tour guide roles in specific areas, whilst others will be on the courses in safety roles based on their medical experience and fitness level.
Below are the UML diagrams (class diagrams)
NOTE: The Safety class (the sub class) must inherit from the Volunteer class (the super class)
Volunteer |
– name: string – surname: string – dob : date – area : string |
+ Constructor(n:string, s:string, dob:date, a:string) + getSurname(): string + getAge():integer + getArea():string + setArea(area:string) + toString():string |
Safety |
– experience: integer – fitnessLevel: integer – role: string |
+ Constructor(n: string, s: string, dob:date, a:string, ex: int, fL:int, role:string) + getExperience():integer + getFitness():integer + toString():string |
There is one text file that has a variable number of data items per row of data depending on the type of object is being represented in that row. All objects have the same first four data items, being the name;surname;dob;area. If the row has 7 data items in it, it is a Safety object being represented and the parts are name;surname;dob;area;fitnessLevel;experience,role
The dob is stored in the format yyyy-mm-dd
Sample of 1st 5 rows of data in the text file:
Nkosinathi;Modiba;1982-11-30;Stadium;7;6;Doctor
Duduzile;Shezi;1967-08-05;Athlete village
Juan;Thompson;1986-04-17;Ski Lodge
Siphokazi;Mokone;1982-10-02;Ski Jump;10;7;Paramedic
Tsholofelo;Sibeko;1973-04-22;Stadium
Click here to download the volunteer text file.
Bare Bones Approach – each class is almost empty – has the least amount of code possible – but your program runs.
- Carefully read (only read) question 4 that deals with the UI class (the one that has the main method). Do this for every exam. Always know what is in the main class before you code the methods and template classes.
- Create the UI class (VolunteerUI) – Q4, but at this point it must be mainly empty.
- Create the manager class (VolunteerManager) – Q3 – mainly empty.
- Create the two template (object) classes (Volunteer and Safety) (both empty) – Q1 and Q2
- Create the manager object in the UI class.
- Make a dummy text file from the provided text file. It is a dummy file because it only has the first record.
- Create the constructor in the manager class that successfully opens the dummy file for reading (only – no while loop yet)
- Compile and run. Now that you have a working framework you can “begin” the exam.
- Now turn your attention to the exact requirements in the exam
- Once your program is pretty much running, move back to the provided text file with its irregular number of data tokens.(some lines have 4 tokens while others have 7)
- Progressively code the UI class (question four) as you code questions 1,2 and 3. Question 4 in this exam is purely academic and your solution should already exist once you have completed question 3.
Question 1 (18)
Using the UML diagram (class diagram) above for the Volunteer class above complete the following instructions:
1.1 Create a new class called Volunteer with name, surname, dob and area fields as indicated in the above class diagram. (4 marks)
1.2 Create a constructor method that accepts a string n as a parameter representing the name field, a string s representing the surname field, a parameter dob representing the dob field and a parameter string a representing the area field. Use these parameters to assign values to the field. (3 marks)
1.3 Create accessor/get methods for the surname and area fields. (2 marks)
1.4 Create the accessor/get method getAge() that returns an integer representing the current age in years of the volunteer according to the current date. No dates are to be hardcoded into the class. (4 marks)
1.5 Create a mutator/set method setArea to set the area to the string area parameter received. (1 mark)
1.6 Code a toString() method to return the fields of a Volunteer as a string in the following format:
Name: name<space>surname<tab>Age:<space>age
Area:<space>area
For example
Caroline Brits Age: 23
Area: Ski Jump (4 marks)
Click here for the memo for question one
Question 2
Using the UML diagram for the Safety class that inherits from the Volunteer class, complete the following instructions:
2.1 Create a new class called Safety that inherits from the Volunteer class with experience, fitnessLevel and role fields as indicated in the class diagram. (5 marks)
2.2 Create a constructor method that accepts the parameters string n, a string s, date dob, string a, and integer ex representing experience, integer fL representing Fitness Level and parameter role representing the role of the person. Use these parameters to assign values to the field. (3 marks)
2.3 Create accessor/get methods for the experience and fitness fields as per UML diagram. (2 marks)
2.4 Code a toString() method to return the fields of a Safety object as a string in the following format:
Name: name<space>surname<tab>Age:<space> age
Area:<space>area <tab>Role:<space>role
For example
Nkosinathi Buthelezi Age: 34
Area: Rink Role: First Aid (2 marks)
Click here for the memo for question two
Question 3
3.1 Create a new class called VolunteerManager with two private fields as follows:
• An array of Volunteer objects called vol with enough capacity to store 50 Volunteer objects.
• An integer variable to count how many objects have been read in from the text file. (4 marks)
3.2 Write code for the constructor method of the VolunteerManager class that accepts a string parameter that holds the name of the text file. The constructor method will read the contents of the text file. One line is used to store information for each volunteer. A Volunteer object has four fields name, surname, dob and area. A Safety object has an additional three fields – fitnessLevel, experience and role.
• Open the file for reading.
• Loop through the file to read in the lines of information and create the appropriate Volunteer or Safety object and add the object to the array called vol. (13 marks)
Click here for the memo for 3.1 and 3.2
REMINDER:
A typed method returns a value – A void method does not return a value
3.3 Create a typed method/function named listAll() in the VolunteerManager class that will return a string containing all the Volunteer objects in the Volunteer array called vol. The Volunteer objects should be included in the format of the toString methods of the objects with each volunteer’s information being separated by an open line. (4 marks)
Click here for the memo for 3.3
3.4 Determine the average age of every volunteer assigned to the “Ski Jump” area. Create a typed method / function named aveAge in the VolunteerManager class that will receive as a parameter the string “Ski Jump” and returning the average age of the Ski Jump volunteers. (10 marks)
Click here for the memo for 3.4
3.5 It is decided to change the area of work for all those in the Safety category (only). They are to be divided up according to their experience and fitness level.
• The new areas of work for those with more than 3 year of experience is as follows – “Holding Area”, “Top Zone” and “On Course”. They are further divided according to their fitness level. Less than 5 is “Holding Area”. More than 10 is “Top Zone”. All others are allocate to area called “On Course”.
• For those with less than 3 years of experience the new area is “Medical Tent”. Their fitness level is not taken into consideration.
Code a void method / procedure called changeArea in the VolunteerManager class that will change the area of work for all the Safety objects stored in the vol array according to the criteria as described above. (8 marks)
Click here for the memo for 3.5
3.6 Write a method called sortArray in the VolunteernManager class to sort the array according to the surname field. This must be a private helper method that the constructor can use once the whole text file has been saved into the array.
Click here for the memo for 3.6
Question 4
4.1 Write code to create a text-based user interface called VolunteerUI that will allow simple output. (1)
4.2 Create a VolunteerManager object using the appropriate method that will parameter pass the name of the text file “Volunteers.txt”. (1)
4.3 Write code to call the sort method in the VolunteerManager class. (1)
4.3 Write code to display all the volunteers’ information. (1)
4.4 Write code to display the average age of the volunteers at the Ski Jump area. (1)
4.5 Write code to change the area of the Safety objects. The list has to be displayed again. (2)
(7 marks)
TOTAL MARKS: 75