Premium Resources

We know the secret of your success

M250/J Object-Oriented JAVA Programming 2016

$39.00

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
    ANSWERS(Purchase full paper to get all the solution)

//a private instance variable text of type String
private String text;

  • a constructor with a String argument, that initialises text to the value of the argument;
  • a public instance method getText() that takes no argument and returns the value of text;
  • a public instance method merge() that takes an argument of type Document and returns no value. This method should append to the receiver's text a single space, followed by the text of the argument.

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".

  • a public instance method with signature boolean equals(Document d)that returns true if the receiver Document and the argument Document contain the same text, that is, if the two texts consist of the same characters in the same order. Otherwise the method should return false.

(11 marks)

(b) From your Document class, identify an example of each of the following, writing down a code snippet for each that illustrates it:

  1. declaration of a reference variable
  2. declaration of a formal argument
  3. use of an operator
  4. use of a Java keyword

(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

  1. When the println() statement is executed it results in the execution of the toString() method inherited by Document, and this produces the observed output. Where is the toString() method inherited from?
  2. Briefly explain what the output represents.

(3 marks)

 

(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?

(1 mark)

(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.)

(4 marks)

(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.

(3 marks)

(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.

(1 mark)

(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.

(2 marks)

(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.

(2 marks)

(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.

(2 marks)

(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.)

(3 marks)

(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 not dreaming, it wakes up, changes its colour to yellow and rouses once (to show resentment at being woken), then moves to the specified stone
  • if dreaming, it wakes up, changes its colour to red and rouses three times (to show extreme resentment at being woken from a dream), then moves directly to the specified stone

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).

(7 marks)

 

(c) Suppose that DozyFrog is a subclass of SleepyFrog. Explain how DozyFrog can implement the interface Dream. Briefly justify your answer.

(2 marks)

(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:

  • a suit (Hearts, Clubs, Diamonds or Spades)
  • a value from 1 to 13 (where 1 is the Ace, 11 is the Jack, 12 is the

    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. 

(2 marks)

(ii) Write the constructor to initialise cards with a suitable empty collection.

(2 marks)

(iii) Write down a line of code that could be used to add the 10 of Spades to the cards collection. 

(2 marks)

(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.

(2 marks)

       

(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 

Set up the Snap object ready to start the game…

  • 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.

and then start the game…

  • 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

(11 marks)

 

(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: 

  • suit of type char which takes one of the values 'H', 'C', 'D' or 'S'.
  • value of type int, which takes one of the values 1 to 13.

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)

 

 

Question 4 (25 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

N

O

P

X

Y

Z

 

A

B

C

K

L

M

 

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:

  • Creates an instance of StringBuilder and assigns it to outputLine.
  • Converts currentLine to uppercase.
  • For each character in currentLine the method:
    • tests whether the character is between A and M, or between N and Z. If it is, the ROT-13 algorithm is applied appropriately to that character. If not, the character is unchanged.
    • appends the character (whether ROT-13 has been applied or not) to

Last updated: Sep 02, 2021 11:03 AM

Can't find a resource? Get in touch

AcademicianHelp

Your one-stop website for academic resources, tutoring, writing, editing, study abroad application, cv writing & proofreading needs.

Get Quote
TOP