We know the secret of your success
PAPER TITLE: OBJECT-ORIENTED JAVA
DATE: Friday, 2 June 2017
Question 1 (40 marks)
ANSWERS(Purchase full paper to get all the solution) public class Player {
// a private instance variable of type int called goals, // which stores the number of goals scored by the player private int goals;
}
Player: Messi has scored 9 goals this season
(10 marks)
b. Explain how the class Player illustrates encapsulation and data hiding. (3 marks)
c. (i) Explain the meaning of the terms overloading and overriding.
(ii) List any methods that are overloaded or overridden in the class Player above, saying which they are, overloaded or overridden. (4 marks)
d. A programmer attempts to compile the following code, using the class Player described above:
Player messi = new Player();
Explain why it will not compile. (2 marks)
e. Consider the following method written for the class Player:
public boolean equals(Object o) {
Player p = (Player) o; //1
if (p.getGoals() == this.getGoals() & p.getName().equals(this.getName())){
return true;
return false;
Answer the following questions about this code:
Player p = new Player("");
Player q = new Player("");
System.out.println(p.equals(q));
Explain your answer. (2 marks)
(ii) Suppose that the following code is executed:
Player rob = new Player("Rob");
Player sue = new Player("Sue");
sue.incrementGoals(); sue.incrementGoals(); rob.incrementGoals();
Draw a variable reference diagram to illustrate the effect of executing these lines of code. (4 marks)
g. A class called Team includes the following declarations:
public class Team {
private final int MAX_PLAYERS;
private Player[] players;
private int numOfPlayers; //number of players in the array
private String name;
(i) What is the nature of the object-oriented relationship between classes Team and Player? Explain your answer. (2 marks)
(ii) Write a zero argument constructor for the class Team, which will initialize MAX_PLAYERS to 11, create the players array with an appropriate capacity, initialize numOfPlayers to 0 and set name to the empty string.
public String toString() {
String temp = "Team " + name +
" has the following players:\n";
for (int i = 0; i < numOfPlayers; i++)
{
temp = temp + players[i].toString() + "\n";
return temp;
Suppose that the following code is executed as a test:
Team myTeam = new Team();
System.out.println(myTeam);
State what output you would expect to see and explain your answer. (3 marks)
(iv) What might be the disadvantage of Team having a getPlayers() method as shown below:
public Player[] getPlayers()
return players;
(2 marks)
Question 2 (30 marks)
(a) Suppose Trainable is an interface that specifies two methods, the first called startTraining() which takes a formal argument of type int, and the second called stopTraining() which takes no argument. Neither of these methods returns a value. Write the code for the Trainable interface. (3 marks)
(b) Suppose that TrainableFrog is a subclass of the OU library class Frog (as in your exam handbook) and that TrainableFrog implements the Trainable interface.
(i) Write down the header for the TrainableFrog class. (1 mark)
(ii) The class TrainableFrog includes a private instance variable isTraining of type boolean.
Write a constructor that takes no argument and sets the value of isTraining to false. The inherited instance variables should be initialised as for the superclass. (2 marks)
(iii) Instances of TrainableFrog are unable to change their colour unless they are training – that is, unless the value of isTraining has been set to true. In order to implement this behaviour, a single method inherited from the Amphibian class (the direct superclass of Frog) should be overridden in the TrainableFrog class.
Identify the method concerned and write the required code. (3 marks)
(iv) Write a startTraining() method for TrainableFrog, according to the following specification:
First, the value of isTraining should be set to true and the colour of the receiver set to green.
Next, the receiver should jump the number of times specified by the argument. (You should assume the argument will always be greater than 0.)
After each jump, a check should be made to see if the colour of the receiver should be changed as follows:
If the receiver has made at least two but fewer than five jumps, its colour should be set to brown.
If the receiver has made five or more jumps, its colour should be set to red. (6 marks)
(v) Write the stopTraining() method to the following specification:
First, the value of isTraining should be set to false.
Next, the receiver should move one stone in the appropriate direction until it reaches stone 5, and then croak once to announce it has finished training. (5 marks)
(c) Briefly explain one potential advantage of defining the Trainable interface. (2 marks)
(d) Write down two similarities and two differences between abstract classes and interfaces. (4 marks)
(e) Suppose that LearnerFrog is a subclass of TrainableFrog. Explain how LearnerFrog can implement the interface Trainable. Briefly justify your answer. (4 marks)
Question 3 (30 marks)
(a) In this part of the question you will develop code for the Reptiles class.
(i) An object of this class has an ordered collection of strings representing the lizards as described in the scenario above.
Write down the declaration of a private instance variable called myLizards, which should be declared to be of a suitable interface type to reference this collection. (2 marks)
(ii) Write the constructor for Reptiles to initialise myLizards with a suitable empty collection. (2 marks)
(iii) Write the public instance method addLizard() that takes a String argument representing a lizard to add to the collection, and returns the current number of lizards in the collection. You can assume that the argument is a valid species name, appropriately capitalised. (4 marks)
(iv)Write the public instance method howManySpecies() that takes no argument and returns the current number of different species in the collection. (5 marks)
(v) Write the public instance method doIHaveIt() that takes a String argument representing a lizard species and returns true if that species is in the collection and false otherwise. (3 marks)
(vi) Write the instance method with the following header:
public void update(String oldSpecies,int occurrence, String newSpecies)
which can be used to find a particular occurrence of a species and replace it with a different one. (Lizards can be difficult to identify if wildcaught and young.) For example, if Sue has 3 lizards of the "Uromastyx Moroccan" species and then discovers that the second one is actually a "Uromastyx Egyptian", then the arguments to this method would be "Uromastyx Moroccan", 2 and "Uromastyx Egyptian".
You can assume the occurrence to be replaced exists in the collection. (5 marks)
(vii) Using the update() method you’ve just written, write the instance method with the following header:
Public void removeLizard(String thisSpecies, int occurrence)
that will remove a particular occurrence of a species, as follows:
No marks will be awarded for solutions which do not follow this specification. (2 marks)
(viii) Give one reason why it is preferable to declare a collection using an interface type, even though the instance assigned to it will be of a concrete class. (2 marks)
(b) In this part of the question you will look at some other scenarios. For those parts where you are asked to determine which collection is most appropriate, select only from the list below:
Array, ArrayList, Set, TreeSet, Map, TreeMap
A class Animal has been written to represent various animals. Objects of this class include attributes of name (String), genus (String), species (String), and microchip number (String).
An animal charity wants to keep records of all the animals they look after. As each animal is accepted by the charity, an object of the class Animal is created with appropriate attribute values.
Purchase full paper by adding to cart
Last updated: Sep 02, 2021 12:12 PM
Your one-stop website for academic resources, tutoring, writing, editing, study abroad application, cv writing & proofreading needs.