DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

Similar documents
SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

Solutions for Selected Exercises

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Chapter 4 Defining Classes I

CS 206 Introduction to Computer Science II

Practical List of. MCA IV SEM Session -2010

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Assertions. Assertions - Example

Introduction to Programming

Welcome to Starting Out with Programming Logic and Design, Third Edition.

About this exam review

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Problem Solving with C++

Assertions, pre/postconditions

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

B. Knowledge of basic algebra and experience in problem solving is beneficial. Students should also possess competence in written communication.

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

STUDENT LESSON A12 Iterations

CS 231 Data Structures and Algorithms, Fall 2016

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

COMP 202 Java in one week

CSE 331 Spring 2018 Midterm

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

CS111: PROGRAMMING LANGUAGE II

Object Oriented Programming with Java

COMP 202. Java in one week

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency.

Data Structures in C++ Using the Standard Template Library

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

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

CS 61B Data Structures and Programming Methodology. June David Sun

Assertions & Verification & Example Loop Invariants Example Exam Questions

A Walkthrough of the Learning Aids

Repetition Structures

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Computer Science II Lecture 1 Introduction and Background

COMP 202 Java in one week

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

Introduction to the Java Basics: Control Flow Statements

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

Introduction to Computing II (ITI 1121) Final Examination

Name SECTION: 12:45 2:20. True or False (12 Points)

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Methods CSC 121 Fall 2014 Howard Rosenthal

a correct statement? You need to know what the statement is supposed to do.

An introduction to Scheme

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

Outline. Computer Science 331. Desirable Properties of Hash Functions. What is a Hash Function? Hash Functions. Mike Jacobson.

Full file at

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Absolute C++ Walter Savitch

Reviewing all Topics this term

COSC 1010 Introduction to Computer Programming

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

BOSTON UNIVERSITY Metropolitan College MET CS342 Data Structures with Java Dr. V.Shtern (Fall 2011) Course Syllabus

COS 126 General Computer Science Spring Written Exam 1

Curriculum Map Grade(s): Subject: AP Computer Science

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

Chapter 2: Data and Expressions

CLASSIC DATA STRUCTURES IN JAVA

Project 1. due date Sunday July 8, 2018, 12:00 noon

AP Computer Science in Java Course Syllabus

CSE 373: Data Structures and Algorithms

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

CS113: Lecture 4. Topics: Functions. Function Activation Records

Data Structures. Lecture 1: Introduction CSC212. Instructor: George Wolberg Department of Computer Science City College of New York

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

For undergraduate courses in assembly language programming and introductory courses in computer systems and computer architecture.

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Contracts. Dr. C. Constantinides. June 5, Department of Computer Science and Software Engineering Concordia University Montreal, Canada 1/71

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COLLEGE OF THE DESERT

Programming refresher and intro to C programming

Assertions & Verification Example Exam Questions

CSE wi Midterm Exam 2/8/18. Name UW ID #

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

CIS133J. Working with Numbers in Java

Flow of Control: Loops

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Largest Online Community of VU Students

Dept. of CSE, IIT KGP

Heaps in C. CHAN Hou Pong, Ken CSCI2100 Data Structures Tutorial 7

CT 229 Java Syntax Continued

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

Programming Exercise 7: Static Methods

CS 113 MIDTERM EXAM 2 SPRING 2013

Course materials Reges, Stuart, and Stepp, Martin. Building Java Programs: A Back to Basics Approach. 2d ed. (Boston: Addison-Wesley, 2011).

Transcription:

INSTRUCTOR S MANUAL WITH SOLUTIONS FOR DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA WALLS AND MIRRORS Second Edition Frank M. Carrano University of Rhode Island Janet J. Prichard Bryant College

