Introduction to Computer Science II (ITI 1121) Midterm Examination

Similar documents
Introduction to Computer Science II (ITI 1121) Midterm Examination

Introduction to Computer Science II (CSI 1101)

Introduction to Computer Science II (CSI 1101) Final Examination

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Introduction to Computer Science II (CSI 1101) Midterm Examination

Introduction to Computer Science II (CSI 1101) Midterm Examination

Introduction to Computing II (ITI 1121) Midterm Examination

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Introduction to Computer Science II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Midterm Examination

Introduction to Computer Science II (CSI 1101) Final Examination

Introduction to Computing II (ITI 1121) Final Examination

CSIS 10B Lab 2 Bags and Stacks

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

Stacks Fall 2018 Margaret Reid-Miller

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

Top of the Stack. Stack ADT

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

ITI Introduction to Computing II

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

ITI Introduction to Computing II

ITI Introduction to Computing II

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

1. Stack Implementation Using 1D Array

Stacks, Queues (cont d)

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Chapter 4 Defining Classes I

ITI Introduction to Computing II

ITI Introduction to Computing II

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

CSE 143 SAMPLE MIDTERM

Stack and Its Implementation

CS 206 Introduction to Computer Science II

ITI Introduction to Computing II

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Stacks. Revised based on textbook author s notes.

Advanced Java Concepts Unit 3: Stacks and Queues

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Good Luck! CSC207, Fall 2012: Quiz 3 Duration 25 minutes Aids allowed: none. Student Number: Lecture Section: L0101. Instructor: Horton

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Stacks and Queues. Chapter Stacks

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

CSC 273 Data Structures

CMPS 390 Data Structures

Review: Expressions, Variables, Loops, and more.

Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS 367: Introduction to Data Structures Midterm Sample Questions

CSC Java Programming, Fall Java Data Types and Control Constructs

Project 1: Implementation of the Stack ADT and Its Application

Csci 102: Sample Exam

Assignment 5. Introduction

Top of the Stack. Stack ADT

Question Points Score

Midterm Exam 2 CS 455, Spring 2011

CS 211 Programming Practicum Spring 2017

Largest Online Community of VU Students

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Problem with Scanning an Infix Expression

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

CS350: Data Structures Stacks

Associate Professor Dr. Raed Ibraheem Hamed

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Introduction to Linked Data Structures

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS 211 Programming Practicum Fall 2018

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

DNHI Homework 3 Solutions List, Stacs and Queues

ECSE 321 Assignment 2

CS 211 Programming Practicum Spring 2018

Data Structure (CS301)

Lab 7 1 Due Thu., 6 Apr. 2017

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

CS111: PROGRAMMING LANGUAGE II

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

PA3 Design Specification

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CSC 222: Computer Programming II. Spring 2005

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Introduction to Programming Using Java (98-388)

Final Exam. COMP Summer I June 26, points

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Compiling expressions

ITI Introduction to Computing II

Transcription:

Introduction to Computer Science II (ITI 1121) Midterm Examination Instructor: Marcel Turcotte February 2008, duration: 2 hours Identification Student name: Student number: Signature: Instructions 1. 2. 3. 4. 5. 6. This is a closed book examination; No calculators or other aids are permitted; Write comments and assumptions to get partial marks; Beware, poor hand writing can affect grades; Do not remove the staple holding the examination pages together; Write your answers in the space provided. Use the backs of pages if necessary. You may not hand in additional pages; Marking scheme Question 1 2 3 4 5 Total Maximum 35 10 10 10 10 75 Result

February 2008 ITI 1121 Page 2 of 18 Question 1: (35 marks) For this question, there is an interface, named Taxable, as well as three classes, Building, ResidentialBuilding and SingleResidence. Building «interface» Taxable ResidentialBuilding SingleResidence Knowing that: 1. all buildings have an address (a character string); 2. all buildings have a valuation (a double); 3. the residential buildings have a number of residential units (an integer); 4. the single residences have a swimming pool or not (a boolean); 5. all the buildings are Taxable, where Taxable is an interface that declares a method named gettaxamount; 6. the tax amount for a residential building is the sum of two values. First, there is a general tax, which is a fixed rate (0.75%) times the valuation of the property. Second, there is a garbage collection tax, which is a fixed amount ($149.77) times the number of residential units; 7. the tax amount for a single residence is the sum of the following values: first, there is a general tax, which is a fixed rate (0.8%) times the valuation of the property. Second, there is a garbage collection tax, which is a fixed amount ($159.77). Thirdly, there is a tax of $89.01 for a swimming pool. In Java, implement the above three classes, as well as the interface. Include the necessary constructors. Declare constants whenever necessary. For all the attributes, create the necessary access methods. However, the objects must be immutable (a concept seen in lecture 10).

February 2008 ITI 1121 Page 3 of 18 A. In Java, implement the above three classes, as well as the interface. (27 marks)

