We know the secret of your success
PAPER TITLE: OBJECT-ORIENTED JAVA
DATE: Tuesday, 7 June 2016
Question 1 (25 marks)
(a) Write the class Document with the following features:
//a private instance variable text of type String private String text;
For example if doc1 is an instance of Document with text "The rain in Spain" and doc2 is an instance of Document with text "falls mainly on the plain", then following the execution of doc1.merge(doc2) the value of text in doc1 will be "The rain in Spain falls mainly on the plain".
(b) From your Document class, identify an example of each of the following, writing down a code snippet for each that illustrates it:
(4 marks)
(c) Suppose that the following line of code is executed:
System.out.println(new Document("Question 1"));
The output observed is
Document@9ea89f
(d) Suppose the following code is written to test the class Document.
Document doc1 = new Document("Then");
Document doc2 = new Document("Daniel");
Document doc3 = new Document("smiled."); Document doc4 = doc1;
System.out.println("doc3 equals doc1 " + doc3.equals(doc1));
doc4.merge(doc2);
System.out.println("doc4 equals doc1 " + doc4.equals(doc1));
(i) When this code is executed, how many Document objects will be created?
(ii) Draw a variable reference diagram to show the relationships between the variables and the objects after this code has been executed. (You do not need to include protocols.)
(iii) The output after running this code is
doc3 equals doc1 false doc4 equals doc1 true
Explain why this is so. (2 marks)
Question 2 (25 marks)
Scenario: This question concerns a variety of frog which only changes colour if it is asleep and being moved to a position other than its current position.
(a) Suppose that Dream is an interface that specifies two methods, startDreaming() and stopDreaming(). Neither of these methods takes arguments or returns values. Write the code for the Dream interface.
(b) Suppose that SleepyFrog is a subclass of the OU library class Frog (as in your exam handbook) and that SleepyFrog implements the Dream interface.
(i) Write down the header for the SleepyFrog class.
(ii) Instances of SleepyFrog have two additional private instance variables awake and dreaming, both of type boolean, in addition to inherited instance variables.
Write a constructor that takes no argument and sets the value of awake to true and the value of dreaming to false. The inherited instance variables should be initialised as for the superclass.
(iii) Instances of SleepyFrog are so lazy that they have lost the ability to change colour when requested to do so. In order to implement this behaviour, a single method inherited from the Amphibian class (the direct superclass of Frog) must be overridden in the SleepyFrog class.
Identify the method concerned and write the required code.
(iv) Write code to implement the methods in the Dream interface so that if an instance of SleepyFrog is asleep (that is, not awake) and receives a startDreaming() message, the value of dreaming is set to true, and if it is asleep and receives a stopDreaming() message, the value of dreaming is set to false.
(v) Write a private helper method called rouse() which takes a single int argument and returns no value. The method should cause the receiver to jump and then croak, both behaviours being carried out the number of times specified by the argument. (You can think of this as a sleepy frog noisily rousing from sleep.)
(Note – in case you are wondering how a SleepyFrog gets to sleep in the first place, you can assume that there is a method that will cause this to happen, but you won't need to use it anywhere so we have omitted the details.)
(vi) Although SleepyFrogs cannot change colour in the normal way, they may change their colour to yellow or even red if asked to change their position while asleep. (SleepyFrogs do this to show their resentment at being woken up).
Write code that overrides the inherited setPosition() method according to the following specification:
If a SleepyFrog is awake it moves in exactly same way as an ordinary Frog.
If a SleepyFrog is asleep and the argument to setPosition() represents a stone different from the one the SleepyFrog is currently on then its behaviour depends on whether it is dreaming or not:
If a SleepyFrog is asleep and the argument to setPosition() represents the same stone as the one the SleepyFrog is currently on then it does nothing.
Write the necessary code for the overridden setPosition() method, which must make use of the helper method you wrote in (v).
(c) Suppose that DozyFrog is a subclass of SleepyFrog. Explain how DozyFrog can implement the interface Dream. Briefly justify your answer.
(d) Briefly explain two differences and one similarity between interfaces and abstract classes.
(3 marks)
Question 3 (25 marks)
Scenario: A standard deck of playing cards consists of 52 cards. Each card has:
Queen, 13 is the King and 2 to 10 are the cards with those face values)
We will represent each card by a String consisting of a letter – H, C, D or S – which stands for its suit, followed by the value 1 to 13 of the card.
For example, the string "H6" represents the six of Hearts, and the string "C12" represents the Queen of Clubs.
(a) In this part you will develop code for the Deck class.
(i) An object of this class has an ordered collection of strings representing a standard deck of cards, and the cards will be accessed in order of their appearance in the collection.
Write down the declaration of a private instance variable called cards, which should be declared to be of a suitable interface type to reference this collection.
(ii) Write the constructor to initialise cards with a suitable empty collection.
(iii) Write down a line of code that could be used to add the 10 of Spades to the cards collection.
(iv) Assuming that the cards collection has been populated with strings representing the cards, write the instance method with the following header:
public void randomOrder()
This method should ensure that the order of the strings in the cards collection is random.
(2 marks)
(v) Write the instance method with the following header:
public String dealCard()
This method must remove and return the first card in the collection cards. You can assume the collection is not empty.
(b)
Assume that a class Hand exists. An object of this class represents a collection of zero or more cards that have been dealt from a deck.
The Hand class contains the following methods (which you do not need to write):
public String getCard() which removes and returns the first card (represented by a string) in the hand
public void addCard(String card) whose string argument, representing a card, is added to the hand
public int getSize() which returns the number of cards (strings) in the hand.
A class Snap has been partially written to play the game ‘Snap’. It has the following instance variables:
private Deck snapDeck; private Hand hand1; private Hand hand2; private int snaps;
In our version of the game, there are two hands, and a card is drawn from each hand repeatedly. If the two drawn cards have the same value (for example the 10 of Hearts and the 10 of Spades) then a ‘snap’ (a match) has occurred. This continues until both hands are empty.
The class Snap requires a public instance method play() (which takes no arguments and returns no value) to play the game. Write the play() method which will
initialise snapDeck to reference a full deck of 52 cards. Assume that the class Deck has a static method called fullDeck(), which returns a full deck of cards.
randomise the order of cards in the snapDeck
initialise snaps to 0
deal all the cards from the snapDeck so that hand1 and hand2 have an equal number of cards. Assume that the Snap constructor initialises the instance variables hand1 and hand2 to empty collections of strings.
while there are still cards left in both hands,
o get a card from hand1 and a card from hand2
o if the cards have the same value, increment the number of snaps
When there are no cards left…
print out the number of snaps
(c) We used strings such as "D12" to represent cards in our Deck class, to keep the scenario simple. A more object-oriented approach would be to use a Card class with two private instance variables:
You do not need to write the Card class, but assuming it has been written, write down the new declaration for the private instance variable cards of the Deck class. (2 marks)
(d) 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)
Scenario: This question concerns a simple substitution cipher used for encoding
and decoding text.
ROT-13 (meaning rotate 13) is a simple substitution cipher that is its own inverse; that is, to undo ROT-13, the same algorithm is applied, so the same action can be used for encoding and decoding.
To apply the ROT-13 algorithm, each of the letters A to M are substituted for the letter 13 places forward along the alphabet, as in Table 1.
Table 1
A
B
C
…
K
L
M
↓
N
O
P
X
Y
Z
And each of the letters N to Z is replaced with the letter 13 places backward along the alphabet as in Table 2.
Table 2
Any lowercase letters will be converted to uppercase letters before ROT-13 is applied, since only uppercase letters are encoded. Punctuation symbols, whitespace, and all other characters are left unchanged.
In Java you can shift the letter 'G' forward by 13 places with the following expression:
(char)('G' + 13)
which would evaluate to the char value 'T'.
Consider a utility class called CipherIO, which has a class method with the following header:
public static void rot13()
In parts (a) to (c) overleaf you will complete this method according to the following specification:
The method presents the user with a file chooser dialogue box, from which the user should select the pathname of a text file. The returned pathname is assigned to the local variable aPathname.
aPathname is then used to create a File object.
The method then attempts to open a buffered stream on the file and assign it to the BufferedReader variable bfr.
currentLine is assigned the first line from the stream.
Within a while loop, the method then:
Last updated: Sep 02, 2021 11:03 AM
Your one-stop website for academic resources, tutoring, writing, editing, study abroad application, cv writing & proofreading needs.