Copyright 2006 by Addison Wesley Longman, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or any other media or embodiments now known or hereafter to become known, without the prior written permission of the publisher. Published in the United States of America. The programs and applications presented have been included for their instructional value. They have been tested with care but are not guaranteed for any particular purpose. The publisher does not offer warranties or representations, nor does it accept any liabilities with respect to the programs or applications.

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 3 INTRODUCTION This instructor s guide, which supplements Data Abstraction and Problem Solving with Java: Walls and Mirrors, is organized as follows: Three Possible Courses Based on Walls and Mirrors. We begin by offering suggestions for how to use Walls and Mirrors in your course. The book s flexibility will allow you to use it in a variety of situations. Solutions to Exercises. The solutions to the end-of-chapter exercises are included in this section. Reminder: All Java code that appears in the book is available online. Please consult the preface of the book for further details.

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 4 THREE POSSIBLE COURSES BASED ON WALLS AND MIRRORS By appropriately choosing which parts of the book to emphasize, you can easily tailor the material to fit your particular curriculum. Although the suggested courses presented here cover material in the order in which it appears in the book, variations are possible. The book s preface provides details about the orders in which you can cover chapters. With this flexibility in mind, here are three possible courses based on this book. A Second Course in Computer Science If your introductory course emphasizes structured programming and introduces recursion, students can review most of the first two chapters on their own. On the other hand, if the introductory course manages to cover only the basics of a programming language, you should devote a fair amount of class time to Chapters 2 and 3. If your students do not know Java, you may need to devote some class time to the coverage of Appendix A, which reviews Java. The course should cover the material through Chapter 11, at least. You can select topics from Chapters 12 through 15, as time permits. You can cover many topics within these chapters in any order. Possible Syllabus for a Second Course in Computer Science Week Topic Chapter 1 Review of programming principles Review of recursion 2 3 2 Data abstraction 4 3 Data abstraction 4 (Cont.) 4 Linked lists 5 5 Linked lists 5 (Cont.) 6 Recursion 6 7 Stacks 7 8 Stacks Queues 7 (Cont.) 8 9 Queues Class relationships 8 (Cont.) 9 10 Class relationships 9 (Cont.) 11 Efficiency and sorting 10 12 Efficiency and sorting Trees 10 (Cont.) 11 13 Trees 11 (Cont.) 14 Tables and priority queues 12 15-16 Selected advanced topics 13-15

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 5 Lower-Division Data Structures Courses At many schools, the primary undergraduate data structures course comes early in the curriculum. You can use this book in such a course by giving high priority to the material in Chapter 4 and Chapters 7 through 15. This means that you should spend as little time as possible on Chapter 2, and you may even find it necessary to omit some of the material on recursion. You should assign projects, such as the ones suggested in Chapter 13, in which students implement 2-3 trees or hashing. Possible Syllabus for a Lower-Division Data Structures Course Week Topic Chapter 1 Review of programming principles Review of recursion 2 3 2 Data abstraction 4 3 Linked lists 5 4 Recursion 6 5 Stacks 7 6 Stacks Queues 7 (Cont.) 8 7 Queues Class relationships 8 (Cont.) 9 8 Efficiency and sorting 10 9 Trees 11 10 Trees Tables 11 (Cont.) 12 11 Priority queues and heaps 12 (Cont.) 12 Balanced search trees 13 13 Hashing 13 (Cont.) 14 Multiple organizations Graphs 13 (Cont.) 14 15 Graphs External sorting 14 (Cont.) 15 16 External searching 15 (Cont.)

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 6 Second- and Third-Quarter Courses This book should work very well for schools on the quarter system. If the first quarter is an introductory programming course, then Chapters 2 through 8 would constitute a reasonable second quarter. This would leave the remainder of book for the third quarter. Possible Syllabus for a Second-Quarter Course Week Topic Chapter 1 Review of programming principles 2 2 Review of recursion 3 3 Data abstraction 4 4 Data abstraction 4 (Cont.) 5 Linked lists 5 6 Linked lists 5 (Cont.) 7 Recursion 6 8 Stacks 7 9 Stacks Queues 7 (Cont.) 8 10 Queues 8 (Cont.) Possible Syllabus for a Third-Quarter Course Week Topic Chapter 1 Class relationships 9 2 Efficiency and sorting 10 3 Trees 11 4 Trees Tables 11 (Cont.) 12 5 Priority queues and heaps 12 (Cont.) 6 Balanced trees 13 7 Hashing 13 (Cont.) Multiple organizations 8 Graphs 14 9 External sorting 15 10 External searching 15 (Cont.)

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 7 SOLUTIONS TO EXERCISES PART I: Problem-Solving Techniques Chapter 1 Review of Java Fundamentals There are no exercises for chapter 1. Chapter 2 Principles of Programming and Software Engineering 1 Use a class Money that stores the number of dollars and cents as private data members. When declaring new objects or changing the data of existing ones, the class methods should ensure that if the number of cents exceeds 99, the number of dollars and cents are changed to represent the monetary amount in its most reduced form. public static main (String args[]) Money itemcost = new Money(dollars, cents); Money amountpaid = new Money(d, c); Money change = getchange(itemcost, amountpaid); change.display(); } public static Money getchange(money price, Money payment) // ---------------------------------------------------------------- // Computes the change remaining from purchasing an item costing // price when paying with payment. // Preconditions: price and payment are objects of class Money and // have been initialized to their desired values. // Postconditions: returns an object of class Money representing the // change to be received. If price < payment, the amount owed is // returned as a negative value. // ---------------------------------------------------------------- 2a Use a class Date that stores the day, month and year as private data members. The class should contains a boolean valued method islegitimate() that determines whether a date is a legitimate date or not (based on facts like June has 30 days and February has 29 in a leap year). public static main (String args[]) Date appointment = new Date(day, month, year); incrementdate(appointment); appointment.display(); } public static void incrementdate(date olddate) // ----------------------------------------------------------------------- // Increments the input date by one day.

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 8 // Precondition: olddate is an object of class Date and is initialized // with a legitimate date. // Postcondition: The object of class Date that represents the day after // the input value olddate is returned. // ----------------------------------------------------------------------- 2b public static void incrementdate(date datetoincrement) // increment day datetoincrement.setday(datetoincrement.getday() + 1); if (!datetoincrement.islegitimate()) // reset day and increment month datetoincrement.setday(1); datetoincrement.setmonth(datetoincrement.getmonth() + 1); if (!datetoincrement.islegitimate()) // reset month and increment year datetoincrement.setmonth(1); datetoincrement.setyear(datetoincrement.getyear() + 1); } // end if } // end if } // end incrementdate 3 We can make several improvements to user interaction and programming style: Prompt the user for the input and indicate the expected form of the input. The user should also be given an option to exit the program. Give more descriptive output. Check input for obvious errors; e.g., a four-digit age entry is surely a typo and the user should be allowed to correct the error. Document the program. Use more descriptive variable names. 4 This is an infinite loop. The loop will never terminate because the condition will always be true. 5 No answer provided 6 There are two hazards with the method computation(). First, the argument x cannot be a negative number because computation() calls the sqrt() method with x as the argument. Second, since zero is in the range of the method cos(x), the result of this computation must be tested prior to using it as a divisor in order to avoid a "Divide by Zero error". The following implementation performs the required fail-safe checks. double computation( double x ) // ------------------------------------------------------------ // Performs a computation. // Preconditions: x >=0 and x!= (2k - 1)*PI/2 where k is a // positive integer. // Postcondition: returns -1 if preconditions are not met // else, returns the value of the computation. // ------------------------------------------------------------ // compute the cosine once and store it

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 9 double denominator = Math.cos(x); double result = 0; // check for negative argument to sqrt() method if ( x < 0 ) System.out.println("Cannot take square root of negative number"); result = -1; } // end if // check for division by zero if ( denominator == 0 ) System.out.println("Cannot divide by zero"); result = -1; } // end if if ( result!= -1 ) result = Math.sqrt(x)/denominator; return result; } // end copmutation 7 The invariants for the for loop of the method factorial() are as follows: a.) 1 i n, b.) result = n! / i!. 8 20 >= i >= 1 9 No answer provided 10 public class ex1_8 public static void main (String[] args) int n = 5; int square = 1; for (int i = 1; i <=n; i++) square = i * i; System.out.println(square); } } } 11 The invariants for the for loop are as follows: a.) 0 i n-1, b.) 0 numpos LIMIT, c.) sum is equal to the sum of the first numpos integers in a. final static int LIMIT = 5;

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 10 public static int computesum(int a[], int n) // ------------------------------------------------------------------------ // Computes the sum of the first LIMIT positive elements in a. // Precondition: a is an array of n elementtypes with at least LIMIT // of a positive and n >= LIMIT. // Postcondition: If a contains at least LIMIT positive elements, then // the method returns the sum of the first LIMIT positive // elements. Else, returns 0. // ------------------------------------------------------------------------ int numpos = LIMIT; int sum = 0; // check for out of bounds errors if(numpos > n n < 0) return sum; // perform the computation on the array for(int i = 0; i < n && numpos > 0; ++i) if(a[i] > 0) sum += a[i]; numpos--; } // end if // end for // verify that LIMIT elements have been added if(numpos > 0) return 0; // pass on results return sum; } // end ComputeSum 12 Invariant: 0 index i and sum = a[0] +... + a[index]. Prior to the while loop, index is set to 0 and sum is initialized to a[index], i.e. a[0], and so the invariant describes the value of sum at this point. Within the while loop, the value of sum so far is a[0] +... + a[index]. If index < n, index is then incremented (i.e.: index = index + 1). The value of the next element of the array with respect to the last accessed element (i.e.: a[ <last value of index> + 1 ]) is added to sum. After the while loop, the last value of index must have been n since the loop terminated. Since index increments before it is used to access the array a, the last value of index (i.e.: n) must also have been used. Since the value of sum prior to this last access was a[0] +... + a[n - 1] and the last quantity added was a[n], the algorithm corroborates the invariant at index = n. 13 The bug is in the while loop: a.) Output is The floor of the square root of 64 is 7. b.) The while loop condition should be while(temp1 <= X) Debugging involves printing the values of temp1, temp2 and result at the top of the loop.

IM with Solutions to Accompany Data Abstraction and Problem Solving with Java: Walls and Mirrors, Second Edition 11 c.) Supply user prompts and check the value of x to ensure that it is greater than 0. 14 final int DZERO = 0; final int AOTRG = 1;.. void severeerrormessage(int error) // ----------------------------------------------------------------- // Displays an error code and terminates program execution. // Preconditions: none. // Postconditions: An error message corresponding to the input // errorcode is output to the standard error stream // and the program is terminated. // ----------------------------------------------------------------- switch(error) case DZERO: System.out.println("Divide by zero error."); break; case AORTG: System.out.println("Array index out of range."); break;.. default: System.out.println("Unknown fatal error."); }; // end switch exit(0); // terminate execution immediately } // end severeerrormessage