OOP216D - OOR216D FA2 2024S1 Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

FA2

FACULTY: INFORMATION & COMMUNICATION TECHNOLOGY


SUBJECT: OBJECT-ORIENTED PROGRAMMING
OOP216D AND OOR216D
Instructions
• Please login to EC to indicate your attendance for this
assessment (signing the class list). By signing the class list, you
agree that you understand and will adhere to the rules and
regulations of Tshwane University of Technology.

• Submission of answers:
• Log into EC
• Select [Student], then select [Tests], then select [Web Test 4]
• Select the subject code OOP216D or OOR216D
• Select the intake 20241
• Select the test number E
• Each question will have a separate section on EC where you
FORMATIVE ASSESSMENT 2 1st Examiner should paste your answers. Answer each question in its
Duration: 180 min M LIEBENBERG
correct section.
Date: 2 May 2024
• Copy your answers from your source files and paste it in the
Total: 82 Moderator
provided spaces and click “GO” every time you are done. By
Pages: 7 V MEMANI
clicking on “GO” you are actually saving your answer for that
question. DO NOT forget to click “GO”.
• Save your work frequently.
• This is a closed book test. NO additional resources, like the
“Debugging is twice as hard as writing the code in the first place. Internet or EC may be used for help.
Therefore, if you write the code as cleverly as possible, you are, by • You may NOT use any flash sticks during the test.
definition, not smart enough to debug it.” • Add your name as a comment on the first line in each file.
Brian W. Kernighan

1
Question 1 (Inheritance): //11//

The questions:
Under section 1 of the test on EC, answer the questions related to Inheritance.

Question 2 (Abstract Class): //24//


Preparation:
1. Create a new NetBeans project called Question2.
2. Add a new Java Class called Consumption to the Question2 project and complete the class as described in Part 1 below.
3. Add a new Java Class called CarConsumption to the Question2 project and complete the class as described in Part 2 below.

Part 1 (Create an abstract class): [7]


For this question, you will create a Consumption class which is an abstract class that defines the common properties and behaviour of some sort of fuel
consumption. The class has two properties distanceTraveled and fuelConsumed which are used to calculate the fuel consumption.

The class also declares an abstract method calculateConsumptionRate(), which does not provide an implementation in the abstract class. Instead, any
concrete subclass of Consumption must implement its calculateConsumptionRate() method, which calculates the consumption rate of the vehicle, typically
measured in units like liters per 100 kilometers.

By defining an abstract class in this way, we can ensure that all subclasses of Consumption will have their implementation of the calculateConsumptionRate()
method, while also allowing us to write code that can operate on any type of Consumption without needing to know the specific subclass.

Follow the given UML class diagram (Table 2.1) and the description above and complete the Consumption abstract concrete class.

oop.consumption.Consumption {abstract}
- distanceTraveled: double
# fuelConsumed: double
+ setDistanceTraveled(double): void
+ calculateConsumptionRate(): double {abstract}
Table 2.1 UML Class Diagram (Consumption)

2
Part 2 (Create a subclass): [17]
For this question, you will create a CarConsumption class which is a subclass of the Consumption class created in Part 1.

Follow the given UML class diagram (Table 2.2) and the description table (Table 2.3) and complete the CarConsumption concrete class.

oop.consumption.carconsumption.CarConsumption
- rateDescription: String
+ CarConsumption(String, double, double)
+ getRateDescription(): String
+ calculateConsumptionRate(): double
Table 2.2 UML Class Diagram (CarConsumption)

Element of the class Description Marks


CarConsumption Packaged concrete class. 2
rateDescription This instance data member will provide the description that the car is consuming fuel. 1
CarConsumption(String, double, double) The parameterized constructor will initialize the properties of the CarConsumption object to the
parameter values. 6
It will receive the rate description, distance travelled, and the fuel consumed as the parameters.
Missing method in Consumption class There is a missing method in the abstract Consumption class that is needed to complete the
implementation of the calculateConsumptionRate() method of this CarConsumption class.
It is NOT a constructor. 4
You must go and add this method to the Consumption class before you can complete the
calculateConsumptionRate() method.
calculateConsumptionRate() This method will calculate the consumption rate of the fuel that the car used when it travelled a
4
specified number of kilometers. The consumption rate is the number of kilometers per liter of fuel.
Table 2.3 Class Description (CarConsumption)

Submitting your answers:


Follow the instructions on EC under section 2 and submit your answers to this question.

