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.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Computer Science 209 Software Development Equality and Comparisons.
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
Jan Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Queues.
Prepared by Dr. Inayatullah Shah1 Data Structures CSC212.
Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Jan Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
Lab class 9: 1. The tree shown in the following figure represents an expression: (((( ) * 3 ) / (( ) + 2 )) - (( 3 * ( )) + 6 )) Please.
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
Search - CIS 1068 Program Design and Abstraction
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 19/23/2015.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
Pointers OVERVIEW.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
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.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CS342 Data Structures End-of-semester Review S2002.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
7: Polymorphism Upcasting revisited Forgetting the object type
Stacks (and Queues).
Planning & System installation
Lecture 2: Data Types, Variables, Operators, and Expressions
Stacks.
Week 15 – Monday CS221.
הרצאה 7: מחרוזות וחתימה של פונקציה
Chapter 8: Collections: Arrays
Introduction to Java Programming
Stacks.
Stacks Abstract Data Types (ADTs) Stacks
class PrintOnetoTen { public static void main(String args[]) {
Lecture 22: Number Systems
Presentation transcript:

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.

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”

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); }

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);} } }}

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 );} }

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

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

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.

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

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

Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen11 5. Please give a java program to calculate the following expression: “ ”. It is required to use ArrayStack to control the computation.

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 = " "; 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

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