21
Prospectus Working title: Systematic programming Authors: Donald F. Stanat 1 and Stephen F. Weiss Department of Computer Science University of North Carolina at Chapel Hill Contact: Stephen Weiss CB# 3175, Sitterson Hall Chapel Hill, NC 27599-3175 919-962-1888 919-962-1799 (FAX) [email protected] I. Background In our CS 2 course, we stress program correctness, formal specification, pre and postconditions, and invariants. We also cover some data structures and programming techniques including linked structures, searching and sorting, recursion, brute force and backtracking, and finite state control. We add rather little to the repertoire of language features introduced in the first course choosing instead to stress "how to do it well" rather than "how to do more." We've been working on the book for a long time. Initially, it was written using the programming language Turing. This is an excellent pedagogical language and contains most modern features (including OOP facilities, executable assertions, etc.). It is also simple and requires very little scaffolding to run a program. Turing comes from Holt and Associates in Canada and is apparently very popular in high schools in Canada. Unfortunately, editors at Holt felt that our book was too advanced for the high school market. And there was very little market for Turing books in the US. At about the same time, we moved our CS2 course from Turing to Java. So we embarked on a project to rewrite the book using Java. The book has been used successfully in our CS 2 1 Don Stanat is now retired.

Prospectus - cs.unc.eduweiss/COMP401/BOOK/Syllabus.doc  · Web viewChapter 9 is a gentle introduction to algorithm analysis and uses the big O and ... 5.2 Real numbers 5.3 Booleans

  • Upload
    trannhu

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Prospectus

Working title: Systematic programmingAuthors: Donald F. Stanat1 and Stephen F. Weiss

Department of Computer ScienceUniversity of North Carolina at Chapel Hill

Contact: Stephen WeissCB# 3175, Sitterson HallChapel Hill, NC 27599-3175919-962-1888919-962-1799 (FAX)[email protected]

I. Background

In our CS 2 course, we stress program correctness, formal specification, pre and postconditions, and invariants. We also cover some data structures and programming techniques including linked structures, searching and sorting, recursion, brute force and backtracking, and finite state control. We add rather little to the repertoire of language features introduced in the first course choosing instead to stress "how to do it well" rather than "how to do more."

We've been working on the book for a long time. Initially, it was written using the programming language Turing. This is an excellent pedagogical language and contains most modern features (including OOP facilities, executable assertions, etc.). It is also simple and requires very little scaffolding to run a program. Turing comes from Holt and Associates in Canada and is apparently very popular in high schools in Canada. Unfortunately, editors at Holt felt that our book was too advanced for the high school market. And there was very little market for Turing books in the US. At about the same time, we moved our CS2 course from Turing to Java. So we embarked on a project to rewrite the book using Java. The book has been used successfully in our CS 2 course for many years. We require that students also have a Java reference of some sort. In most cases, their CS1 text suffices.

