CONFIDENTIAL CD/MAY 2024/CSC186
UNIVERSITI TEKNOLOGI MARA
ANSWER SCHEME TEST
COURSE : OBJECT ORIENTED PROGRAMMING
COURSE CODE : CSC186
SEMESTER : MARCH - AUGUST 2024
TIME : 2 HOURS
ANSWER SCHEME
DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO
This examination paper consists of 12 printed pages
CONFIDENTIAL 2 CD/MAY 2024/CSC186
PART A
QUESTION 1 (10 marks)
a) Joe’s Automotive Shop is a car servicing company that provides service quotes for
customers. Given the following scenario.
Joe’s Automotive Shop services foreign cars, and specializes in servicing cars made by
Mercedes, Porsche, and BMW. When a customer brings a car to the shop, the manager
gets the customer’s name, address, and telephone number. Then the manager checks
the brand, model, and year of the car. The customer will receive a service quotation that
includes the estimated costs for parts, labour, sales tax, and the total estimated charges.
Identify TWO possible objects from the above scenario and list three attributes associated
with each object.
(5 marks)
Answer:
Two acceptable entities/objects. 2.5m for each object with appropriate attributes.
Object (1m) Attributes (1.5m)
Car brand
model
year
Customer Name
phone Number
address
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 3 CD/MAY 2024/CSC186
b) Given the following snippet of a Java class that simulates a bank account.
public class BankAccount{
private double balance; // Account balance
public BankAccount() {
balance = 0.0;
}
public BankAccount(double startBalance){
balance = startBalance;
}
public BankAccount(String str){
balance = Double.parseDouble(str);
}
//other methods definition
}//end of class BankAccount
i) Identify one characteristic of Object-Oriented Programming applied to the above
methods.
(1 mark)
ii) Write three method headers that relate to the characteristic identified in i).
(1.5 marks)
iii) Briefly describe the characteristic identified in i) and relate it to the code segment.
(2.5 marks)
Answer:
i) Polymorphism/method overloading/constructor overloading
ii) public BankAccount()
public BankAccount(double startBalance)
public BankAccount(String str)
iii) Polymorphism is the ability to take more than one form. Polymorphism is
implemented in the form of overloading and overriding existing methods.
In the code given, there are three overloaded constructors. The first constructor is
a no-argument constructor. It sets the balance field to 0.0. If we wish to execute
this constructor when we create an instance of the class, we simply pass no
constructor arguments.
The second constructor has a double parameter variable, startBalance, which is
assigned to the balance field. If we wish to execute this constructor when we create
an instance of the class, we pass a double value as a constructor argument.
The third constructor has a String parameter variable, str. It is assumed that the
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 4 CD/MAY 2024/CSC186
String contains a string representation of the account’s balance. The method uses
the Double.parseDouble method to convert the string to a double, and then assigns
it to the balance field. If we wish to execute this constructor when we create an
instance of the class, we pass a reference to a String as a constructor argument.
QUESTION 2 (20 marks)
Personal Shopper Co. is a service that assists clients in purchasing and delivering a range of
products. The total cost for a client depends on the product price, the service fee based on
the product price, and any additional charges. The service fee is a percentage of the product
price, which varies depending on the type of service selected. The formula to calculate the
total cost is as follows:
Total cost = Product Price + (Product Price * Service Fee Percentage/100) + Additional
Charges
Personal Shopper Co. needs a computerised system to calculate the total cost.
Given the class diagram for the class PersonalService.
PersonalService
- price: double //product price
- serviceFee: double // service Fee Percentage
- addCharge: double //additional Charges
+ PersonalService(double, double, double)
+ setPrice(double): void
+ setServiceFee (double): void
+ setAdditionalCharges(double): void
+ getPrice(): double
+ getServiceFee(): double
+ getAddCharge(): double
+ calcTotalCost(): double
+ toString(): String
Based on the above information, answer the following questions:
a) Write a complete class definition for PersonalService.
(8 marks)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 5 CD/MAY 2024/CSC186
Answer:
public class PersonalService
{
private double price;
private double serviceFee;
private double addCharge;
//normal constructor
public PersonalService(double price, double serviceFee, double
addCharge)
{
this.price = price;
this.serviceFee = serviceFee;
this.addCharge = addCharge;
}
//mutators
public void setPrice(double price)
{ this.price = price;}
public void setServiceFee(double serviceFee)
{ this.serviceFee = serviceFee;}
public void setAdditionalCharges(double addCharge)
{ this.addCharge = addCharge;}
//retrievers
public double getPrice(){return price;}
public double getServiceFee(){return serviceFee;}
public double getAddCharge(){return addCharge;}
public double calcTotalCost()
{
return (price + (price * serviceFee / 100.0) + addCharge);
}
public String toString()
{
return (price + " " + serviceFee + " " + addCharge);
}
}
b) Write a program segment in the main application for the following tasks:
i) Receive user input for a product price, service fee percentage, and additional
charges. Store the values in an object named serviceObj of class type
PersonalService. Then display the details of the PersonalService object
including the charges.
(2.5 marks)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 6 CD/MAY 2024/CSC186
Answer:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter product price: ");
double productPrice = scanner.nextDouble();
System.out.println("Enter service fee percentage: ");
double serviceFeePercentage = scanner.nextDouble();
System.out.println("Enter additional charges: ");
double additionalCharges = scanner.nextDouble();
PersonalService serviceObj = new PersonalService(productPrice,
serviceFeePercentage, additionalCharges);
System.out.println("The details of Personal Service:" +
serviceObj.toString());
System.out.println("Total charge before discount: RM"+
serviceObj.calcTotalCost());
ii) During a promotional event, Personal Shopper Co. decides to offer a special discount
on their service fee, reducing it to 5%. Write the code to update the service fee
percentage in the serviceObj.
(1 mark)
Answer:
double x = serviceObj.getServiceFee() - 5; //0.5m
System.out.println("The new charges is: " + x);
serviceObj.setServiceFee(x); //0.5m
iii) The company offers a discount on the total cost based on the product price. Calculate
and display the new total cost after the discount. The details of the percentage
discounts are given below.
Product Price Discount (%)
RM 1000 – RM 4999 5%
RM 5000 – RM 9999 10%
RM 10000 – RM 19999 15%
More than RM 20000 20%
(4.5 marks)
Answer:
double totalCost = serviceObj.calcTotalCost();
double discount=0;
if (serviceObj.getPrice() > 20000) {
discount = totalCost * 0.20;
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 7 CD/MAY 2024/CSC186
} else if (serviceObj.getPrice() > 10000) {
discount = totalCost * 0.15;
} else if (serviceObj.getPrice() > 5000) {
discount = totalCost * 0.10;
} else if (serviceObj.getPrice() > 1000) {
discount = totalCost * 0.05;
double totalCostAfterDiscount = totalCost - discount;
System.out.println("Total charge after discount: RM +
totalCostAfterDiscount);
iv) Using discount value and total cost after the discount calculated in iii), display the
report as below:
****************************************************
Detail Price:
Product Price: RM XXXX.XX
Service Fee: XX.XX %
Additional Charges: RM XXX.XX
Discount: RM XXX.XX
Total Price After Discount: RM XXX.XX
****************************************************
(4 marks)
Answer:
// Format the numbers to two decimal places
DecimalFormat df = new DecimalFormat("0.00");
String formattedProductPrice = df.format(serviceObj.getPrice());
String formattedServiceFee =df.format(serviceObj.getServiceFee());
String formattedAddCharges=df.format(serviceObj.getAddCharge());
String formattedDiscount = df.format(discount);
String
formattedTotalCostAfterDiscount=df.format(totalCostAfterDiscount);
// Display the report
System.out.println("**************************************");
System.out.println("Detail Price:");
System.out.println("Product Price: RM " + formattedProductPrice);
System.out.println("Service Fee: RM " + formattedServiceFee);
System.out.println("Additional Charges: RM "+formattedAddCharges);
System.out.println("Discount: RM " + formattedDiscount);
System.out.println("Total Price After Discount: RM " +
formattedTotalCostAfterDiscount);
System.out.println("*************************************");
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 8 CD/MAY 2024/CSC186
QUESTION 3 (5 marks)
Ahmad was asked to collect data of local temperature readings. He should store the readings
in an array. Given the structure of a class named Locality.
public class Locality {
private int localityId;
private String district;
private double[] temperatureReadings; // 5 readings
//normal constructor
//accessor methods
//processor method
//printer method
}
a) Write statement(s) to create a Locality object using a normal constructor by providing
suitable data.
(2 marks)
Answer:
double[] tempReadings = {36.5, 37.4, 39.0, 39.1, 38.5}; //1m
Locality L = new Locality(5, "Arau", tempReadings); //1m
b) Calculate and display the average temperature reading using the object created in a).
(3 marks)
Answer:
double sum = 0.0; //0.25m
for(int i = 0; i < L.getTemperatureReadings().length; i++) {
//1m
sum = sum + L.getTemperatureReadings()[i]; //1m
}
double avg = sum / L.getTemperatureReadings().length;//0.5m
System.out.println("Average temperature reading: " + avg);
//0.25m
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 9 CD/MAY 2024/CSC186
QUESTION 4 (15 marks)
You are assigned to develop a uniform management system for a school. The system should
allow administrators to input and manage uniform details for students, including Supplier, type,
size, quantity and logo presence. Additionally, the system should provide functionality to
retrieve specific information about the uniforms stored in the system. The following is the class
diagram of the proposed system.
Based on the given information, answer all the following questions:
a) Explain the relationship between class Uniform and Supplier.
(1 mark)
Answer:
It is a has-a relationship that shows an aggregation link.
Supplier can supply uniforms.
Object Supplier can exist independently without object Uniform.
b) Define the following methods for class Supplier.
i) Write a method calculatePrice() to calculate and return the price based on
uniform type and size. Price calculations for the uniform are as follows.
Type Size Price per unit (RM)
Primary S - small 35.00
M - Medium 50.00
L- Large 60.00
Secondary S - small 40.00
M - Medium 60.00
L- Large 70.00
(5 marks)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 10 CD/MAY 2024/CSC186
Answer:
// Method to calculate the price based on uniform type and
size
public double calculatePrice() { //0.25m
double price = 0.0; //0.25m
if (uni.getType().equalsIgnoreCase("Primary")) //0.5m
if (uni.getSize()== 'S')
price = 35.00 * uni.getQuantity();
else if (uni.getSize()== 'M')
price = 50.00 * uni.getQuantity();
else if (uni.getSize()== 'L')
price = 60.00 * uni.getQuantity(); //1.5m
else if
(uni.getType().equalsIgnoreCase("Secondary")) //0.5m
if (uni.getSize()== 'S')
price = 40.00 * uni.getQuantity();
else if (uni.getSize()== 'M')
price = 60.00 * uni.getQuantity();
else if (uni.getSize()== 'L')
price = 70.00 * uni.getQuantity(); //1.5m
return price; //0.5m
}
ii) Write a method named getUniLogo() to return logo presence of a uniform.
(1.5 marks)
Answer:
public boolean getUniLogo() //0.5m
{ return uni.getLogo(); } //1m
c) In the Java main program, write program segments to do the following tasks:
i) Declare an array to store Supplier objects. The size of array is determined by the
user.
(1.5 marks)
Answer:
Scanner scanner = new Scanner(System.in); //0.25m
// Prompt user to enter the number of Supplier objects
System.out.print("Enter the number of Supplier: ");//0.25m
int numberOfSupplier = scanner.nextInt(); //0.5m
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 11 CD/MAY 2024/CSC186
// Declare an array to store Supplier objects
Supplier[] supp = new Supplier[numberOfSupplier]; //0.5m
ii) Read and store all data into the array.
(4 marks)
Answer:
// Read and store data into the array of objects
for (int i = 0; i < supp.length; i++) {
//0.5m
System.out.print("Supplier name?");
String suppName = scanner.nextLine();
System.out.print("Supplier address");
String suppAddress = scanner.nextLine();
System.out.print("Type:
PRIMARY/SECONDARY/PREFECT");
String type = scanner.nextLine();
System.out.print("Quantity uniform?");
Int quantity = scanner.nextInt();
System.out.print("Size code (S-Small; M-Medium;
L-Large ): ");
char size = scanner.next().charAt(0);
System.out.print("Has logo (true/false): ");
boolean logo = scanner.nextBoolean();
//1.5m
// Create a Uniform object
Uniform uniObj = new
Uniform(type,quantity,size,logo); //1m
// Create a Uniform object and store it in the
array
supp[i] = new
Supplier(suppName,suppAddress,uniObj); //1m
}
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 12 CD/MAY 2024/CSC186
iii) Display number of uniforms that have logo printed.
(2 marks)
Answer:
int total=0; //0.25m
for (int i = 0; i < supp.length; i++) { //0.25m
if(supp[i].getUniLogo()) //0.5m
total = total + supp[i].getUniQuantity();
//0.5m
}
System.out.println(“Number of Uniform that have logo
printed: “+total); //0.5m
END OF ANSWER PAPER
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL