Question
A Java game designer wishes to store all the game preferences (e.g., player name, screen size, music volume, etc.) wisthin a custom Preference class. (i)
A Java game designer wishes to store all the game preferences (e.g., player name,
screen size, music volume, etc.) wisthin a custom Preference class.
(i) Assuming each preference is stored as a unique String key mapping to
a String value, give a simple implementation ofs Preference that allows
for effiffifficiently setting or updating preferences and retrieving previously set
ones. Your implementation should defifine an exception that is thrown when
a preference key is requested but not present. [5 marks]
(ii) It is important that only one Preference object exists in a running game.
Show how to apply access modififiers and the Singleton design pattern to
ensure this. Your implementation should lazily instantiate the object. Is it
necessary to make your class final or Cloneable? Explain your answer.
[6 marks]
(c) The designer also implements other Singleton classes in the game and proposes
to create a SingletonBase base class from which all such classes would inherit
the singleton behaviour. By providing example Java code, explain why this is
not viable. [5 marks]
4CST.2014.1.5
4
Object-Oriented Programming
A Lecturer wishes to create a program that lists his students sorted by the number
of practical assignments they have completed. The listing should be greatest number
of assignments fifirst, sub-sorted by name in lexicographical order (A to Z).
A class StudentInfo stores the name and number of assignments completed for a
student. Amongst other methods, it contains a void setCompleted(int n) method
that allows changes to the number of completed assignments.
(a) Provide a defifinition of StudentInfo with an equals() method and a natural
ordering that matches the given requirement. [9 marks]
(b) A TreeSet is used to maintain the StudentInfo objects in appropriate order.
When setCompleted(...) is called on a StudentInfo object it is necessary
to remove the object from the set, change the value and then reinsert it to
ensure the correct ordering. This is to be automated by applying the Observer
design pattern via classes UpdatableTreeSet and SubscribableStudentInfo.
A partial defifinition of UpdatableTreeSet is provided below.
public class UpdatableTreeSet extends
TreeSet {
// To be called just before the StudentInfo object is updated
public void beforeUpdate(SubscribableStudentInfo s) {
remove(s);
}
// To be called just after the StudentInfo object is updated
public void afterUpdate(SubscribableStudentInfo s) {
add(s);
}
}
(i) Extend StudentInfo to create SubscribableStudentInfo such that: mul
tiple UpdatableTreeSet objects can subscribe and unsubscribe to receive
updates from it; and the beforeUpdate(...) and afterUpdate(...)
methods are called appropriately on the subscribed UpdatableTreeSet
objects whenever setCompleted(...) is called. [6 marks]
Explain the purpose of access modififiers in OOP languages. [2 marks]
(ii) Copy and complete the table below to show the access restrictions for the
four access modififiers in Java. [2 marks]
Access Modififier
Defifining class
Class in same package
Subclass in difffferent package
Non-subclass in difffferent package
(b) A Java game designer wishes to store all the game preferences (e.g., player name,
screen size, music volume, etc.) within a custom Preference class.
(i) Assuming each preference is stored as a unique String key mapping to
a String value, give a simple implementation of Preference that allows
for effiffifficiently setting or updating preferences and retrieving previously set
ones. Your implementation should defifine an exception that is thrown when
a preference key is requested but not present. [5 marks]
(ii) It is important that only one Preference object exists in a running game.
Show how to apply access modififiers and the Singleton design pattern to
ensure this. Your implementation should lazily instantiate the object. Is it
necessary to make your class final or Cloneable? Explain your answer.
[6 marks]
(c) The designer also implements other Singleton classes in the game and proposes
to create a SingletonBase base class from which all such classes would inherit
the singleton behaviour. By providing example Java code, explain why this is
not viable. [5 marks]
4CST.2014.1.5
4
Object-Oriented Programming
A Lecturer wishes to create a program that lists his students sorted by the number
of practical assignments they have completed. The listing should be greatest number
of assignments fifirst, sub-sorted by name in lexicographical order (A to Z).
A class StudentInfo stores the name and number of assignments completed for a
student. Amongst other methods, it contains a void setCompleted(int n) method
that allows changes to the number of completed assignments.
(a) Provide a defifinition of StudentInfo with an equals() method and a natural
ordering that matches the given requirement. [9 marks]
(b) A TreeSet is used to maintain the StudentInfo objects in appropriate order.
When setCompleted(...) is called on a StudentInfo object it is necessary
to remove the object from the set, change the value and then reinsert it to
ensure the correct ordering. This is to be automated by applying the Observer
design pattern via classes UpdatableTreeSet and SubscribableStudentInfo.
A partial defifinition of UpdatableTreeSet is provided below.
public class UpdatableTreeSet extends
TreeSet {
// To be called just before the StudentInfo object is updated
public void beforeUpdate(SubscribableStudentInfo s) {
remove(s);
}
// To be called just after the StudentInfo object is updated
public void afterUpdate(SubscribableStudentInfo s) {
add(s);
}
}
(i) Extend StudentInfo to create SubscribableStudentInfo such that: mul
tiple UpdatableTreeSet objects can subscribe and unsubscribe to receive
updates from it; and the beforeUpdate(...) and afterUpdate(...)
methods are called appropriately on the subscribed UpdatableTreeSet
objects whenever setCompleted(...) is called. [6 marks]
Telef netwe ANO AS Soc S CA A 1 and are comory and you The Space in the right hand margin indice marks owing questions: mut BOLT A hamizatir Advance Progra BRANCH SE T3 prog DA A a ang papara D AdE 65 ya J pa efoonge The we & Ap Seveche Max G.CO Refe when Am PO T
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started