1.      A set of classes is given as below:

a.       Draw a class inheritance diagram for the above set of classes: (10 points)

Solution: omit

 

  1. Consider the following piece of Java code: (5 points)

TraditionalStudent tradStu = new TraditionalStudent();

tradStu.setEmployer("Hi-Tech");

Student student = tradStu;

String e1 = ((TraditionalStudent)Student).getEmployer();

String e2 = ((NonTraditionalStudent)student).getEmployer();

Explain the result when this piece of code is executed.

 

Solution: Second line will have a syntax error (exception) that TraditionalStudent doesn't have a method setEmployer

        The fourth line has the same error

        The last line will have a run-time error (Class cast error) since student (object of TraditionalStudent) can not cast to NonTraditionalStudent

 

 

2.      Implement the Vector ADT using a singly linked list. Design your implementation class and describe methods using pseudo-code. (15 points)

Solution:

a.      Vector is the same as ArrayList

b.      Use Adaptor Design Pattern

c.       You can also maintain a singly linked list directly

Vector:

        Instance field: SinglyLinkedList sll

        Constructor:

                sll = new SinglyLinkedList()

        Methods:

                isEmpty:                                            sll.isEmpty()

                size:                                                    sll.size()

                get(i) (elemAtRank(i)):                       if (i>size()) error

                                                                            ptr <-- sll.getFirst()

                                                                            rank <--0

                                                                            while (rank < i) ptr <-- ptr.next()

                                                                            return ptr.element()

                set(i, e) (replaceAtRank(i)):                if (i>size()) error

                                                                            ptr <-- sll.getFirst()

                                                                            rank <-- 0

                                                                            while (rank < i) ptr <-- ptr.next();

                                                                            sll.set(ptr, e)

                add(i, e) (insertAtRank(I, e):               if (i>size()) sll.addLast(e)

                                                                            ptr <-- sll.getFirst()

                                                                            rank <-- 0

                                                                            while (rank < i) ptr <-- ptr.next();

                                                                            sll.addBefore(ptr, e)

                remove(i) (removeAtRank(i):             if (i>size()) error

                                                                            ptr <-- sll.getFirst()

                                                                            rank <-- 0

                                                                            while (rank < i) ptr <-- ptr.next();

                                                                            sll.remove(ptr)

 

3.      Design an adaptor class to implement the Queue interface using the methods of the List ADT. (15 points)

Solution:

Queue:

Instance field:      List lst

Constructor:        lst <-- a new empty list

Methods:

        isEmpty:         lst.isEmpty

        size:                lst.size()

        enqueue(e):    lst.addFirst(e)

        dequeue():      lst.remove(lst.last())

        front():           lst.getFirst()

 

 

4.      Asymptotic analysis

a.       Order the following functions in terms of Big-O notation: (5 points)

       log(n),  n1/2, 2n, nlog(n), n2 

Solution:

logn, n1/2, nlogn, n2, 2n

b.      Show that if d(n) is O(f(n)) and e(n) is O(g(n)), then d(n)e(n) is O(f(n)g(n)). (10 points)

Solution:

d(n) is O(f(n)) --> exists n1>0 and c1>0 such that when n>=n1, d(n)<c1f(n)

e(n) is O(g(n) --> exists n2>0 and c2>0 such that when n>=n2, e(n) <c2g(n)

Let c=c1 c2 and n0=max{n1, n2}, then when n>n0,

d(n)e(n)< c1f(n)c2g(n)=c(f(n)g(n) -->  d(n)e(n) is O(f(n)g(n))