SlideShare a Scribd company logo
PAPER CODE NO. EXAMINER : Grant Malcolm
COMP213 DEPARTMENT : Computer Science Tel. No. 795 4244
FIRST SEMESTER EXAMINATIONS 2012/13
Advanced Object-Oriented Programming
TIME ALLOWED : Two hours
INSTRUCTIONS TO CANDIDATES
Answer four questions.
If you attempt to answer more questions than the required number of questions (in any section),
the marks awarded for the excess questions answered will be discarded (starting with your
lowest mark).
PAPER CODE COMP213 page 1 of 9 Continued
1. Below is a Maude specification of ‘search’ trees of integers. A search tree is a binary tree
that is either empty (written as the constant ‘null’), or is of the form node(T1, I, T2), where
I is an integer (we call it the ‘node value’ of the tree) and T1 and T2 are search trees (we
call them the left and right subtrees of the tree), with the property that all the integers in the
left subtree T1 are less than I, and all the integers in the right subtree T2 are greater than
I — moreover, all the nodes in T1 and T2 have the same property: all integers in the left
subtree are less than the node value, and all integers in the right subtree are greater than the
node value.
fmod SEARCH-TREES is
protecting INT .
sort SearchTree .
op null : -> SearchTree [ ctor ] .
op node : SearchTree Int SearchTree -> SearchTree [ ctor ] .
op height : SearchTree -> Int .
op insert : SearchTree Int -> SearchTree .
vars I J : Int .
vars T1 T2 : SearchTree .
eq height(null) = 0 .
eq height(node(T1, I, T2)) = max(height(T1), height(T2)) + 1 .
eq insert(I, null) = node(null, I, null) .
eq insert(I, node(T1, I, T2)) = node(T1, I, T2) .
cq insert(I, node(T1, J, T2)) = node(insert(I, T1), J, T2) if I < J .
cq insert(I, node(T1, J, T2)) = node(T1, J, insert(I, T2)) if I > J .
endfm
Operations null and node are structural operations that provide a binary tree structure; it
is possible to use these to construct trees that do not have the intended property that all
integers in left subtrees are less than the node value and all integers in right subtrees are
greater than the node value. For example,
node(node(null, 3, null), 2, node(null, 1, null))
does not have that property, as 3 is in the left subtree, but is not less than 2. However, the
operation insert will ensure that this property holds: if it is called from a search tree, then
the new value will be inserted at the appropriate point.
(a) Give a Java implementation of class SearchTree that uses a class, TreeNode, to
provide the binary tree structure (in a similar way to how a class Node can be used
to provide a linked list structure). Class TreeNode should also provide methods
height() and insert(). Marks will be awarded for:
i. correct implementation of the binary tree structure using class TreeNode
[3 marks]
PAPER CODE COMP213 page 2 of 9 Continued
ii. correct implementation of the constant null as a SearchTree constructor
[1 marks]
iii. correct implementation of node as a TreeNode constructor [2 marks]
iv. correct implementation of height() in class SearchTree [2 marks]
v. correct implementation of insert() in class SearchTree [3 marks]
vi. correct implementation of height() in class TreeNode [2 marks]
vii. correct implementation of insert() in class TreeNode [6 marks]
viii. appropriate use of scope modifiers. [2 marks]
(b) Suppose the class TreeNode is declared as a protected inner class within the class
SearchTree.
i. Where precisely would TreeNode and its public members be visible?
[3 marks]
ii. Where would any private members of TreeNode be visible? [1 mark]
PAPER CODE COMP213 page 3 of 9 Continued
2. Message Boards provide a forum for communication by maintaining lists of messages that
have been sent by users, which can be viewed and added to by other users. Messages are
usually organised by topic. Users can start a new topic, or view a list of all topics that have
been started by other users. From this list they can choose a topic and view the messages in
it. Users can also add a message to a particular topic. The list of topics is typically sorted
by the time a topic was last added to, with topics that were most recently added to coming
first. This question asks you to specify lists of Topics in Maude; for simplicity, we will not
require that most-recently updated threads be moved to the start of the list.
(a) What exactly is an Abstract Data Type? [3 marks]
(b) Give a Maude specification of an abstract data type of Messages, where a Message
consists of the name of the sender, and the text of the message, both of which are
strings. There are three operations:
• newMessage, which creates a message, given two strings (the sender’s name, and
the message text);
• getSender, which returns the name of a given message’s sender (as a string); and
• getText, which returns the text of a given message (as a string).
newMessage is a structural operation (ctor); you will need equations to define the
behaviour of getSender and getText: both take a Message as argument. [6 marks]
(c) Give a Maude specification of an abstract data type of Topics, where a Topic is a non-
empty list of Messages (i.e., every Topic has at least one Message), and a Topic also
has a title (which is a String). The abstract data type has the following operations:
• newTopic, which creates a Topic, given the Topic’s title (a String), and a Message
(the first Message in the Topic);
• addMessage, which takes a Topic and a message, and returns a Topic (the Topic
with one more Message added to it); and
• getTitle, which returns the title of the Topic (as a String).
NB: both newTopic and addMessage are structural operations (ctors); you will need
equations to define the behaviour of getTitle. [7 marks]
(d) Give a Maude specification of Topic Lists, which are lists of topics. There are two
structural operations (ctors):
• empty, a constant representing the empty list of Topics, and
• add, which takes a Topic and a Topic List, and returns a Topic List.
In addition, there are the following operations:
• addNewTopic, which takes a Message and a String (the title of the new Topic)
and a Topic List, and creates a new Topic with the given Message and title, and
adds it to the start of the given Topic List; and
• addMessageToTopic, which takes a Message, a String (the title of the Topic to
add the Message to), and a Topic List, and returns the Topic List, where the Topic
with the given title has had the given Message added to it. If there is no Topic
with the given title in the Topic List, then the operation leaves the list unchanged.
NB: you will need equations defining the behaviour of these two operations.
[9 marks]
PAPER CODE COMP213 page 4 of 9 Continued
3. Consider the following class of binary trees. The method isInOrder() is intended to test
whether the top label of the tree is greater than the top label of the left subtree and smaller
than the top label of the left subtree.
class BinaryTree
{
private BinaryTree leftSubtree;
private int label;
private BinaryTree rightSubtree;
BinaryTree(BinaryTree left, int val, BinaryTree right)
{
leftSubtree = left;
label = val;
rightSubtree = right;
}
public int getLabel()
{
return label;
}
public boolean isInOrder()
{
return leftSubtree.getLabel() < label
&& label < rightSubtree.getLabel();
}
public static void main(String[] args)
{
BinaryTree bt = new BinaryTree(null, 5, null);
bt = new BinaryTree(bt, 7, null);
System.out.println(bt.isInOrder());
}
}
(a) Briefly describe the function of the ‘method-call stack’ in the Java interpreter.
[3 marks]
(b) What happens when the main method in the BinaryTree class is executed? Describe
the state of the method-call stack during the execution of this main method.
[5 marks]
(c) Briefly describe the differences between ‘checked’ and ‘unchecked’ exceptions.
[4 marks]
(d) Write a checked exception class, NoSubtreeException, and modify the isInOrder()
method so that it throws a NoSubtreeException if either the left or right subtree
is null. What other changes would be necessary to the class? [6 marks]
PAPER CODE COMP213 page 5 of 9 Continued
(e) What is meant by a ‘class invariant’? [2 marks]
(f) Modify the constructor of class BinaryTree so that it throws a checked exception if
either:
• the parameter left is not null and its label is not smaller than parameter val, or
• the parameter right is not null and its label is not greater than parameter val.
With this modification, is the property that isInOrder() always returns true a class
invariant for BinaryTree? [5 marks]
PAPER CODE COMP213 page 6 of 9 Continued
4. Consider the following two classes.
class FriendFinderThread extends Thread {
// number of FriendBook friends
int numFriends = 0;
public void run() {
// join, then leave FriendBook
FriendBook.join(this);
try { Thread.sleep(10000); }
catch (InterruptedException(ie) {}
FriendBook.leave(this);
}
}
class FriendBook {
// list of FriendBook members
static Vector<FriendFinderThread> members =
new Vector<FriendFinderThread>();
static void join(FriendFinderThread f) {
// add a new friend to all existing members
int size = members.size();
for (int i = 0; i < size; i++) {
members.elementAt(i).numFriends++;
}
f.numFriends = size; // new member’s friends
members.add(f); // add to list of members
}
static void leave(FriendFinderThread f) {
members.remove(f); // remove from list
int size = members.size();
for (int i = 0; i < size; i++) {
members.elementAt(i).numFriends--;
}
}
public static void main() {
for (int n = 0; n < 100; n++) {
new FriendFinderThread().start();
}
}
}
PAPER CODE COMP213 page 7 of 9 Continued
Class FriendBook maintains a list of its ‘members’; a FriendFinderThread in-
stance becomes a member by calling the join() method. While it is a member of
FriendBook, the numFriends field should be equal to the total number of members.
When a new FriendFinderThread instance becomes a member, the join() method
increments the numFriends field of all existing members; similarly, the leave() method
decrements these fields.
(a) The method start() in class Thread will place a new thread in a pool of threads that
are ‘ready’ to run under time-slicing. The Java interpreter will select a thread from this
pool to ‘run’ (i.e., execute the thread). ‘Ready’ and ‘running’ are two of the possible
states that threads may be in, and threads move between these states under the control
of the Java interpreter’s time-slicing mechanism. What other states can threads be in,
and how do they change from one state to another? [6 marks]
(b) One problem that might arise in the FriendBook program is ‘interference’. Briefly
say what interference is, and describe how it might arise in this program. [6 marks]
(c) Describe how synchronization is used in Java to prevent interference. [6 marks]
(d) Briefly describe the difference between a synchronized method and a synchronized
block of code. [3 marks]
(e) How would you use synchronization to prevent interference in the FriendBook
program? Justify your answer. [4 marks]
PAPER CODE COMP213 page 8 of 9 Continued
5. The class BinaryTree in Question 3 stores integers in a binary tree structure, and method
isInOrder() tests whether the top label of the tree is greater than the top label of the
left subtree and smaller than the top label of the left subtree. A generic form of that class
could store instances of any class that implements the Comparable interface:
public interface Comparable<T>
{
public int compareTo(T o);
}
where instance.compareTo(arg) returns a negative value if instance is less
than arg, 0 if instance and arg are equal, and a positive value if instance is greater
than arg. For example, class Integer implements Comparable<Integer> with
instance.compareTo(arg) returning instance - arg.
(a) Give a generic version of class BinaryTree that stores instances of a type parameter E,
with the requirement that E extends Comparable<E>. Note that you will need to
change the code in the isInOrder() method so that it uses compareTo(). Do
not include a main method in your answer. [6 marks]
(b) Modify the main method of BinaryTree so that it creates instances of type
BinaryTree<Integer> (this is acceptable, since Integer implements
Comparable<Integer>). [4 marks]
(c) Generic types are implemented in Java by ‘erasure’.
i. Briefly describe what erasure is. [4 marks]
ii. Give the result of erasure on the interface Comparable<T>. [2 marks]
iii. Give the result of erasure on your answer to part (b) (the main method).
[2 marks]
iv. For the generic class BinaryTree in your answer to part (a), why should erasure
not replace the type parameter E with Object? [1 mark]
v. Give the result of erasure on your answer to part (a) (generic class BinaryTree).
[6 marks]
PAPER CODE COMP213 page 9 of 9 End

More Related Content

What's hot

Chap1 array
Chap1 arrayChap1 array
Chap1 array
raksharao
 
R교육1
R교육1R교육1
R교육1
Kangwook Lee
 
Programming with Python - Week 2
Programming with Python - Week 2Programming with Python - Week 2
Programming with Python - Week 2
Ahmet Bulut
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Introduction to Data Structure : Pointer
Introduction to Data Structure : PointerIntroduction to Data Structure : Pointer
Introduction to Data Structure : Pointer
S P Sajjan
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
FALLEE31188
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
MuhammadUmerShakir
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
Ahmet Bulut
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Celine George
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
MLG College of Learning, Inc
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
Haitham El-Ghareeb
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
Shahriar Robbani
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
DurgaDeviCbit
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
Haitham El-Ghareeb
 
Data structures using C
Data structures using CData structures using C
Data structures using C
Pdr Patnaik
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
Prof. Dr. K. Adisesha
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
Lesson 3 simple sorting
Lesson 3   simple sortingLesson 3   simple sorting
Lesson 3 simple sorting
MLG College of Learning, Inc
 

What's hot (18)

Chap1 array
Chap1 arrayChap1 array
Chap1 array
 
R교육1
R교육1R교육1
R교육1
 
Programming with Python - Week 2
Programming with Python - Week 2Programming with Python - Week 2
Programming with Python - Week 2
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Introduction to Data Structure : Pointer
Introduction to Data Structure : PointerIntroduction to Data Structure : Pointer
Introduction to Data Structure : Pointer
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Lesson 3 simple sorting
Lesson 3   simple sortingLesson 3   simple sorting
Lesson 3 simple sorting
 

Viewers also liked

07
0707
Lo3
Lo3Lo3
Lo3
liankei
 
Kulturalna wycieczka po Kobyłce
Kulturalna wycieczka po KobyłceKulturalna wycieczka po Kobyłce
Kulturalna wycieczka po Kobyłce
sp2zabki
 
Wir sind alle kinder dieser welt
Wir sind alle kinder dieser weltWir sind alle kinder dieser welt
Wir sind alle kinder dieser weltsp2zabki
 
Prezentacja z wycieczki do Rogowa klas IVc oraz IVd
Prezentacja z wycieczki do Rogowa klas IVc oraz IVdPrezentacja z wycieczki do Rogowa klas IVc oraz IVd
Prezentacja z wycieczki do Rogowa klas IVc oraz IVd
sp2zabki
 
755 Cordova Bay Rd - Open House Presentation
755 Cordova Bay Rd - Open House Presentation755 Cordova Bay Rd - Open House Presentation
755 Cordova Bay Rd - Open House Presentation
AragonProperties
 
Internet1
Internet1Internet1
Internet1
sp2zabki
 
Płock
PłockPłock
Płock
sp2zabki
 
Płock1
Płock1Płock1
Płock1
sp2zabki
 
Bezpieczny internet
Bezpieczny internet Bezpieczny internet
Bezpieczny internet
sp2zabki
 
Prezentacja
PrezentacjaPrezentacja
Prezentacja
sp2zabki
 
Dbi
DbiDbi
Bezpieczeństwo w sieci prezentacja power point
Bezpieczeństwo w sieci prezentacja power pointBezpieczeństwo w sieci prezentacja power point
Bezpieczeństwo w sieci prezentacja power point
sp2zabki
 

Viewers also liked (13)

07
0707
07
 
Lo3
Lo3Lo3
Lo3
 
Kulturalna wycieczka po Kobyłce
Kulturalna wycieczka po KobyłceKulturalna wycieczka po Kobyłce
Kulturalna wycieczka po Kobyłce
 
Wir sind alle kinder dieser welt
Wir sind alle kinder dieser weltWir sind alle kinder dieser welt
Wir sind alle kinder dieser welt
 
Prezentacja z wycieczki do Rogowa klas IVc oraz IVd
Prezentacja z wycieczki do Rogowa klas IVc oraz IVdPrezentacja z wycieczki do Rogowa klas IVc oraz IVd
Prezentacja z wycieczki do Rogowa klas IVc oraz IVd
 
755 Cordova Bay Rd - Open House Presentation
755 Cordova Bay Rd - Open House Presentation755 Cordova Bay Rd - Open House Presentation
755 Cordova Bay Rd - Open House Presentation
 
Internet1
Internet1Internet1
Internet1
 
Płock
PłockPłock
Płock
 
Płock1
Płock1Płock1
Płock1
 
Bezpieczny internet
Bezpieczny internet Bezpieczny internet
Bezpieczny internet
 
Prezentacja
PrezentacjaPrezentacja
Prezentacja
 
Dbi
DbiDbi
Dbi
 
Bezpieczeństwo w sieci prezentacja power point
Bezpieczeństwo w sieci prezentacja power pointBezpieczeństwo w sieci prezentacja power point
Bezpieczeństwo w sieci prezentacja power point
 

Similar to 05

Scala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To KnowScala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To Know
Lightbend
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docx
jeanettehully
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
Mahmoud Samir Fayed
 
Binary trees
Binary treesBinary trees
Binary trees
Ssankett Negi
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
Sharbani Bhattacharya
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
3.ppt
3.ppt3.ppt
3.ppt
3.ppt3.ppt
Advanced Data Structures And Algorithms Roadmap By ScholarHat PDF
Advanced Data Structures And Algorithms Roadmap By ScholarHat PDFAdvanced Data Structures And Algorithms Roadmap By ScholarHat PDF
Advanced Data Structures And Algorithms Roadmap By ScholarHat PDF
Scholarhat
 
Most Asked Coding Questions .pdf
Most Asked Coding Questions .pdfMost Asked Coding Questions .pdf
Most Asked Coding Questions .pdf
krishna415649
 
Java execise
Java execiseJava execise
Java execise
Keneth miles
 
GSP 125 Final Exam Guide
GSP 125 Final Exam GuideGSP 125 Final Exam Guide
GSP 125 Final Exam Guide
monsterr20
 
Gsp 125 final exam guide
Gsp 125 final exam guideGsp 125 final exam guide
Gsp 125 final exam guide
williamsoncolling14
 
Recursion Lecture in Java
Recursion Lecture in JavaRecursion Lecture in Java
Recursion Lecture in Java
Raffi Khatchadourian
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxAssg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
festockton
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
MemeMiner
 
611+tutorial
611+tutorial611+tutorial
611+tutorial
Prudence Mashile
 

Similar to 05 (20)

Scala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To KnowScala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To Know
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docx
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
 
Binary trees
Binary treesBinary trees
Binary trees
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Advanced Data Structures And Algorithms Roadmap By ScholarHat PDF
Advanced Data Structures And Algorithms Roadmap By ScholarHat PDFAdvanced Data Structures And Algorithms Roadmap By ScholarHat PDF
Advanced Data Structures And Algorithms Roadmap By ScholarHat PDF
 
Most Asked Coding Questions .pdf
Most Asked Coding Questions .pdfMost Asked Coding Questions .pdf
Most Asked Coding Questions .pdf
 
Java execise
Java execiseJava execise
Java execise
 
GSP 125 Final Exam Guide
GSP 125 Final Exam GuideGSP 125 Final Exam Guide
GSP 125 Final Exam Guide
 
Gsp 125 final exam guide
Gsp 125 final exam guideGsp 125 final exam guide
Gsp 125 final exam guide
 
Recursion Lecture in Java
Recursion Lecture in JavaRecursion Lecture in Java
Recursion Lecture in Java
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxAssg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
611+tutorial
611+tutorial611+tutorial
611+tutorial
 

More from liankei

Lo 19
Lo 19Lo 19
Lo 19
liankei
 
Lo 18
Lo 18Lo 18
Lo 18
liankei
 
Lo 13
Lo 13Lo 13
Lo 13
liankei
 
Lo 12
Lo 12Lo 12
Lo 12
liankei
 
Lo 11
Lo 11Lo 11
Lo 11
liankei
 
Lo 06
Lo 06Lo 06
Lo 06
liankei
 
Lo 05
Lo 05Lo 05
Lo 05
liankei
 
Lo 04
Lo 04Lo 04
Lo 04
liankei
 
Lo 01
Lo 01Lo 01
Lo 01
liankei
 
Lo 16
Lo 16Lo 16
Lo 16
liankei
 
Lo 15
Lo 15Lo 15
Lo 15
liankei
 
Lo 09
Lo 09Lo 09
Lo 09
liankei
 
Lo 08
Lo 08Lo 08
Lo 08
liankei
 
Lo 03
Lo 03Lo 03
Lo 03
liankei
 
Lo 20
Lo 20Lo 20
Lo 20
liankei
 
Lo19
Lo19Lo19
Lo19
liankei
 
Lo20
Lo20Lo20
Lo20
liankei
 
Lo17
Lo17Lo17
Lo17
liankei
 
Lo18
Lo18Lo18
Lo18
liankei
 
Lo15
Lo15Lo15
Lo15
liankei
 

More from liankei (20)

Lo 19
Lo 19Lo 19
Lo 19
 
Lo 18
Lo 18Lo 18
Lo 18
 
Lo 13
Lo 13Lo 13
Lo 13
 
Lo 12
Lo 12Lo 12
Lo 12
 
Lo 11
Lo 11Lo 11
Lo 11
 
Lo 06
Lo 06Lo 06
Lo 06
 
Lo 05
Lo 05Lo 05
Lo 05
 
Lo 04
Lo 04Lo 04
Lo 04
 
Lo 01
Lo 01Lo 01
Lo 01
 
Lo 16
Lo 16Lo 16
Lo 16
 
Lo 15
Lo 15Lo 15
Lo 15
 
Lo 09
Lo 09Lo 09
Lo 09
 
Lo 08
Lo 08Lo 08
Lo 08
 
Lo 03
Lo 03Lo 03
Lo 03
 
Lo 20
Lo 20Lo 20
Lo 20
 
Lo19
Lo19Lo19
Lo19
 
Lo20
Lo20Lo20
Lo20
 
Lo17
Lo17Lo17
Lo17
 
Lo18
Lo18Lo18
Lo18
 
Lo15
Lo15Lo15
Lo15
 

Recently uploaded

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 

Recently uploaded (20)

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 

05

  • 1. PAPER CODE NO. EXAMINER : Grant Malcolm COMP213 DEPARTMENT : Computer Science Tel. No. 795 4244 FIRST SEMESTER EXAMINATIONS 2012/13 Advanced Object-Oriented Programming TIME ALLOWED : Two hours INSTRUCTIONS TO CANDIDATES Answer four questions. If you attempt to answer more questions than the required number of questions (in any section), the marks awarded for the excess questions answered will be discarded (starting with your lowest mark). PAPER CODE COMP213 page 1 of 9 Continued
  • 2. 1. Below is a Maude specification of ‘search’ trees of integers. A search tree is a binary tree that is either empty (written as the constant ‘null’), or is of the form node(T1, I, T2), where I is an integer (we call it the ‘node value’ of the tree) and T1 and T2 are search trees (we call them the left and right subtrees of the tree), with the property that all the integers in the left subtree T1 are less than I, and all the integers in the right subtree T2 are greater than I — moreover, all the nodes in T1 and T2 have the same property: all integers in the left subtree are less than the node value, and all integers in the right subtree are greater than the node value. fmod SEARCH-TREES is protecting INT . sort SearchTree . op null : -> SearchTree [ ctor ] . op node : SearchTree Int SearchTree -> SearchTree [ ctor ] . op height : SearchTree -> Int . op insert : SearchTree Int -> SearchTree . vars I J : Int . vars T1 T2 : SearchTree . eq height(null) = 0 . eq height(node(T1, I, T2)) = max(height(T1), height(T2)) + 1 . eq insert(I, null) = node(null, I, null) . eq insert(I, node(T1, I, T2)) = node(T1, I, T2) . cq insert(I, node(T1, J, T2)) = node(insert(I, T1), J, T2) if I < J . cq insert(I, node(T1, J, T2)) = node(T1, J, insert(I, T2)) if I > J . endfm Operations null and node are structural operations that provide a binary tree structure; it is possible to use these to construct trees that do not have the intended property that all integers in left subtrees are less than the node value and all integers in right subtrees are greater than the node value. For example, node(node(null, 3, null), 2, node(null, 1, null)) does not have that property, as 3 is in the left subtree, but is not less than 2. However, the operation insert will ensure that this property holds: if it is called from a search tree, then the new value will be inserted at the appropriate point. (a) Give a Java implementation of class SearchTree that uses a class, TreeNode, to provide the binary tree structure (in a similar way to how a class Node can be used to provide a linked list structure). Class TreeNode should also provide methods height() and insert(). Marks will be awarded for: i. correct implementation of the binary tree structure using class TreeNode [3 marks] PAPER CODE COMP213 page 2 of 9 Continued
  • 3. ii. correct implementation of the constant null as a SearchTree constructor [1 marks] iii. correct implementation of node as a TreeNode constructor [2 marks] iv. correct implementation of height() in class SearchTree [2 marks] v. correct implementation of insert() in class SearchTree [3 marks] vi. correct implementation of height() in class TreeNode [2 marks] vii. correct implementation of insert() in class TreeNode [6 marks] viii. appropriate use of scope modifiers. [2 marks] (b) Suppose the class TreeNode is declared as a protected inner class within the class SearchTree. i. Where precisely would TreeNode and its public members be visible? [3 marks] ii. Where would any private members of TreeNode be visible? [1 mark] PAPER CODE COMP213 page 3 of 9 Continued
  • 4. 2. Message Boards provide a forum for communication by maintaining lists of messages that have been sent by users, which can be viewed and added to by other users. Messages are usually organised by topic. Users can start a new topic, or view a list of all topics that have been started by other users. From this list they can choose a topic and view the messages in it. Users can also add a message to a particular topic. The list of topics is typically sorted by the time a topic was last added to, with topics that were most recently added to coming first. This question asks you to specify lists of Topics in Maude; for simplicity, we will not require that most-recently updated threads be moved to the start of the list. (a) What exactly is an Abstract Data Type? [3 marks] (b) Give a Maude specification of an abstract data type of Messages, where a Message consists of the name of the sender, and the text of the message, both of which are strings. There are three operations: • newMessage, which creates a message, given two strings (the sender’s name, and the message text); • getSender, which returns the name of a given message’s sender (as a string); and • getText, which returns the text of a given message (as a string). newMessage is a structural operation (ctor); you will need equations to define the behaviour of getSender and getText: both take a Message as argument. [6 marks] (c) Give a Maude specification of an abstract data type of Topics, where a Topic is a non- empty list of Messages (i.e., every Topic has at least one Message), and a Topic also has a title (which is a String). The abstract data type has the following operations: • newTopic, which creates a Topic, given the Topic’s title (a String), and a Message (the first Message in the Topic); • addMessage, which takes a Topic and a message, and returns a Topic (the Topic with one more Message added to it); and • getTitle, which returns the title of the Topic (as a String). NB: both newTopic and addMessage are structural operations (ctors); you will need equations to define the behaviour of getTitle. [7 marks] (d) Give a Maude specification of Topic Lists, which are lists of topics. There are two structural operations (ctors): • empty, a constant representing the empty list of Topics, and • add, which takes a Topic and a Topic List, and returns a Topic List. In addition, there are the following operations: • addNewTopic, which takes a Message and a String (the title of the new Topic) and a Topic List, and creates a new Topic with the given Message and title, and adds it to the start of the given Topic List; and • addMessageToTopic, which takes a Message, a String (the title of the Topic to add the Message to), and a Topic List, and returns the Topic List, where the Topic with the given title has had the given Message added to it. If there is no Topic with the given title in the Topic List, then the operation leaves the list unchanged. NB: you will need equations defining the behaviour of these two operations. [9 marks] PAPER CODE COMP213 page 4 of 9 Continued
  • 5. 3. Consider the following class of binary trees. The method isInOrder() is intended to test whether the top label of the tree is greater than the top label of the left subtree and smaller than the top label of the left subtree. class BinaryTree { private BinaryTree leftSubtree; private int label; private BinaryTree rightSubtree; BinaryTree(BinaryTree left, int val, BinaryTree right) { leftSubtree = left; label = val; rightSubtree = right; } public int getLabel() { return label; } public boolean isInOrder() { return leftSubtree.getLabel() < label && label < rightSubtree.getLabel(); } public static void main(String[] args) { BinaryTree bt = new BinaryTree(null, 5, null); bt = new BinaryTree(bt, 7, null); System.out.println(bt.isInOrder()); } } (a) Briefly describe the function of the ‘method-call stack’ in the Java interpreter. [3 marks] (b) What happens when the main method in the BinaryTree class is executed? Describe the state of the method-call stack during the execution of this main method. [5 marks] (c) Briefly describe the differences between ‘checked’ and ‘unchecked’ exceptions. [4 marks] (d) Write a checked exception class, NoSubtreeException, and modify the isInOrder() method so that it throws a NoSubtreeException if either the left or right subtree is null. What other changes would be necessary to the class? [6 marks] PAPER CODE COMP213 page 5 of 9 Continued
  • 6. (e) What is meant by a ‘class invariant’? [2 marks] (f) Modify the constructor of class BinaryTree so that it throws a checked exception if either: • the parameter left is not null and its label is not smaller than parameter val, or • the parameter right is not null and its label is not greater than parameter val. With this modification, is the property that isInOrder() always returns true a class invariant for BinaryTree? [5 marks] PAPER CODE COMP213 page 6 of 9 Continued
  • 7. 4. Consider the following two classes. class FriendFinderThread extends Thread { // number of FriendBook friends int numFriends = 0; public void run() { // join, then leave FriendBook FriendBook.join(this); try { Thread.sleep(10000); } catch (InterruptedException(ie) {} FriendBook.leave(this); } } class FriendBook { // list of FriendBook members static Vector<FriendFinderThread> members = new Vector<FriendFinderThread>(); static void join(FriendFinderThread f) { // add a new friend to all existing members int size = members.size(); for (int i = 0; i < size; i++) { members.elementAt(i).numFriends++; } f.numFriends = size; // new member’s friends members.add(f); // add to list of members } static void leave(FriendFinderThread f) { members.remove(f); // remove from list int size = members.size(); for (int i = 0; i < size; i++) { members.elementAt(i).numFriends--; } } public static void main() { for (int n = 0; n < 100; n++) { new FriendFinderThread().start(); } } } PAPER CODE COMP213 page 7 of 9 Continued
  • 8. Class FriendBook maintains a list of its ‘members’; a FriendFinderThread in- stance becomes a member by calling the join() method. While it is a member of FriendBook, the numFriends field should be equal to the total number of members. When a new FriendFinderThread instance becomes a member, the join() method increments the numFriends field of all existing members; similarly, the leave() method decrements these fields. (a) The method start() in class Thread will place a new thread in a pool of threads that are ‘ready’ to run under time-slicing. The Java interpreter will select a thread from this pool to ‘run’ (i.e., execute the thread). ‘Ready’ and ‘running’ are two of the possible states that threads may be in, and threads move between these states under the control of the Java interpreter’s time-slicing mechanism. What other states can threads be in, and how do they change from one state to another? [6 marks] (b) One problem that might arise in the FriendBook program is ‘interference’. Briefly say what interference is, and describe how it might arise in this program. [6 marks] (c) Describe how synchronization is used in Java to prevent interference. [6 marks] (d) Briefly describe the difference between a synchronized method and a synchronized block of code. [3 marks] (e) How would you use synchronization to prevent interference in the FriendBook program? Justify your answer. [4 marks] PAPER CODE COMP213 page 8 of 9 Continued
  • 9. 5. The class BinaryTree in Question 3 stores integers in a binary tree structure, and method isInOrder() tests whether the top label of the tree is greater than the top label of the left subtree and smaller than the top label of the left subtree. A generic form of that class could store instances of any class that implements the Comparable interface: public interface Comparable<T> { public int compareTo(T o); } where instance.compareTo(arg) returns a negative value if instance is less than arg, 0 if instance and arg are equal, and a positive value if instance is greater than arg. For example, class Integer implements Comparable<Integer> with instance.compareTo(arg) returning instance - arg. (a) Give a generic version of class BinaryTree that stores instances of a type parameter E, with the requirement that E extends Comparable<E>. Note that you will need to change the code in the isInOrder() method so that it uses compareTo(). Do not include a main method in your answer. [6 marks] (b) Modify the main method of BinaryTree so that it creates instances of type BinaryTree<Integer> (this is acceptable, since Integer implements Comparable<Integer>). [4 marks] (c) Generic types are implemented in Java by ‘erasure’. i. Briefly describe what erasure is. [4 marks] ii. Give the result of erasure on the interface Comparable<T>. [2 marks] iii. Give the result of erasure on your answer to part (b) (the main method). [2 marks] iv. For the generic class BinaryTree in your answer to part (a), why should erasure not replace the type parameter E with Object? [1 mark] v. Give the result of erasure on your answer to part (a) (generic class BinaryTree). [6 marks] PAPER CODE COMP213 page 9 of 9 End