We know the secret of your success
PAPER TITLE: OBJECT-ORIENTED JAVA
DATE: Monday, 10 June 2019
Question 1
(a) (i) Write a class called Performer with the following features:
ANSWERS(Purchase full paper to get all the solution)
//Solution 1 (a) (i)
public class Performer {
//private instance variable of type String called equityName
private String equityName;
//private instance variable of type double called payRate
private double payRate;
//private instance variable of type Branch called branch
private Branch branch;
//public class variable of type double called minPayRate
public static double minPayRate;
//public single-argument constructor which initializes equityName
//to the argument string aName, sets branch to null and sets
//payRate to minPayRate
public Performer(String aName){
this.equityName = aName;
this.branch = null;
this.payRate = minPayRate;
}
//public setter method for payRate
public void setPayRate(double payRate){
this.payRate = payRate;
//public getter method for branch
public Branch getBranch(){
return this.branch;
//public setter method for branch
public void setBranch(Branch branch){
this.branch = branch;
//public getter method for equityName
public String getEquityName(){
return this.equityName;
(ii) Write a public instance method isInSameBranchAs() that has a Performer argument. This method will return true if the receiver and the argument Performer objects are members of the same branch, and false otherwise. (5 marks)
(iii) Write a public instance method getFirstName() that has no arguments. This method will return a String consisting of all the characters in the equityName, up to but not including the first space. You may assume that there is a space in the equityName. (5 marks)
(b) Given the code developed in part (a), assume that the following code is part of a method and is executed:
Branch b1; // 1
b1 = new Branch("Kent", "The Alexander Centre"); // 2
Branch b2; // 3
b2 = new Branch("Dorset", "Wessex fm Studios"); // 4
Performer.minPayRate = 9.50; // 5
Performer p1 = new Performer("Happy Bunny"); // 6
Performer p2 = new Performer("Silly Sausage"); // 7
p1.setRate(10.00); // 8
p2.setRate(20.00); // 9
p1.setBranch(b1); // 10
p2.setBranch(b1); // 11
System.out.println(p1.isInSameBranchAs(p2)); // 12
In the numbered lines of code above, identify all the examples of the following, stating the line number(s) on which they occur. If there are no examples, state ’None’ explicitly.
(c) For the class Performer, write the public instance method equals() that overrides the equals() method inherited from Object. This method will return true if the equityName of the receiver is the same as the equityName of the argument object, and otherwise return false. (5 marks)
(d) Based on the Performer class written so far, answer the following questions:
(i) What is the nature of the object-oriented relationship between the classes Performer and Branch? Explain your answer. (2 marks)
(ii) Consider line // 5 in part (b) above. Why can the value of minPayRate be set at this point when no Performer objects have been constructed? (2 marks)
(iii) Give two examples of how scope applies to the Performer class. One example should relate to an instance variable and the other should relate to a formal argument. (5 marks)
(40 marks total)
Question 2
(a) Drivable is a Java interface that specifies three methods accelerate(), brake() and stop(). These methods take no argument and return no value.
Write down the Drivable interface. (3 marks)
(b) In this part of the question you will develop code for the Vehicle class. The class Vehicle inherits directly from Object and implements the Drivable interface.
(i) Write down the header for the Vehicle class. (1 mark)
(ii) Suppose Vehicle has a single private instance variable speed of type int. Vehicle implements the methods of the Driveable interface according to the following rules.
(c) In this part of the question you will develop code for the Car class. The class Car is a subclass of Vehicle.
Car has two extra int instance variables maxSpeed and increment.
(d) Suppose that a class called SpeedBoat, which is unrelated to Car, also implements the Drivable interface, and that a class called Service has a public constructor that takes a formal argument of type Drivable.
Car c = new Car();
SpeedBoat sb = new SpeedBoat();
Service s1 = new Service(c); //1
Service s2 = new Service(sb); //2
i. Briefly explain why lines 1 and 2 below are valid:
(e) Describe three differences between abstract classes and interfaces. (6 marks)
(f) Suppose that SportsCar is a subclass of Car. Describe what needs to be added to the class SportsCar (if anything) so that SportsCar will implement the interface Drivable. Briefly justify your answer. (4 marks)
(30 marks total)
Question 3
(a) In this part of the question you will develop additional code for the CaravanSite class.
(i) Write down the declaration of a private instance variable called bookings, which should be declared as a List of Booking elements, representing bookings currently made for the site, in the order the bookings were made. (1 mark)
(ii) Write a two-argument constructor for CaravanSite that takes a String argument representing the name of the caravan site, and an int representing the maximum number of caravans that can be accommodated, and initialises the instance variables accordingly. The constructor should also initialise bookings with a suitable empty collection. (3 marks)
(iii) Write a public instance method addBooking() that takes a Booking argument representing the booking of a caravan. As long as the number of bookings already made is less than the maximum number of caravans the site can accommodate, the Booking is added to bookings. If there is not enough room then a suitable message is printed. In both cases the remaining number of vans that can still be accommodated after this booking is returned. (4 marks)
(b) In this part of the question you will develop extra code for the Booking class so that instances of Booking may be sorted from earliest to latest estimated arrival hour.
Assume the equals () and hashCode () methods for Booking have already been written.
(i) Write down the new class header for Booking, which must now implement an appropriate interface. (1 mark)
(ii) Write a compareTo(Booking) method for Booking that will allow ordering of Booking instances as above. (3 marks)
(c) Write a public instance method orderBookings() for the CaravanSite class that takes no argument and returns no value. This method should reorder the elements of bookings by estimated arrival hour. (2 marks)
(d) In this part of the question you will develop code for a further class, CaravanClub. This class will help to determine the pattern of estimated arrival times across all caravan sites.
The class CaravanClub requires a single private instance variable arrByTime. This is a map where the key is a particular estimated arrival hour as a whole number (e.g. 16) and the value is an unordered set of Booking with that arrival time, from all caravan sites.
(i) Write down the declaration of a private instance variable arrByTime of a suitable interface type to reference the map described above. (2 marks)
(ii) Write a zero argument constructor that initialises arrByTime to a suitable collection. (2 marks)
(iii) Write the public instance method addSite(). This method takes a CaravanSite instance as the argument and has no return value. The method adds each of the bookings for that particular site to the arrByTime map, according to the bookings’ estimated arrival hours.
Assume that the class CaravanSite has a public instance method getBookings() that returns a list of the bookings for that site.
Note that you cannot assume that a particular estimated arrival hour exists as a key in the map. (8 marks)
(e) (i) Why is it preferable to declare a collection variable in terms of an interface type, such as List, rather than a concrete class, such as ArrayList, which implements that interface? Explain your answer, making two points. (2 marks)
(ii) Give two ways in which an ArrayList is different from an array. (2 marks)
[END OF QUESTION PAPER]
Purchase full paper by adding to cart
Last updated: Sep 02, 2021 12:15 PM
Your one-stop website for academic resources, tutoring, writing, editing, study abroad application, cv writing & proofreading needs.