Analysis of MidtermExamination 1 a How to define

  • Slides: 20
Download presentation
Analysis of Midterm-Examination 1. (a) How to define an abstract data type? (2) How

Analysis of Midterm-Examination 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. Sept. 2015 ACS-2947 Yangjun Chen 1

Analysis of Midterm-Examination (c) header p trailer … … Baltimore Rome Seattle Toronto DLNode

Analysis of Midterm-Examination (c) header p trailer … … Baltimore Rome Seattle Toronto DLNode x = new DLNode(); x. set. Element(new String(“Toronto”)); Sept. 2015 ACS-2947 Yangjun Chen 2

Analysis of Midterm-Examination Update the links: header trailer p … … Baltimore Rome Seattle

Analysis of Midterm-Examination Update the links: header trailer p … … Baltimore Rome Seattle Toronto x. set. Prev(p); x. set. Next(p. get. Next()); (p. get. Next()). set. Prev(x); p. set. Next(x); Sept. 2015 x. prev header; x. next header. next; header. next. prev x; header. next x; ACS-2947 Yangjun Chen 3

Analysis of Midterm-Examination (d) header trailer Toronto Baltimore Rome Seattle ((trailer. get. Prev()). set.

Analysis of Midterm-Examination (d) header trailer Toronto Baltimore Rome Seattle ((trailer. get. Prev()). set. Next(trailer); trailer. set. Prev((trailer. get. Prev()); trailer. prev. next trailer; trailer. prev; Sept. 2015 ACS-2947 Yangjun Chen 4

Analysis of Midterm-Examination (e)Analyze the following program and show the outputs of the program.

Analysis of Midterm-Examination (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” } Sept. 2015 ACS-2947 Yangjun Chen 5

Analysis of Midterm-Examination (2)Write a Java program to compute the following function (20) h(N)

Analysis of Midterm-Examination (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); } Sept. 2015 ACS-2947 Yangjun Chen 6

Analysis of Midterm-Examination public int computing-f(int n) { if (n==0 || n==1) return 1;

Analysis of Midterm-Examination 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); } } } } Sept. 2015 ACS-2947 Yangjun Chen 7

Analysis of Midterm-Examination 3. Please give an algorithm (not a java program) to search

Analysis of Midterm-Examination 3. Please give an algorithm (not a java program) to search a binary tree in the breadth-first manner using a Queue data structure to control 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 x 1, x 2, …, xk be the children of x; for (i = 1 to k) do {S. enqueue(xi); } } Sept. 2015 ACS-2947 Yangjun Chen 8

Analysis of Midterm-Examination Sept. 2015 ACS-2947 Yangjun Chen 9

Analysis of Midterm-Examination Sept. 2015 ACS-2947 Yangjun Chen 9

Analysis of Midterm-Examination Sample trace: step 1: 1 step 5: step 2: step 4:

Analysis of Midterm-Examination Sample trace: step 1: 1 step 5: step 2: step 4: step 3: 2 3 3 4 5 6 7 visit(1) visit(2) visit(3) step 6: step 7: 5 6 7 8 9 10 11 12 13 visit(4) visit(5) visit(6) step 8: 8 9 10 11 12 13 14 15 visit(7) Sept. 2015 … … step 16: empty visit(15) ACS-2947 Yangjun Chen 10

Analysis of Midterm-Examination 4. Assume that interface Stack and class Array. Stack are available.

Analysis of Midterm-Examination 4. Assume that interface Stack and class Array. Stack are available. Please give a java program to compute the stock spans with Stack and Array. Stack being used. The span of the stock’s price on a given day is defined to be the maximum number of the consecutive days up to the current day such that the stock price on each of those days has been less than or equal to the price on the current day. (25) Sept. 2015 ACS-2947 Yangjun Chen 11

Analysis of Midterm-Examination si = i – h(i) Sept. 2015 ACS-2947 Yangjun Chen 12

Analysis of Midterm-Examination si = i – h(i) Sept. 2015 ACS-2947 Yangjun Chen 12

Analysis of Midterm-Examination Sept. 2015 ACS-2947 Yangjun Chen 13

Analysis of Midterm-Examination Sept. 2015 ACS-2947 Yangjun Chen 13

Analysis of Midterm-Examination 5. Please give a java program to calculate the following expression:

Analysis of Midterm-Examination 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 Array. Stack to control the computation. (20) Sept. 2015 ACS-2947 Yangjun Chen 14

Analysis of Midterm-Examination public class Expression-computation{ //start class public static void main( String args[]

Analysis of Midterm-Examination 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 Array. Stack(); int temp; char operator; int a = 0; data. push (new Integer(1)); for (int x = 1; x < s. length(); x++) { if (s. char. At(x) == ‘+’ || s. char. At(x) == ‘-’) data. push(new Character(s. char. At(x))); else { //else it is a number Sept. 2015 ACS-2947 Yangjun Chen 15

Analysis of Midterm-Examination operator = (Character) data. pop(); a = ((Integer)data. pop()). int. Value();

Analysis of Midterm-Examination operator = (Character) data. pop(); a = ((Integer)data. pop()). int. Value(); if (operator == ‘+’) temp = a + char. At(x) – ‘ 0’; else temp = a – (char. At(x) – ‘ 0’); data. push(new Integer(temp)); } System. out. println("The answer is: " + ((Integer) data. pop()). int. Value()); } // end method main }// end class Sept. 2015 ACS-2947 Yangjun Chen 16

Analysis of Midterm-Examination 6. Define a class to implement Queue data structure, using a

Analysis of Midterm-Examination 6. Define a class to implement Queue data structure, using a singly linked list as the physical element container. (Assume that the interface Queue and the class Node are available. You only need to give class List. Queue. ) (8) public interface Queue { public void enqueue( Object element ); throws Queue. Full. Exception; public Object dequeue() throws Queue. Empty. Exception; public int size(); public boolean is. Empty(); public Object front() throws Queue. Empty. Exception; } Sept. 2015 ACS-2947 Yangjun Chen 17

Analysis of Midterm-Examination public class Node { private Object element; private Node next; public

Analysis of Midterm-Examination 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 get. Element() return element; } Node get. Next() { return next; } void set. Elemnt(Object new. Elem) { element = new. Elem; } void set. Next(Node new. Next) { Next = new. Next; }}} Sept. 2015 ACS-2947 Yangjun Chen 18

Analysis of Midterm-Examination public class List. Queue implements Queue { protected Node front, rear;

Analysis of Midterm-Examination public class List. Queue implements Queue { protected Node front, rear; //reference to the front and rear node protected int size; // number of elements in the queue public List. Stack() { // constructs an empty queue front = null; rear = null; size = 0; } public int size() { return size; } public boolean is. Empty() { 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. set. Next(v); rear = v; size++; } Sept. 2015 ACS-2947 Yangjun Chen 19

Analysis of Midterm-Examination public Object front() throws Queue. Empty. Exception { if (is. Empty())

Analysis of Midterm-Examination public Object front() throws Queue. Empty. Exception { if (is. Empty()) throw new Queue. Empty. Exception("Stack is empty. "); return front. get. Element(); } public Object dequeue() throws Queue. Empty. Exception { if (is. Empty()) throw new Queue. Empty. Exception(“Queue is empty. "); Object temp = front. get. Element(); front = front. get. Next(); // 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 Queue. Empty. Exception extends Runtime. Exception { public Queue. Empty. Exception(String err) { super(err); }} Sept. 2015 ACS-2947 Yangjun Chen 20