The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than...

46
The M874 Exam How to cope with it
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than...

Page 1: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

The M874 Exam

How to cope with it

Page 2: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

General Points (1)

• Two sections, first generally easier than second.

• The second section mainly asks you to generate code.

• Minor syntactic errors will not be punished.

• You will be given an index to any class documentation

needed and you can use your software guide.• In the longer section there will be at least one discursive

question.

Page 3: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

General points (2)

• Always use examples to illustrate your points, especially in the first section of the exam.

• Leave time to check your answers.

• Remember that the number of marks allocated to a question or part of a question reflects the difficulty of the question.

Page 4: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 1

How is super used within constructors? [2]

Page 5: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 1 Answer

If a class inherits from a superclass then super within aconstructor calls on the corresponding constructor withinthe superclass. For example if there was a single int argument constructor within the superclass then the code

super(j)

where j is an int would call on this constructor.

Use an example

Page 6: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 2

Describe two differences between Vectors and Arrays? [2]

Page 7: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 2 AnswerThe first difference is that Vector objects are automaticallyextensible, if you want to extend an array you need toprogram it [1].

The second difference is that a Vector is an object while anarray is more akin to a scalar [1].

Page 8: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 3Write down the code for the definition of an exceptionwhich behaves just like an IllegalArgumentExceptionbut is named IllegalParameter Exception [3].

Page 9: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 3 Answer

class IllegalParameterException extends IllegalArgumentException{}

Notice that there are no methods defined,marks would be deducted if you did definethem.

Page 10: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 4

Would you normally use exceptions to monitor whetherarray bounds were violated in a Java program? [2]

Page 11: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 4 Answer

No, you need to check this by careful testing. Exceptions arenormally used to check for errors which occur that areout of the control of the programmer [1]. For example, a userproviding an illegal value when running a program [1].

Always try and give an example

Page 12: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 5

If you were looking for a method which you were totallyconvinced should be part of a class and you were notable to find it in the class, what could have happened? [2]

Page 13: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 5 Answer

What almost certainly has happened is that themethod has been defined in a superclass and youhave not looked further up the inheritance hierarchy [2].

Page 14: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 6

What is an abstract class? [3]

Page 15: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 6 AnswerAn abstract class is a class which contains abstract methods [1].

These methods contain no code, their code is supplied by the classes that inherit from theabstract class [1].

For example, you may have a class Employee which youonly want subclasses to implement a salary method [1].

Example

Page 16: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 7A class ExCon contains two instance variables in1 and in2 which are ints and a String instance variable str.Write down the code for a method equals whichcompares two ExCon objects for equality [4].

Assume that you can directly access the variables usingthe dot notation.

Page 17: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 7 Answer

public boolean equals(ExCon e){return( (this.in1 ==e.in1)&& (this.in2 == e.in2)&& (this.str).equals(e.str));}

The this keywordcan be dropped, itis used fordocumentationpurposes

Page 18: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 8

Describe two differences between applets and applications [2]?

Page 19: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 8 Answer

Applets always inherit from the class Appletwhile applications are stand-alone.

Applications have constructors while appletsdo not.

Applets are executed within a browser, whileapplications are executed stand-alone.

Any two will do

Page 20: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 9A class TestInt has a static int variable countInt and an int instance variable inInt. The static variablekeeps track of the number of TestInt objects that have been created.

Write code for the single-int constructor for this class. One of the functions it carries out is to initialisethe instance variable to the value given by its argument[3].

Page 21: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 9 Answer

Testint(int val){inInt = val;countInt++;}

1 mark for the general form, 1 markfor setting the instance variable and1 mark for incrementing the static variable

Page 22: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 10

What is the role of a layout manager in Java?

Page 23: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 10 Answer

The layout manager determines where components are to beplaced in a container[1]. For example, BorderLayout willuse the points of the compass in determining the position [1].

Always try and useexamples

Page 24: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 11 (Part i)

Why do many of the methods in the Hashtable class take Object arguments? [7]

A class is to be developed which forms part of a system forkeeping track of the number of laps each driver in a racehas completed. How would you use a Hashtable to implement this class? [3]

Page 25: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 11 (Part ii)

Write down the code for three methods in the class. The first is the constructor which allocates a capacity of 50 [3].The second method addLap(Driver d) increases the number of laps completed by driver d.[8]The third method removeDriver(Driver d) removes a driver d from the race [4]. Assume the existence of a classDriver. You will also need to use the static method valueOf which can be found in the class Integer and which convertsits Integer argument into an int.

Page 26: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 11 Answer (i)

The reason is that the Hashtable class can hold a varietyof objects. The class Object is the top-level classin the Java inheritance hierarchy and so any objectcan be used where an Object argument is specified.

The best implementation would be a class in which the instance variable would be a Hashtable object which related drivers to the number of laps that they had finished.

For this type of questionthe class documentationfor Hashtable would be provided

We are not lookingfor inheritancehere

Page 27: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 11 Answer (ii)Assume that the class is known as Race and the instancevariable is laps.

Race(){laps = new Hashtable(50);}

Page 28: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 11 Answer (iii)public void addLap(Driver d){int currLaps;currLaps = Integer.valueOf((Integer)laps.get(d));currLaps++;laps.put(d,new Integer(currLaps));}

The tricky part of this question is that since Hashtableonly stores objects we have to do some trickycoding to retrieve and convert to an int and thenwrite it back. This is why the marks are high for this part.

Page 29: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 11 Answer (iv)

public removeDriver(Driver d){laps.remove(d);}