3
Question 3 (Interface): //14//
Preparation:
1. Create a new NetBeans project called Question3.
2. Add a new Java Interface called Taxable to the Question3 project and complete the interface as described in Part 1 below.
3. Add a new Java Class called PurchaseItem to the Question3 project and complete the class as described in Part 2 below.

Part 1 (Create an Interface): [6]


The Taxable interface will define the operation(s) that registered businesses in South Africa charge on taxable supplies of goods and services. The Value-
added Tax (VAT) in South Africa is currently set at 15%;

Follow the given UML class diagram (Table 3.1) and the description above and complete the Taxable interface. Indicate all the relevant modifiers for the two
elements of this interface.

oop.taxable.Taxable <<interface>>
+ VAT: double
+ addTax(double): double
Table 3.1 UML Class Diagram (Taxable)

Part 2 (Implement an Interface): [8]


For this question, you will create a PurchaseItem class that will give implementation to the capabilities as specified in the Taxable interface you created in
Part 1.

Follow the given UML class diagram (Table 3.2) and the description table (Table 3.3) and complete the PurchaseItem concrete class.

oop.purchaseitem.PurchaseItem
- code: String
- description: String
- price: double
+ PurchaseItem(String, String, double)
+ addTax(double): double
Table 3.2 UML Class Diagram (PurchaseItem)

4
Element of the class Description Marks
PurchaseItem Packaged concrete class. 2
code An item that is purchased in a shop must have a code, a description, and a price that is
description represented as instance data members in the PurchaseItem class. 1
price
PurchaseItem(String, String, double) The parameterized constructor will initialize the instance data members with the parameter
1
values.
addTax(double) This method is used to add the specified tax to the item when it is purchased. 4
Table 3.3 Class Description (PurchaseItem)

Submitting your answers:


Follow the instructions on EC under section 3 and submit your answers to this question.

5
Question 4 (Polymorphism): //33//
Preparation:
1. Create a new NetBeans project called Question4.
2. Download and extract the needFilesFA2.zip from EC.
3. Under the folder ‘Q4’ you will find the needed files for this question.
4. Add the provided BodyParts.jar file to the “Libraries” for Question4 project.
5. Remember to Edit the added library and add the extracted “javadoc" folders as the Javadoc for this library.
6. Add a new Java Class called AIRobot to the Question4 project and complete the class as described below.

The questions:
Imagine a cool robot friend that you can build piece by piece! This robot isn't made of metal scraps, but of special parts that make it look and act almost
human.

First, you might build the body, like a big, strong base. Then you can add the arms and legs, so it can move around. Next comes the head, where you put the
eyes (cameras to see) and ears (microphones to hear). Finally, you add the most important part - the brain! This brain is a super smart computer that lets the
robot talk, learn, and even feel happy or sad.

This robot buddy is kind of like you, built up one step at a time!

For this question, you will create a class that can represent such an AI robot with its different body parts. For now, we can assume that the body is already
created so that we can add the other body parts. These will include arms and legs.

For this question, it is expected of you to complete the AIRobot concrete class as shown in the UML class diagram below (Table 4.1). Follow the description of
the class (Table 4.2) and complete this concrete class.

oop.fa2.airobot.AIRobot
- name: String
- version: int
- bodyParts: ArrayList<BodyPart>
+ AIRobot(String, int)
+ addBodyPart(BodyPart): void
+ testBodyPart(String): String
+ performActions(): String
Table 4.1 UML Class Diagram (AIRobot)

6
Element of the class Description Marks
AIRobot A packaged class. 3
name Your friendly AI robot must be given a name and a version, and you will add different body
version parts (legs and arms) as you are building the robot. 3
bodyParts
AIRobot(String, int) The parameterized constructor will initialize all three instance data members. 3
addBodyPart(BodyPart) The method is used to add a specific body part to the robot. 2
testBodyPart(String) This method will receive the name of a body part to test if it is functioning. You can only test
a body part that has been added to the robot. If there is no body part as specified by the
parameter, it must just return a string with the value “NO SUCH BODY PART”.
If the body part is functioning it should create a String value with an appropriate message
11
that indicates that the specific body part is functioning and it should also indicate the action
that the specific body part is performing.
Otherwise, it should return a String value that indicates that the specific body part is not
functioning correctly.
performActions() This method is used to create a concatenated String that indicates the specific actions, all the
11
body parts can perform. Each action will be indicated on a separate line.
Table 4.2 Class Description (AIRobot)

Submitting your answers:


Follow the instructions on EC under section 4 and submit your answers to this question.

You might also like