Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data...

20
Analysis of Midterm-Examination Sept. 2015 ACS-2947 Yangjun Chen 1 1. (a) How to define an abstract data type? (2) How to implement an abstract data type? (b) Describe the main character of Stacks. (2) Describe the main character of Queues. (c) How to insert a new node into a doubly linked list after a certain node p? (3) (d) How to remove a node from a doubly linked at the tail? (3) Answer: (a) To define an abstract data type, we define an interface for that data type. To implement an abstract data type, we define a class, in which the methods defined in the interface for that data type are implemented.

Transcript of Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data...

Page 1: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 1

1. (a) How to define an abstract data type? (2)How to implement an abstract data type?

(b) Describe the main character of Stacks. (2)Describe the main character of Queues.

(c) How to insert a new node into a doubly linked list after a certain node p? (3)

(d) How to remove a node from a doubly linked at the tail?(3)

Answer:(a) To define an abstract data type, we define an interface for that data type.

To implement an abstract data type, we define a class, in which the methods defined in the interface for that data type are implemented.

(b) Stack: first-in last-out. Queue: first-in first-out.

Page 2: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 2

(c)

DLNode x = new DLNode();x.setElement(new String(“Toronto”));

header

Rome Seattle

trailer

Baltimore

Toronto

p

… …

Page 3: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 3

x.setPrev(p);x.setNext(p.getNext());(p.getNext()).setPrev(x);p.setNext(x);

x.prev header;x.next header.next;header.next.prev x;header.next x;

Update the links:

header

Rome Seattle

trailer

Baltimore

Toronto

p

… …

Page 4: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 4

(d)

((trailer.getPrev()).getPrev()).setNext(trailer);trailer.setPrev((trailer.getPrev()).getPrev());

trailer.prev.prev.next trailer;trailer.prev trailer.prev.prev;

header

Rome

Seattle

trailer

BaltimoreToronto

Page 5: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 5

(e)Analyze the following program and show the outputs of the program. (10)

class Note { int value; Note(int val) {value = val;} public static final Note

middle_c = new Note(0),c_sharp = new Note(1),b_flat = new Note(2);}

class Instrument { public void play(Note n) {

System.out.println(“Instrument.play()”);}} class Wind extends Instrument {

public void play(Note n) {System.out.println(“Wind.play()” + “ “ + n.value);}

public static void tune(Instrument i) {i.play(Note.middle_c);}

public static void main(String[] args) {Wind flute = new Wind();tune(flute);}}

}

Answer:

“Wind.play() 0”

Page 6: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 6

(2)Write a Java program to compute the following function (20) 

h(N) = f(N) + g(N), 

where f(N) = 3*g(N-3) + 4*g(N-2) - 5*g(N-1) with f(0) = 1, f(1) = 1, and f(2) = 3,and g(N) = f(N-3) *f(N-2) - 5*f(N-1) with g(0) = 1, g(1) = 2, and g(2) = 4.

public class Computing-H {static void main(String args[]) {

int n = 10;int h = computing-f(n) + computing-g(n);System.out.println(“result:” + “ ” + h);

}

Page 7: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 7

public int computing-f(int n) {if (n==0 || n==1) return 1;else {if (n==2) return 3;

else return 3*computing-g(n-3)+4*computing-g(n-2)-5*computing-g(n-1);

}}public int computing-g(int n) {

if (n==0 )return 1;else {if (n==1) return 2;

else {if (n==2) return 4; else return computing-f(n-3)*computing-f(n-2)

-5*computing-f(n-1);} }

}}

Page 8: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 8

3. Please give an algorithm (not a java program) to search a binarytree in the breadth-first manner using a Queue data structure tocontrol the process. (7) Algorithm Breadth-first-search(T, root)establish queue S;S.enqueue(root);while (the S is not empty) do{x := S.dequeue; print(x);

let x1, x2, …, xk be the children of x;for (i = 1 to k) do {S.enqueue(xi);}

}

Page 9: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 9

2 3

1

5

8

4 6 7

109 11 12 13 14 15

Page 10: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 10

Sample trace:

1

step1:

2 3

step2:

visit(1)

3 4 5

step3:

visit(2)

4 5 6 7

step4:

visit(3)

