Computer Science Department: Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences
Computer Science Department: Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences
Student ID:
Section No.:
Instructions:
1 3
2 4
3 8
Total 15
Page 1 of 7
sum: 4
sum: 4
sum: 10
sum: 10
sum: 10
avg: 2
Page 2 of 7
In the following code (B.java), there are 4 compilation errors: 1 error in class A and 3 errors in class B.
List the line number and the cause of each error in the table below.
1 // file B.java
2 class A
3 {
4 public int x;
5 private int y;
6 protected int z;
7
8 private final int a;
9
10 public A(int x, int y, int z) {
11 this.x = x;
12 this.y = y;
13 this.z = z;
14 }
15 }
16
17 public class B extends A
18 {
19 public B() {
20 }
21
22 @Override
23 public void print() {
24 System.out.println(x + y + z);
25 }
26 }
Answer: (0.25 mark for each correct line / 0.75 mark for each correct cause)
Error # Class Line # Cause of the error
1 A 8 (accepted lines: 10, or 14) constant a is not initialized
Page 3 of 7
For all of the following classes, you don't have to write any getter or setter method. But, you can
consider these methods existing and call them whenever you need.
a) Write a class Reservation where the number of days is initialized by its unique constructor.
Define one method calculatePrice without body that calculates and returns the price based on
the received double variable unitPrice. (2 marks)
Answer a):
public abstract class Reservation // 0.5 mark
{
protected int numberOfDays; // 0.25 mark
// 0.75 mark
public abstract double calculatePrice(double unitPrice);
}
Page 4 of 7
Answer b):
public class HotelReservation extends Reservation // 0.50 mark
{
private int numberOfRooms; // 0.25 mark
@Override
public double calculatePrice(double unitPrice) { // 0.75 mark
return unitPrice * numberOfRooms * numberOfDays; // getNumberOfDays()
}
}
Page 5 of 7
Answer c):
public class CarReservation extends Reservation // 0.50 mark
{
private int driverAge; // 0.25 mark
@Override
public double calculatePrice(double unitPrice) { // 0.75 mark
double result = unitPrice * numberOfDays; // getNumberOfDays()
if (driverAge < 25)
result *= 1.2;
return result;
}
}
Page 6 of 7
Page 7 of 7