We know the secret of your success
PAPER TITLE: OBJECT-ORIENTED JAVA
DATE: Thursday, 5 June 2014
Question 1 (25 marks)
(a) Write a class called Player to model a football player, with the following features:
Player Giggs has scored 5 goals
If the player scored no goals, the returned value would be of the form:
Player Giggs has scored no goals
ANSWERS(Purchase full paper to get all the solution)
(a)
// A private integer instance variable goals to represent the number of players
private int goals;
Player p1 = new Player("Joe Bloggs"); //1
Player p2 = new Player("Jane Doe"); //2
Player p3 = new Player("Joe Bloggs"); //3 p1.addGoals(2); //4 p2.addGoals(1); //5 p3.setName("Joseph Bloggs"); //6 System.out.println(p1); //7
With reference to the above statements only, identify the lines of code (state "none" if there are no such lines) in which:
(d) Sketch a variable reference diagram to illustrate the state of the object referenced by player p3 after the code in part (c) has been executed. (3 marks)
(e) Briefly explain the difference between an actual argument and a formal argument using examples from your Player class. (2 marks)
(f) Consider the following incomplete and incorrect class called Team, intended to describe a team of players:
public class Team
{
private int numOfPlayers;
private String name;
private final int MAX_PLAYERS;
public Team(String newName)
{ super;
this.MAX_PLAYERS = 11; name = newName;
}
Explain why this code will not compile and how you could fix it. (2 marks)
Question 2 (25 marks)
(a) Suppose that Feedable is an interface that specifies a single instance method with the name feed(). This method takes no argument and returns a value of either true or false. Write down the Feedable interface.
(b) Suppose that HungryFrog is a subclass of the OU library class Frog and that HungryFrog implements the Feedable interface.
(i) Write down the header for the HungryFrog class.
Write a zero-argument constructor for HungryFrog.
However, if an instance of HungryFrog does not have enough energy to move to a specified stone it does not move. In such case, it remains on the stone it is currently on and an appropriate message is displayed in a dialogue box.
This behaviour is to be achieved by overriding the setPosition() method inherited from the Frog class.
Write the method setPosition() for HungryFrog.
The feed() method should first check whether the hungry frog is standing on a feeding stone, which for hungry frogs will be stones 1 and 10.
If the hungry frog is not currently standing on a feeding stone, an attempt is made to move it to its nearest feeding stone using setPosition().
If at this stage the hungry frog is on a feeding stone the feed() method now increases the hungry frog's energy level to FULL by increments of 100. On each increment the hungry frog also croaks.
Finally, if the hungry frog is on a feeding stone, the method should return true, otherwise false.
(c) Suppose that VegetarianHungryFrog is a subclass of HungryFrog. Explain how VegetarianHungryFrog can implement the Feedable interface and briefly justify your answer.
(d) A class called Snake, which is unrelated to HungryFrog, also implements the Feedable interface.
A class called ZooKeeper will be used to control the behaviour of two objects belonging to classes that implement the Feedable interface. The constructor for the class takes two formal arguments of type Feedable.
Explain, making reference to the concept of substitutability, why it is legal to supply the constructor with actual arguments of type Snake and HungryFrog.
(e) Write down three similarities in the usages of abstract classes and interfaces.
Question 3 (25 marks)
(a) Write down Java code for the declaration and creation of collections that are appropriate for each of the following scenarios:
In each case justify your choice of collection.
(b) The following table shows types of flower bulbs, such as Crocus, Daffodil and Tulip, and their varieties, such as Pickwick. The table is part of a catalogue for a commercial flower bulb grower that shows the grower’s current range for sale.
Type of bulb
Varieties
"Crocus"
"Pickwick", "Grand Maitre", "Yalta"
"Daffodil"
"Elka", "Jenny", "Carlton"
"Tulip"
"Blueberry Ripple", "Red Impression"
The table is to be implemented as a map where:
The keys of the map must be maintained in ascending alphabetical order, while each value (a list of varieties) is to be maintained in the order in which a variety is added.
A class BulbCatalogue is to be developed to manage this map.
(i) The class BulbCatalogue requires a private instance variable bulbMap to reference a map appropriate for holding the data in the above table. Write the declaration for this variable.
(ii) Write a zero-argument constructor for BulbCatalogue that initialises bulbMap to reference an empty map of a suitable class.
(iii) Write a public instance method for BulbCatalogue to add a new bulb type to bulbMap. The collection of this new bulb type’s varieties should be empty. The method has the following header:
public void addBulbType(String bulbType)
(iv) Write a public instance method for BulbCatalogue to add a new variety of bulb for an existing bulb type to bulbMap. The method has the following header:
public void addVariety(String bulbType, String variety)
Your method should assume that the bulb type already exists as a key in the map but that the variety is new.
(v) Write a public instance method for BulbCatalogue that removes a variety of bulb for a particular bulb type from bulbMap. The method has the following header:
public void removeVariety(String bulbType, String variety)
Your method should assume that the bulb type exists in the map and also that the variety exists for that bulb type.
(2 marks) (vi) Write a public instance method with the following header:
public void printVarieties(String bulbType)
that will print to the standard output a list of all the varieties of the specified bulb type. The output should be of the form:
Our varieties of Tulip are:
Blueberry Ripple
Red Impression
Your method should assume that the bulb type exists as a key in the map.
(vii) Write a public instance method printCatalogue() that takes no arguments and returns no value. The method prints to standard output each bulb type and its varieties. The output should be of the form:
Catalogue:
Our varieties of Crocus are:
Pickwick
Grand Maitre
Yalta
Our varieties of Daffodil are:
Elka
Jenny
Carlton
(viii) As time passes, bulb types and their varieties are added to and removed from the map and it becomes apparent that it is all too easy to add the same variety for a particular bulb type more than once by mistake.
Rather than add special code to check for when a duplicate variety may be added, we could use a different collection for bulbMap to avoid the same variety appearing more than once in the variety list for a bulb type.
Write down an appropriate declaration of bulbMap to accomplish this and explain (without code) any other changes that would have to be made to the BulbCatalogue class.
(ix) None of the methods we asked you to write include any validation of arguments, so things can go wrong.
Explain what could go wrong with the addVariety() method above and describe how you would fix it using defensive programming.
You do not need to write any code, but you may illustrate your answer with code if you so wish.
Question 4 (25 marks)
(a) (i) In the context of a data source and a data sink, describe the purpose of a stream in Java.
(ii) In the context of a program and an external device, explain what is meant by a buffer and explain its main advantage.
(b) “Redaction” is the process of removing sensitive information from a document or other medium, so that it may be distributed to a broader audience. In this part of the question you are to complete a method to perform redaction, with the following header:
public static void redactFile(Set prohibitedWords)
The purpose of the method is to read a .txt file and write to another file, a copy of the .txt file with any of the prohibited words being replaced by four X’s, like this "XXXX". For simplicity, you can assume that the file being read has no punctuation, only spaces.
The incomplete code for the method is shown on pages 9-10.
(i) In your answer book, write down and complete the assignment statements that begin:
String inPathname =
String outPathname =
File originalFile = File redactedFile = bufferedFileReader = bufferedFileWriter =
(ii) The outer while loop iterates reading a line from the file at a time, until all lines have been read. Write the boolean condition for the outer while loop.
Each line read by the outer while loop is converted into a Scanner object. Complete the assignment statement that begins:
Scanner lineScanner =
The inner while loop iterates reading a single token from lineScanner, until all tokens have been read. Write the boolean condition for the inner while loop.
(iii) Each iteration of the inner while loop reads a token from lineScanner and checks to see if it is in the prohibited word set. If the current token is in prohibitedWords then "XXXX" should be added to redactedLine followed by a space, otherwise the unredacted token followed by a space should be added to redactedLine. After the final iteration of this inner loop redactedLine should contain a redacted version of the current line. Write the statement block for the inner while loop.
(iv) Write the code to complete the statement block for the outer while loop which writes each redacted line to the redacted file followed by a carriage return, and then gets the next line for processing.
(c) (i) The variables bufferedFileWriter and bufferedFileReader are declared before the first try block. In the context of this method, what is the reason for this?
(2 marks)
(ii) In part (b)(i) you were asked to complete the assignment statements
bufferedFileReader = bufferedFileWriter =
These assignment statements are within a try block. Why is this the case? Explain your answer.
(iii) Give two reasons why it is important that an attempt is made to close the streams referenced by bufferedFileWriter and bufferedFileReader
(iv) Explain why the code to close the streams is within a finally block.
Incomplete code for the method
OUDialog.alert("Please choose a file to redact");
String inPathname = //complete for part (b)(i)
File originalFile = //complete for part (b)(i)
OUDialog.alert("Please provide a file name for the redacted file");
String outPathname = //complete for part (b)(i)
File redactedFile = //complete for part (b)(i)
BufferedReader bufferedFileReader = null; BufferedWriter bufferedFileWriter = null; try { bufferedFileReader = //complete for part (b)(i) bufferedFileWriter = //complete for part (b)(i) String currentLine = bufferedFileReader.readLine();
while (//complete for part (b)(ii))
String redactedLine = “”;
Scanner lineScanner = //complete for part (b)(ii) &n
Last updated: Sep 02, 2021 10:50 AM
Your one-stop website for academic resources, tutoring, writing, editing, study abroad application, cv writing & proofreading needs.