February 2008 ITI 1121 Page 4 of 18

February 2008 ITI 1121 Page 5 of 18 B. Create a static method, named tallytaxes. Its first parameter is a reference to an array of Taxable objects. The second parameter is the number of occupied cells in the array. You can assume that i) the parameter number of occupied cells is less than or equals to the number of cells in this array, ii) the elements are stored in the leftmost contiguous cells. The method returns the sum of the tax amount of all the objects stored in the array. (5 marks) C. Declare and create an array, named city, to hold 23,000 buildings. (3 marks)

February 2008 ITI 1121 Page 6 of 18 Question 2: Using a stack (10 marks) The class Calculator (next page) implements a postfix evaluator similar to the one seen in class. Show the information that will be printed onto the screen when the following two statements are executed: Calculator c = new Calculator(); c.execute( "6 4 5 print * print 2 3 print ^ print ~ print + print" ); Notes: For this question, there is an implementation of the interface Stack named LinkedStack; The implementation of the classes Token and Reader can be found in Appendix B and C.

February 2008 ITI 1121 Page 7 of 18 Using a stack (continued) public class Calculator { private Stack<Token> operands = new LinkedStack<Token>(); public void execute( String program ) { Reader r = new Reader( program ); while ( r.hasmoretokens() ) { Token t = r.nexttoken(); if (! t.issymbol() ) { operands.push( t ); else if ( t.svalue().equals( "+" ) ) { Token b = operands.pop(); Token a = operands.pop(); Token res = new Token( a.ivalue() + b.ivalue() ); operands.push( res ); else if ( t.svalue().equals( "-" ) ) { Token b = operands.pop(); Token a = operands.pop(); Token res = new Token( a.ivalue() - b.ivalue() ); operands.push( res ); else if ( t.svalue().equals( "*" ) ) { Token b = operands.pop(); Token a = operands.pop(); Token res = new Token( a.ivalue() * b.ivalue() ); operands.push( res ); else if ( t.svalue().equals( "~" ) ) { Token o = operands.pop(); Token res = new Token( - o.ivalue() ); operands.push( res ); else if ( t.svalue().equals( "^" ) ) { Token b = operands.pop(); Token a = operands.pop(); Token res = new Token( (int) Math.pow( a.ivalue(), b.ivalue() ) ); operands.push( res ); else if ( t.svalue().equals( "print" ) ) { System.out.println( "-top-" ); Stack<Token> tmpstack = new LinkedStack<Token>(); while (! operands.isempty() ) { t = operands.pop(); System.out.println( t ); tmpstack.push( t ); while (! tmpstack.isempty()) { operands.push( tmpstack.pop() ); System.out.println( "-bottom-" ); System.out.println();

February 2008 ITI 1121 Page 8 of 18 Question 3: (10 marks) The class DynamicArrayStack below uses the technique seen in class to increase its physical size according to the needs of the application. DynamicArrayStack uses an array to store the elements of this stack; The interface Stack and its implementation, DynamicArrayStack, have a formal type parameter (in other words, the implementation uses the concept of generics types, introduced in Java 1.5); The initial capacity of this array is given by the constant INITIAL CAPACITY; The physical size of the array is increased by a FACTOR (an other constant) whenever the method void push( E elem ) is called and the array is full; The instance variable top designates the top element (i.e. the cell where the last element was inserted, or -1 if the stack is empty); The description of the methods can be found in the Appendix A. A. Complete the implementation of the method compact. The method decreases the physical size of the array so that there are no unoccupied cells. However, the physical size of the array must never be less than INITIAL CAPACITY. public class DynamicArrayStack<E> implements Stack<E> { // Constants public static final int INITIAL_CAPACITY = 10; public static final double FACTOR = 1.5; // Instance variables private E[] elems; private int top = -1; // Stores the elements of this stack // Designates the top element public DynamicArrayStack() { elems = (E[]) new Object[ INITIAL_CAPACITY ]; public boolean isempty() { return top == -1; public E peek() { return elems[ top ]; // Continues on the next page...

February 2008 ITI 1121 Page 9 of 18 public void push( E element ) { if ( ( top + 1 ) == elems.length ) { increasesize(); top++; elems[ top ] = element; private void increasesize() { // The implementation of the method increasesize would be here. // It has been removed to avoid giving away too much information. public E pop() { E saved; saved = elems[ top ]; elems[ top ] = null; top--; return saved; public void compact() { // Complete the implementation of this method // End of compact // End of DynamicArrayStack

February 2008 ITI 1121 Page 10 of 18 Question 4: (10 marks) Complete the implementation of the instance methods size() and swap() within the class Linked- Stack below. A. The method size() returns the number of elements that are currently stored into this stack; B. The method swap exchanges the first two elements (not the values); the first element becomes the second and the second element becomes the first. The method returns false if there are less than 2 elements in the list. You cannot use the methods push and pop, instead the links of the structure (references) must be transformed. public class LinkedStack<T> implements Stack<T> { private static class Elem<E> { // Implements the nodes of the list private E info; private Elem<E> next; private Elem( E info, Elem<E> next) { this.info = info; this.next = next; private Elem<T> top; // Instance variable, designates the top element public int size() { // Continues on the next page...

February 2008 ITI 1121 Page 11 of 18 public boolean swap() { // End of swap // The other instance methods, such as push and pop would go here; // but they cannot be used to answer this question. // End of LinkedStack

February 2008 ITI 1121 Page 12 of 18 Question 5: (10 marks) A. Convert the following infix expression to postfix notation (Reverse Polish Notation RPN). ( ( 5 + 3 ) / 2 ) ( 4 1 ) B. This question is about the interface Valuable, the class Number and the class Integer, as defined on page 14. Which of the following 4 outcomes characterizes best each of the 8 test cases found on the next page. (1) Produces a compile time error? (2) Produces a run time error? (3) Prints if ; (4) Prints else ; For each test case, circle one answer. (a) 1 2 3 4 (b) 1 2 3 4 (c) 1 2 3 4 (d) 1 2 3 4 (e) 1 2 3 4 (f) 1 2 3 4 (g) 1 2 3 4 (h) 1 2 3 4

February 2008 ITI 1121 Page 13 of 18 (a) Number n; n = new Integer( 3 ); (e) Object o = new String( "3" ); Integer i; if ( n.gettype().equals( "Number" ) ) { System.out.println( "if" ); else { System.out.println( "else" ); (b) Valuable v; i = (Integer) o; if ( i.getvalue() == 3 ) { System.out.println( "if" ); else { System.out.println( "else" ); v = new Valuable( 3 ); if ( v.getvalue() == 3 ) { System.out.println( "if" ); else { System.out.println( "else" ); (c) Number n; Integer i; n = new Integer( 3 ); i = n; if ( n.getvalue() == 3 ) { System.out.println( "if" ); else { System.out.println( "else" ); (d) Integer i, j, k; i = new Integer( 1 ); j = new Integer( 2 ); k = i.plus( j ); if ( k.getvalue() == 3 ) { System.out.println( "if" ); else { System.out.println( "else" ); (f) Number n; n = new Integer( 3 ); if ( n.tostring().equals( "Integer: 3" ) ) { System.out.println( "if" ); else { System.out.println( "else" ); (g) Integer i, j; i = new Integer( 3 ); j = new Integer( 3 ); if ( i == j ) { System.out.println( "if" ); else { System.out.println( "else" ); (h) Integer i, j; i = new Integer( 3 ); j = new Integer( 3 ); if ( i.equals( j ) ) { System.out.println( "if" ); else { System.out.println( "else" );

February 2008 ITI 1121 Page 14 of 18 Declarations for Question 5: on page 12. public interface Valuable { public int getvalue(); public abstract class Number implements Valuable { public String tostring() { return "Number: " + getvalue(); public String gettype() { return "Number"; public class Integer extends Number { private int value; public Integer( int value ) { this.value = value; public int getvalue () { return value; public Integer plus( Integer other ) { int value = 0; return new Integer( value + other.value ); public String tostring() { return "Integer: " + getvalue(); public String gettype() { return "Integer";

February 2008 ITI 1121 Page 15 of 18 A Stack public interface Stack<T> { /** * Tests if this Stack is empty. * * @return true if this Stack is empty; and false otherwise. */ public abstract boolean isempty(); /** * Removes and returns the top element. * * @return The top element. */ public abstract T pop(); /** * Puts an element onto the top of this stack. * * @param element the element be put onto the top of this stack. */ public abstract void push( T element );

February 2008 ITI 1121 Page 16 of 18 B Token public class Token { private static final int INTEGER = 1; private static final int SYMBOL = 2; private int ivalue; private String svalue; private int type; public Token( int ivalue ) { this.ivalue = ivalue; type = INTEGER; public Token( String svalue ) { this.svalue = svalue; type = SYMBOL; public int ivalue() { // pre-condition: this Token represents an integer value return ivalue; public String svalue() { // pre-condition: this Token represents a symbol return svalue; public boolean isinteger() { return type == INTEGER; public boolean issymbol() { return type == SYMBOL; public String tostring() { switch ( type ) { case INTEGER: return "INTEGER: " + ivalue; case SYMBOL: return "SYMBOL: " + svalue; default: return "INVALID";

February 2008 ITI 1121 Page 17 of 18 C Reader import java.util.stringtokenizer; public class Reader { private StringTokenizer st; public Reader( String s ) { st = new StringTokenizer( s ); public boolean hasmoretokens() { return st.hasmoretokens(); public Token nexttoken() { String t = st.nexttoken(); try { return new Token( Integer.parseInt( t ) ); catch ( NumberFormatException e ) { return new Token( t );

February 2008 ITI 1121 Page 18 of 18 (blank space)