Page 30: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 (i)Explain how exceptions are used within Java [6].A class is to be developed which reflects the state of a series of switches which are identified by integers starting at 1. Three methods need to be developed, the first is theconstructor which has one int parameter that representsthe number of switches [3]. The second method is called stateit has a switch number as an argument and will return with a zero if a switch is off and a one if it is on. The third method is called offRange[2]. This has two arguments, the first is a switch number and the second is a higher switch number.

Page 31: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 (ii)The method will switch off all the switches in the range definedby the arguments [5].Your task is to write code for these methods including any necessary exception handling [9].We do not require any code for the Exception classes that you use in the methods.

Page 32: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 Answer (i)

There are three aspects to the use of exceptions in Java:first, a method needs to inform the world that it is to throw an exception, second a method which uses code that could throw an exception needs to catch the exception and third there is a need to create an exceptionobject.

To inform the world that a method throws an exceptionrequires the use of the throws keyword.

Page 33: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 Answer (ii)

For example,

public int getVal() throws IllegalArgumentException{

carries this out.

Always use examples

Page 34: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 Answer(iii)When a method contains code that could potentiallythrow an exception then the code should should be enclosedby a try-catch clause which specifies the processingthat needs to be carried out. This code creates the exception and contains code that is executed whenthe exception is thrown

try{code containing an exception}catch(ExceptionType variable){Code to be executed}

Use an example

Page 35: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 Answer (iv)The final aspect to exceptions is the throwing. Thisis done via the throw keyword. This creates theexception object using new.

if(some error condition){ throw new SomeException();…}

Yet another example

Page 36: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 Answer (v)

class Switches{int[] states;int noSwitches;

Switches(int no){states = new int[no];noSwitches = no;}

...}

The most reasonable solution involves an array of ints containingeither zero or one,a Boolean array wouldalso be possible

Page 37: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 (vi)public void state(int no) throws wrongSwitchNumberException{if (no<1 || no> noSwitches){ throw new wrongSwitchNumberException();else return states[no-1];}

Here we are assumingthat the switch numbersrange from 1 up to some upper limit even though theyare stored from 0

Page 38: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 12 Answer (vii)public void offRange(int first, int second)throws wrongSwitchNumberException, wrongRangeException{if (first>second) throw new wrongRangeException();if (first<1 ||first>noSwitches || second<1 || second>noSwitches) throw new wrongSwitchNumberException();(for int j =first-1 ;j++;j<second) states[j] = 0;}

Notice the error, this is the sort of mistake made in the heat of the moment in an exam, we would not deductany marks for this.

Page 39: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 13 (i)A Java-based system is to be developed which administersa number of message queues in a telecommunication system.Each queue is associated with a computer name such as AS400or Alpha1.

Develop a class which associates these names with message queues. A number of methods are required. First, a constructorwith one int argument is required which allocates an initial amount of space for the queues given by the argument [5].Second a method moveFrom is required. This has two String

Page 40: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 13 (ii)arguments which are computer names. The method takes the message at the head of of the queue identified by the second argument and places it at the end of the queue associated with thesecond argument [7]. The third method moveAll has two stringarguments which are computers, it takes the queue associatedwith the second argument and transfers it to the end of the queueassociated with the first argument. The second argument then points at an empty queue.

For this question provide code for all the class. Assume the existence of a class Queue which has three methods. The firstcount returns with the number of messages in a Queue object.The second remove removes the message at the head of a queue and returns with the message. The third addMessage has a

Page 41: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 13 (iii)single argument which is a Message object which is added to the end of a Queue. Also assume the existence of a class Message which describes messages.

Page 42: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 13 Answer (i)class CompQueues{Hashtable assoc;

CompQueues(int no){assoc = new Hashtable(no);}

Page 43: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 13 Answer (ii)

public void moveFrom(String s1, String s2){Queue q1 = (Queue) assoc.get(s1); q2 = (Queue) assoc.get(s2);Message m = q2.remove();q1.addMessage(m);put(s1,q1);put(s2,q2);}

Remember if you are asked to use an unfamilarclass such as Hashtable then the class documentation will beprovided as part of the exam.

Page 44: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 13 Answer (iii)

public void moveAll(String s1, String s2){Queue q1 = (Queue)assoc.get(s1), q2 = (Queue) assoc.get(s2);while(q2.count()!=0){ q1.addMessage(q2.remove());}put(s1,q1);put(s2,q2);}

Page 45: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 14A distributed system consist of a number of clients and servers. A server is a

computer which provides some service such as returning a collection of files,

and a client is a computer which calls on a server for a service. Both clients

and servers are identifed by their IP address. One of the problems with distributed

systems is that physical details are hardwired into their code. This means that

when the physical details of a server changes, for example the IP address, then

some re-programming has to take place. One solution to this problem is to use a computer

known as a dispatcher. This associates a symbolic name to the physical details of a

server, so that whenever a client wishes to connect to a server it uses a symbolic name.

What facilities of Java would be used to implement a distributed system using a dispatcher?

[25]

Page 46: The M874 Exam How to cope with it. General Points (1) Two sections, first generally easier than second. The second section mainly asks you to generate.

Question 14 answer

This is note form. Examples should be provided.

There are four facilities:

The use of the Socket class for implementing client facilities. [6]The use of the ServerSocket class for implementing server facilities. [6]The use of some data structure such as a Hashtable or Properties for associating symbolic names with physical details. [6]The use of InputStream and OutputStream classes for establishing connections between the dispatcher and the client and the client and the server. [4]The use of the String class for setting up and decoding some protocol between clients, servers and the dispatcher. [3]