Premium Resources

We know the secret of your success

M250/H Object-Oriented JAVA Programming 2018

$39.00

PAPER TITLE: OBJECT-ORIENTED JAVA

DATE: Friday, 8 June 2018

 

Question 1 (40 marks)

(a) (i) Write a class called RoomCode with the following features:

  • a private instance variable of type String called code, which will be used to reference a room code as outlined in the scenario above
  • a public zero-argument constructor which initializes code to an empty string
  • a public getter method getCode() that returns the current value of code (4 marks)

 

ANSWERS(Purchase full paper to get all the solution)

    //(i)
    //a private instance variable of type String called code
    private String code;

    //a public zero-argument constructor which initializes code to an empty string 
    RoomCode(){
        this.code = "";
    }

    //a public getter method getCode() that returns the current value of code 
    public String getCode(){
        return this.code;
    }

 

(ii) The class requires further public getter methods that will return the building letter, the floor and the room of the receiver object, based on the current value of code. You should assume that the code value will be valid in all cases.

  • a method getBuilding() that returns the building letter part of the current room code as a char
  • a method getFloor() that returns the floor number part of the current room code as a char
  • a method getRoom() that returns the room part of the current room code as a String (6 marks)

(iii) Write a public instance method toString(), which returns a String representation of a RoomCode object in the format shown by the following example:

  Building J Floor 3 Room 1B 

(2 marks)

 

(b) (i)        Write a private helper method isValidCode(String s) for the class RoomCode. The method takes a single String argument representing a possible room code and returns true if the argument is not null, has four characters, and represents a valid room code, as described in the Scenario above. Otherwise the method returns false.

(Remember that characters can be compared using the > and < operators and this offers one way to carry out some of the checks that are needed.)  (8 marks)

(ii) Write a public method for the RoomCode class called setCode(String s), which takes a single String argument. The method determines if the argument represents a valid code. If so, the receiver’s code variable is set to the argument, otherwise no change is made. (2 marks)

(iii) Write a method isEqualCode(RoomCode r)for the RoomCode class, which takes a single argument of type RoomCode, and tests to see if the receiver and the argument have the same codes.

If both codes are null the method should return true.

If exactly one code is null the method should return false.

Otherwise the method should return true if the receiver’s code and the argument’s code are equal, and return false otherwise.  (5 marks)

 

(c) Based on the RoomCode class written so far, answer the following questions:

(i)             The RoomCode class has more attributes than it has instance variables. How many of each does it have? Explain your answer.  (2 marks)

(ii) How many reference type and primitive type instance variables does the RoomCode class have? Explain your answer. (1 mark)                

(iii) Write down any examples of overloading in the class RoomCode, or state ‘none’.  (1 mark)

(iv) Write down any examples of overriding in the class RoomCode, or state ‘none’.  (1 mark)       

(v) Explain how the class RoomCode illustrates the idea of encapsulation(2 marks)

 

(d) A class called RoomFinder includes the following declarations:

 public class RoomFinder

 {

   private final int MAX_ROOMS = 105;     private RoomCode[] rooms;       }                

(i)  What is the nature of the object-oriented relationship between classes RoomCode and RoomFinder? Explain your answer. (2 marks)

(ii) Is the variable rooms a reference variable or a primitive variable? Explain your answer. (2 marks)

(iii) Suppose that a colleague of yours adds a method getRooms() in the RoomFinder class with the following header:

                                                      public RoomCode[] getRooms()

Write a short explanation to your colleague explaining why it may be undesirable to provide this method.  (2 marks)

 

 

Question 2 (30 marks)

(a) Suppose that Feedable is a Java interface that specifies a single  method with the signature feed(). This method takes no argument and returns a value of either true or false. Write down the Feedable interface. (2 marks)

(b) Suppose that HungryFrog is a subclass of the OU library class Frog and that HungryFrog implements the Feedable interface. Write down the header for the HungryFrog class.  (1 mark)

(c) Assume HungryFrog has an additional instance variable energyLevel of type int, and methods for getting and setting the value of this instance variable.       

Also, assume HungryFrog has a class constant FULL which has the value 2000.                

(i) When an instance of HungryFrog is created its inherited instance variables are to be initialised to the same values as for instances of Frog. The additional instance variable energyLevel is to be initialised to the class constant FULL.

                                                      Write a zero-argument constructor for HungryFrog. (2 marks)

(ii) Instances of HungryFrog expend energy when their position is changed, according to how far they move. The energy required is 100 units per stone moved.

For example, if a frog is on stone 1 and it moves to stone 5, it uses 4 x 100 = 400 units of energy.

However, if an instance of HungryFrog does not have enough energy to move to a specified stone it does not move. In this case, it remains on the stone it is currently on and the message "Insufficient energy to move" is printed to the Display Pane. 

This behaviour is to be achieved by overriding a single method in the Frog class. 

                                                      Write the overriding method for HungryFrog. (5 marks)

(iii) Write the feed() method for HungryFrog to the following specification:

