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