Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type (b)Describe."— Presentation transcript:

1 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen1 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)What is the main difference between Vectors and Lists?(3) (d)What is the main difference between vectors and arrays?(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. (c)In a vector, the objects are accessed through the ranks associated with the objects. In a list, the objects are visited by giving their positions. (d)In a vector, the ranks of the objects can be changed. A vector can receive infinitely many objects. In an array, the indexes of the entries cannot be changed. The number of the objects is limited.

2 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen2 (e)Analyze the following program and show the outputs of the programs. 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”

3 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen3 (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); }

4 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen4 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);} } }}

5 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen5 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 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, …, x k be the children of x; for (i = k to 1) do {S.enqueue(x i );} }

6 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen6

7 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen7 Sample trace: 1 step1: 2323 step2: visit(1) 345345 step3: visit(2) 45674567 step4: visit(3) 5678956789 step5: visit(4) empty step16: visit(15) 67891011 step6: visit(5) 78910111213 step7: visit(6) 89101112131415 step8: visit(7) …

8 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen8 4.Assume that interface Stack and class ArrayStack are available. Please give a java program to compute the stock spans with Stack and ArrayStack 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.

9 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen9

10 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen10

11 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen11 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.

12 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen12 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

13 Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen13 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


Download ppt "Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type (b)Describe."

Similar presentations


Ads by Google