The feed() method should first check that the hungry frog is not standing on a feeding stone, which for hungry frogs will be stone 1 and stone 10. 

If the hungry frog is not currently standing on a feeding stone, an attempt is made to move it directly to its nearest feeding stone using setPosition().

If the hungry frog is on a feeding stone, the feed() method should increase the hungry frog’s energy level to FULL by increments of 100. On each increment, the hungry frog should croak once.

Finally, if the hungry frog is on a feeding stone, the method should return true, otherwise false. (6 marks)

 

(d) Suppose that we want to keep a count of the number of HungryFrog objects that have been created. Explain using code fragments how we could achieve this. (3 marks)

 

(e) Briefly explain one advantage of using an interface type. (1 mark)

 

(f)  Suppose that a class called Snake, which is unrelated to HungryFrog, also implements the Feedable interface, and that a class called ZooKeeper has a constructor that takes a formal argument of type Feedable.

  1. Briefly explain why the following lines of code are valid:

                                                      HungryFrog hf = new HungryFrog();

                                                      ZooKeeper zk1 = new ZooKeeper(hf);

                                                      Snake s = new Snake();

                                                      ZooKeeper zk2 = new ZooKeeper(s);

  1. Explain, with reference to the ZooKeeper class, how the Feedable interface supports polymorphism(4 marks)

 

(g) Write down three differences between abstract classes and interfaces. For each difference, you should state what is true of an abstract class, and what is true of an interface. (6 marks)

 

 

Question 3 (30 marks)

 

(a) In this part of the question you will develop code for the Walk class.                

(i)  An object of this class has a list of strings representing the date and places as described in the scenario above. Write down the declaration of a private instance variable of Walk, called details, which should be declared as a List of String elements. (1 mark)  

(ii) Write a zero-argument constructor for Walk to initialise details with a suitable empty collection. (1 mark)                

(iii) Write a two-argument constructor for Walk that takes a String argument representing the date of the walk, and a list of Strings representing places visited on that date (in order) and initialises details using these arguments. (3 marks) (iv) Write a public instance method addPlace() that takes a String argument representing a place to be added, adds it to the end of the details list, and returns the number of places now in details.  (2 marks)

(v) Write a public instance method whichPlace() that takes an int argument representing the position of the place to be found – so for example if the third place visited is required, then the value of this argument would be 3. If the argument is valid (i.e. does represent one of the places in details), the String representing that place should be returned, otherwise "Invalid index" should be returned. (3 marks)                

(vi) Write a public instance method whichIndex() that takes a String argument representing a place and returns the index at which the place appears in the details list, or if the place is not present in the list, returns -1.

For example if the argument is "Mawgan" in the example given in the Scenario then 3 should be returned. If the argument is "Sunhaven" in the same example then -1 should be returned.

You can assume a place never appears in the list more than once and that the case of each letter in the argument is correct. (2 marks)

(vii) Write a public instance method removePlace() that takes a String argument representing a place. If the place is present on the walk it should be removed and an int returned which represents the number of places now remaining in the list. If the place is not present in the list then -1 should be returned.

As in part (vi) you can assume a place never appears in the list more than once. (3 marks)

(viii) Write a public instance method updateName() that takes two String arguments.                     

The first argument represents a place that may be present in details. The second argument represents a replacement name for the place, if the place is in details. (Such a method might be required if an existing place turned out to be incorrectly named.)                    

If the first argument does represent a place in details, that place should be replaced by the second argument and true returned. Otherwise false should be returned,                    

For illustration, in the example given in the Scenario, if the first argument to this method is "Bedruthan Steps" and the second is "Park Head" then the fourth place in the places list will be replaced by "Park Head" and true will be returned. (5 marks)                

(ix) Write a public instance method printWalk() that takes no arguments and returns no value. The method should print out on separate lines the date, followed by the places visited on the walk, as in the example output below.

                                            On 16/7/17 the following places were visited:

  Porth

  Watergate Bay

  Mawgan

  Bedruthan Steps

  Porthcothan

(3 marks)

 

 

(b) Using instances of the Walk class is a suitable way to record information about a small number of walks, but it would be unwieldy for a large number of walks, and has the further disadvantage that finding the information for a given date is rather awkward.

Sue therefore decides to adopt a different design. In this new design a class called WalkDiary will have a single map that records information for a whole series of walks. Each key in the map will be a date, represented as a string, and the corresponding map value will be a list of strings representing the places visited on that date, in the order visited.

(i)     Write down a declaration of a private instance variable walks of a suitable type to reference the map described above. (2 marks)

(ii) Write a public instance method addWalk() that takes two arguments. The first argument of type String represents the date of a walk. The second argument is a list of strings representing the places visited on the walk. The method should simply update walks using the arguments and return no value. (3 marks)

(iii) Could the design in part (b) have used a set of strings for the places visited, rather than a list? Explain your answer. (2 marks)

 

[END OF QUESTION PAPER]

 

Purchase full paper by adding to cart

 

Last updated: Sep 02, 2021 12:15 PM

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