Over the years, we have found only one other book whose content comes close to the course we offer: The Design of Data Structures and Algorithms, by van Amstel and Poirters (Prentice Hall, 1989). But the book was not popular with students. It is very difficult (possibly because English is not the authors' first language), full of errors, and uses Modula for examples. One semester, a majority of my students voted that they would have preferred to have no book instead of this one. I could find no reference to a new version of this book, and it may now be out of print. Most other CS2 books that I have examined either don't cover formal specifications at all, or give them only passing coverage.

1 Don Stanat is now retired.

II. Audience

The book is intended for college students in a second level programming course. We assume that students have had some programming experience, but no particular language is assumed. The book has been used successfully by students whose CS1 language was C, C++, Java, Hascal, Basic, Pascal, and Turing. No object oriented programming is assumed, although students whose first language is Java or C++ will have certainly seen basic OO features. We assume students are familiar with the concepts of sequential code, selection (if...then), iteration, subroutines, simple I/O, and simple data structures (mostly arrays). The first chapter of the book is an extensive review of Java that provides all the Java the student will need initially. More advanced Java features are introduced in later chapters. No particular mathematics is assumed beyond basic algebra.

III. Current status

The book currently consists of 14 chapters, 13 of which have been written and are available for review. The one remaining chapter on trees has yet to be written although we know what it will contain. Some of the chapters have exercises. We have a larger collection of exercises and programming assignment from our course archives. There are several major issues yet to be resolved, in particular, the role of executable assertions (which add considerably to the quality and reliability of the program, but also add to its complexity, bulk, and running time). There may be some inconsistencies from one chapter to another due to the amount of time that passed during the writing. A preface and introduction must be written. We anticipate that the book would have a companion web site with the text of all the sample programs, selected answers, and possibly more. There's more work to be done. But the book is fine as-is for our course. And before I put a lot more effort into it, I wanted to see if there was any market.

IV. What makes this book unique

The book stresses good programming with emphasis on programming in the small rather than programming in the large. We introduce formal documentation (assertions, pre and postconditions, and loop invariants) and rules of inference for demonstrating program correctness. The formal documentation is carried through the text, and we show how to write executable assertions in Java. We also discuss guidelines for writing good code. Finally, we stress throughout the text the notion of abstraction: the separation of architecture (how something looks form the outside) from its implementation (how it works). Our objective is that students are able to write code that is clear, concise, generalized, modifiable, and provably correct. The van Amstel & Poirters book is the only other book with similar emphases.

V. Possible reviewers. I can think of no obvious candidate reviewers. Don Stanat suggested

Professor Wilbon DavisSouthwest Texas State University

Department of Computer Science 601 University Drive San Marcos, TX 78666-4616 Phone: (512) 245-3409 Fax: (512) 245-8750 E-Mail: [email protected]

URL: http://www.cs.swt.edu/faculty/pages/DavisWilbon.shtml

VI. Chapter summaries

Chapter 1 is an extensive Java review. It is not intended to teach programming. Rather its purpose is to teach Java to readers whose programming background is in some other language and to review Java for those who have programmed in Java before. The chapter is keyed to later chapters in the text; parts of chapter 1 can be postponed until these chapters are covered.

Chapter 2 introduces that language of logic and formal documentation that we'll use throughout the text. We also present the concept of program state and assertions about program state.

Chapter 3 presents for formal rules of logic for demonstrating the correctness of assignment statements (the axiom of assignment), selection, and loops (loop invariants). By the end of chapter 3, the students should be able to write pre and postconditions for methods and to prove simple programs (containing assignment, selection, and iteration) correct.

Chapters 4 and 5 present guidelines for writing good code using the concepts presented so far: straight line code, selection, iteration, and methods (subroutines). We stress the notion of procedural abstraction in Chapter 5. Chapter 6 introduces recursion and mathematical induction by which recursive programs can be shown correct.

Chapter 7 introduces the concept of the abstract data type (ADT) and reintroduces the Java class (which introductory Java programmers are forced to deal with, at least a little) as the way to implement ADTs. Procedural abstraction (Chapter 5) and data abstraction in this chapter complete the notion of abstraction in programming.

Chapter 8 introduces the second major attribute (the first being support for abstraction) of object oriented languages: inheritance – building new classes from old. Starting with very simple inheritance, we progress through expansion, redefining preserving semantics, and then to redefining without preserving semantics. We also present abstract classes and show how they

allow us to implement polymorphism. Finally, we show a second way to build new classes from old: using one class in the definition of another.

Chapter 9 is a gentle introduction to algorithm analysis and uses the big O and notation. Our primary interest is in the asymptotic time complexity of simple algorithms. We use loop counting for iterative algorithms and recurrence equations for recursive algorithms.

Chapter 10 presents sorting algorithms and uses the tools from chapter 9 to analyze the algorithms. We cover both n2 sort and n log n sorts and show where each sort is the method of choice.

Chapter 11 and 12 deal with linked structures with linear structures in Chapter 11 and nonlinear structures – mostly trees – in 12. With linked lists, we show how they are implemented and present algorithms for scanning, searching, insertion, and deletion. We also cover enhanced linked lists including 2-way links, circular lists, and thumb indexes. Chapter 12 has not yet been written. But it will briefly introduce the general notion of nonlinear structures (graphs), then focus on trees, and more specifically on binary trees.

Chapters 13 and 14 cover two powerful, but often neglected programming techniques; brute force and finite state machines. Chapter 13 motivates brute force as a problem solution technique and presents the backtracking algorithm as an implementation of brute force. Several important techniques (reducing the size of the superset, pruning, and filter improvement) are presented as ways to speed up the basic brute force process.

Chapter 14 presents the finite state machine, some applications, and its limitations. While a pure FSM is not very powerful, the basic FSM concept can be the basis of a very effective programming technique.

VII. Table of contents

Chapter 1: Java Review (62 pages)

1 Introduction2 A first program3 Program annotations: Comments4 Java Identifiers

4.1 Identifier Names5 Java primitive data types

5.1 Integers5.2 Real numbers5.3 Booleans5.4 Character5.5 Variables5.6 Constants5.7 Expressions

6 Java statements6.1 Assignment statement6.2 The String class6.3 Simple input and output

6.3.1 Output6.3.2 Input6.3.3 Program 0 revealed

6.4 Arrays6.5 Java Control Structures

6.5.1 Selection: conditional execution6.5.2 Iteration: Repeated execution6.5.3 Counting loops6.5.4 The do loop6.5.5 The continue statement6.5.6 Scope of variable names

7 Methods7.1 Method syntax7.2 Overloading methods

8 Object oriented programming8.1 Constructors8.2 Special methods

8.2.1 toString method8.2.2 Equals method

8.3 An alternate implementation of Rectangle8.4 Private methods8.5 Static methods and class variables8.6 What's really going on here?8.7 Another example: the Rational class8.8 Another look at arrays

8.8.1 Array length8.8.2 The args array

8.9 Objects as parameters8.10 Another look at Strings

8.10.1 String tools8.11 Vectors8.12 Wrapper classes8.13 Interfaces (required for Chapter 8)

9 Exceptions, and robust programs (required for Chapter 3)9.1 Robust programs9.2 Java exceptions9.3 More on catching exceptions.9.4 The finally clause9.5 Throwing an exception9.6 Creating new exceptions

10 Reading and writing a text file11 A final word

Chapter 2: Program Documentation (37 pages)

1 Forms of Programmer Documentation1.1 Why Bother?1.2 A Caveat

2 Program State2.1 Making Assertions about the Program State2.2 Program Specification

3 A Simple Language of Assertions: The Propositional Calculus3.1 Identities3.2 Weak versus Strong Assertions3.3 The Boolean Data Type of a Programming Language3.4 The boolean operators in Java3.5 Implication in Java

4 Simple Propositions as Preconditions and Postconditions4.1 Creating an Assert Statement4.2 Denoting Original Values of Variables

5 A Richer Language of Assertions: Quantifiers5.1 The Universal and Existential Quantifiers5.2 Additional Quantifiers

5.2.1 The 'count' or 'number' quantifier Num5.2.2 The 'sum’ quantifier Sum5.2.3 The 'product’ quantifier Prod5.2.4 The 'maximum’ quantifier Max5.2.5 The 'minimum’ quantifier Min

6 In Conclusion7 Summary

7.1 Assertions as program documentation7.2 The language of assertions7.3 The propositional calculus7.4 Weaker and stronger assertions7.5 The Boolean data type and short-circuit evaluation7.6 The predicate calculus7.7 Binding of the variables of a predicate7.8 The empty domain7.9 Equivalences of quantified assertions7.10 Changing the order in which variables are quantified.7.11 Additional Quantifiers

8 Exercises:

Chapter 3: The Rules of Programming (38 pages)

1 Formal Systems2 The Specification Rules of Inference

2.1 Rules for Changing Specifications2.1.1 The Rule of the Weakened Postcondition2.1.2 The Rule of the Strengthened Precondition

2.2 The Rules of Control Structures2.2.1 The Rule of Sequential Execution, or The Rule of

Program Composition2.2.2 Selection Statements (Conditional Execution)

2.2.2.1 The if(b){C} structure2.2.2.2 The if(b){C} else {D} Structure

2.2.3 Iteration Statements2.2.3.1 The while loop Structure2.2.3.2 The for loop Structure

3 The Assignment Axiom3.1 Weakest Preconditions and Strongest Postconditions

4 Program Termination5 Another implementation for assertions

5.1 Assertion exception class and assert class5.2 Adding pre and postconditions to methods5.3 More examples

6 In Conclusion7 Chapter Summary

7.1 The Axiom of Assignment7.2 The Rules of Inference7.3 Program Termination

Chapter 4: The Rules of Coding I (36 pages)

Basic Control Structures1 Introduction2 Basic Program Style3 Program Documentation

3.1 Comments3.2 Program Format3.3 Other forms of documentation

4 Declarations4.1 Restricting the range of variables

5 Single Entrance-Single Exit Code6 Sequential Control7 Conditional statements

7.1 Basic rules for choosing a conditional execution structure:7.2 The cascaded (or chained) if statement7.3 Nested Conditionals7.4 Changing the form of conditional statements7.5 Avoiding use of the null statement (a special case):

8 Loops8.1 While loops8.2 Writing loops8.3 Summary:8.4 For Loops (Counter-controlled)

9 Writing Program Assertions10 A Final Caveat11 Summary12 Exercises:

Chapter 5: Rules of Coding II: methods (23 pages)

1 Why use methods?1.1 Methods make programs easier to write1.2 Methods make programs easier to read.2 The role of preconditions and postconditions2.1 Specifying pre and postconditions2.2 Implementing pre and postconditions 2.3 A caveat concerning actual parameters3 Functions3.1 Names of Functions3.2 Using Functions3.3 Examples of functions

Finding the largest of three integer valuesFinding the index of the minimum value of a subarraySequential search of an array

End of examples of functions4 Procedures

4.1 Names of Procedures4.2 Using Procedures4.3 Examples of procedures

4.3.1 Get a valid value4.3.2 Selection sort4.3.3 The Dutch National Flag

5 Tracing methods6 Summary7 Exercises:

Chapter 6: Rules of Coding III: Recursion (34 pages)

1 How recursive methods work2 Why recursive methods work3 Tracing recursive methods4 Examples of recursive functions

4.1 Finding the largest entry of an integer subarray4.2 Recognizing an Unsigned integer4.3 Greatest common divisor

5 Examples of recursive procedures5.1 Processing the entries of a subarray5.2 Towers of Hanoi5.3 The Chinese rings puzzle5.4 Selection sort

6 Divide and Conquer6.1 Finding the largest entry of an integer subarray6.2 Binary search6.3 Mergesort6.4 Quicksort

7 Avoiding inefficient recursion8 When should you use recursion?9 Exercises

Chapter 7: Abstract Data Types and Classes (15 pages)

1 Introduction2 What is an Abstract Data Type?

2.1 The ADTs Integer and Java int3 How is an ADT specified?4 The concept of class and objects5 The ADT Pair

5.1 An implementation of the ADT Pair as a class5.2 Using the class implementation of the ADT Pair

6 A second example: rationals6.1 The class for rationals6.2 A helper class for the rationals

7 Keeping track of objects8 Summary

Chapter 8 Building new classes from old (23 pages)

1 Inheriting a class1.1 Inheriting with expansion1.2 Constructors1.3 Inheritance with overriding

1.3.1 Preserving semantics1.3.2 Not preserving semantics

2 Abstract classes2.1 Standardization2.2 ADT development2.3 Polymorphism2.4 Comparison of interfaces to abstract classes

2.4.1 Contents2.4.2 Implementing2.4.3 Polymorphism2.4.4 Which should you use?

2.5 A final word on inheritance3 Using a class to define another class4 To use or to inherit?5 A driving problem: Listing prime numbers.

5.1 A pseudocode program for listing prime numbers6 Summary

Chapter 9: Asymptotic Complexity: Measuring the Cost of Program Execution (22 pages)

1 Measuring Execution Time: the time complexity of an algorithm2 Comparing Complexity Functions3 How Do We Use It?4 The Principal Complexity Classes

4.1 Θ(1) functions. (Constant complexity)4.2 Θ (log n) functions. (Logarithmic complexity)4.3 Θ (n) functions. (Linear complexity)4.4 Θ (n log n) functions. (n log n complexity)4.5 Θ (n2) functions. (Quadratic complexity)4.6 Θ (n3) functions. (Cubic complexity)4.7 Θ (2n) functions. (Exponential complexity)4.8 Θ (n!) functions. (Factorial complexity)

5 Finding complexity of non-recursive code:6 Finding the Complexity of Recursive Subroutines

6.1 Solutions to recurrence equations7 Average Case Complexity8 Space complexity9 Using complexity results10 Sidebar on Logarithms11 Exercises:

Chapter 10: Sorting (20 pages)

1 Introduction and Overview2 The Easy Comparisons Sorts.

2.1 Selection Sort.2.1.1 The space and time complexity of Selection Sort

2.2 Insertion Sort2.2.1 The space and time complexity of Insertion Sort

3 The Efficient Comparison Sorts3.1 Mergesort3.2 Quicksort

3.2.1 Partitioning an array3.2.2 Choosing a pivot value3.1.3 Refinements of quicksort.3.1.4 Comparison of the sorting complexities

4 The Asymptotic Optimality of Mergesort and Quicksort5 Epilogue

Chapter 11: Linked lists (23 pages)

1 Analogy2 Linked Lists3 Description of a linked list4 Working with linked lists5 Java implementation of a linked list

5.1 Common pitfalls of linked structures6 The ADT List7 Using copying to simplify insertions and deletions

7.1 Insertion7.2 Deletion

8 Variations on the basic linked list8.1 Eliminating the special cases: dummy head and tail nodes8.2 Circular linked list8.3 Doubly Linked Lists8.4 Multiple access

8.4.1 Tail pointers8.4.2 Thumb index

8.5 Summary9 Introduction to non-linear linked structures: trees and graphs

Chapter 12: Trees (not yet written)

Chapter 13: Brute Force and Backtracking (25 pages)

1 Analogy2 Solving problems by brute force

2.1 Combination padlock2.2 Factoring2.3 Eight queens

3 An ADT for sequences4 Generating sequences: the backtrack tree5 Analysis of the backtrack algorithm6 The eight queens problem

6.1 Reducing the size of the superset6.2 Pruning nonviable sequences6.3 Implementing the filter6.4 Speeding up the filter: adding state

7 Some other problems that can be solved by brute force7.1 Shelf cutting7.2 Instant Insanity7.3 Permutations7.4 Sorting

8 Finding a single solution9 Finding the best solution

9.1 The traveling salesman problem10 Solutions of unknown length

10.1 The Shortest path problem11 Chapter summary12 Exercises

12.1 Best checker move12.2 Binary sequences12.3 Map and graph coloring12.4 The tree maze problem12.5 The generalized maze problem12.6 Permutations12.7 Word jumbles

Chapter 14: The finite state control structure (27 pages)

1 Analogy2 Introduction3 The Basic Finite State Machine Model

3.1 Example: the stamp machine4 Implementing a FSM5 Final output machines6 Acceptor machines7 String Searching

7.1 Naive String Search (Algorithm A)7.2 Finite State String Search (Algorithm B)

8 What a FSM cannot do9 Extensions to the basic model

9.1 Integer Reader 19.2 Integer Reader 29.3 Comment locator9.4 Text Compression

10 Summary11 Exercises