5 6 7 8 9

step5:

visit(4)

empty

step16:

visit(15)

6 7 8 9 10 11

step6:

visit(5)

7 8 9 10 11 12 13

step7:

visit(6)

8 9 10 11 12 13 14 15

step8:

visit(7)

… …

Page 11: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 11

4. Assume that interface Stack and class ArrayStack are available.Please give a java program to compute the stock spans withStack and ArrayStack being used. The span of the stock’s priceon a given day is defined to be the maximum number of theconsecutive days up to the current day such that the stock priceon each of those days has been less than or equal to the price onthe current day. (25)

Page 12: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 12

public void computeDailyHighSpan( Quote Q[]) { int prevHigh; Stack D = new ArrayStack(); for( int i = 0; i < Q.length; i++ ) { while( !D.isEmpty() && Q[ i ].getPrice() >= ((Quote)D.top()).getPrice() ) D.pop(); if( D.isEmpty() ) prevHigh = -1; else prevHigh = (( Quote )D.top()).getDay(); Q[ i ].setSpan( i - prevHigh ); D.push( Q[ i ]); } }

si = i – h(i)

Page 13: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 13

public class Quote { private int day, price, span; public Quote( int d, int p ) { setDay( d ); setPrice( p ); } public void setDay( int d ) { day = d; } public int getDay() { return day; } public void setPrice( int p ) { price = p; } public int getPrice() { return price; } public void setSpan( int s ) { span = s; } public int getSpan() { return span; } }

Page 14: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 14

5. Please give a java program to calculate the following expression:“1+2+3-4-5+6-7+8-9”.It is required to use ArrayStack to control the computation. (20)

Page 15: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 15

public class Expression-computation{ //start class public static void main( String args[] )//start main body { String s = "1+2+3-4-5+6-7+8+9";

Stack data = new ArrayStack(); int temp; char operator;

int a = 0; data.push (new Integer(1));

for (int x = 1; x < s.length(); x++) { if (s.charAt(x) == ‘+’ || s.charAt(x) == ‘-’)

data.push(new Character(s.charAt(x))); else { //else it is a number

Page 16: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 16

operator = (Character) data.pop();a = ((Integer)data.pop()).intValue();if (operator == ‘+’) temp = a + charAt(x) – ‘0’;

else temp = a – (charAt(x) – ‘0’); data.push(new Integer(temp)); }

System.out.println("The answer is: " + ((Integer) data.pop()).intValue());

} // end method main}// end class

Page 17: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 17

6. Define a class to implement Queue data structure, using asingly linked list as the physical element container. (Assumethat the interface Queue and the class Node are available.You only need to give class ListQueue.) (8)

public interface Queue {public void enqueue( Object element );

throws QueueFullException;public Object dequeue()

throws QueueEmptyException;public int size();public boolean isEmpty();public Object front()

throws QueueEmptyException;}

Page 18: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 18

public class Node {private Object element;private Node next;public Node() {

this( null, null )}public Node (object e, Node n) {

element = e;next = n;

}Object getElement()

return element;}Node getNext() {

return next;}void setElemnt(Object newElem) {

element = newElem;}void setNext(Node newNext) {

Next = newNext;}}}

Page 19: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 19

public class ListQueue implements Queue { protected Node front, rear; //reference to the front and rear node protected int size; // number of elements in the queue public ListStack() { // constructs an empty queue front = null; rear = null; size = 0; } public int size() { return size; } public boolean isEmpty() { if (front == null) return true; return false; } public void enqueue(Object elem) { Node v = new Node(elem, null);//create and link-in a new node

if (size == 0) {front = v; rear = v;} else {rear.setNext(v); rear = v; size++; }

Page 20: Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.

Analysis of Midterm-Examination

Sept. 2015 ACS-2947 Yangjun Chen 20

public Object front() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException("Stack is empty."); return front.getElement(); } public Object dequeue() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException(“Queue is empty."); Object temp = front.getElement(); front = front.getNext(); // link-out the former front node size--; return temp; }} /** * Runtime exception thrown when one tries to perform operation

* front or dequeue on an empty queue. */ public class QueueEmptyException extends RuntimeException { public QueueEmptyException(String err) { super(err); }}