CS 2 54-283 Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.

Slides:



Advertisements
Similar presentations
Code Documentation. Important!  It’s for you  It’s for others.
Advertisements

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
Zhigang Zhu, CSC212 Data Structures - Section FG Lecture 1: Introduction Instructor: Zhigang Zhu Department of Computer Science City College.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Analysis of Algorithms
Software Testing and Quality Assurance
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Elementary Data Structures and Algorithms
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Edgardo Molina, CSC212 Data Structures - Section AB Lecture 1: Introduction Instructor: Edgardo Molina Department of Computer Science City College.
Abstract Data Types (ADTs) Data Structures The Java Collections API
CS 1031 Introduction (Outline) The Software Development Process Performance Analysis: the Big Oh. Abstract Data Types Introduction to Data Structures.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Xiaoyan Li, CSC211 Data Structures Lecture 1: Introduction Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Grading Exams 60 % Lab 20 % Participation 5% Quizes 15%
Programming Life Cycle Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
The Software Development Process
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Data Structures Using C++ 2E
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
George Wolberg, CSC212 Data Structures Lecture 1: Introduction Instructor: George Wolberg Department of Computer Science City College of New York.
Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Chapter 1 The Phases of Software Development
Welcome to CSIS 10B Overview of Course Software Design (from Nyhoff)
Chapter 1 The Phases of Software Development
The Software Development Cycle
The Selection Structure
Design by Contract Fall 2016 Version.
Preconditions, Postconditions & Assertions
Chapter 3: Program Statements
Introduction to Data Structures
Introduction to Data Structures
Defining Classes and Methods
Data Structures and Algorithms for Information Processing
Preconditions and Postconditions
Preconditions and Postconditions
The Software Development Cycle
Presentation transcript:

CS Fall 2007 Dr. Barbara Boucher Owens

CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building blocks –Improve skill in programming, design and specification –Meld engineering, mathematics and science –Judge efficiency

Building blocks Classes Arrays and Vectors Dynamic Lists Trees Searching Sorting Big-O

Programming, Design, Specification Use Java Progamming in the Small Programming in the Large Specify pre and post conditions Improve interface Reliable, modifiable programs

First Assignment First Program assignment

Software Engineering Life Cycle Specify task Design a solution Implement solution Analysis of Solution Testing and Debugging Maintenance Obsolescence

Decompose the Problem Break into subtasks Then break into smaller and smaller subtasks

Specifying a Java Method 5 part format –Short Introduction –Parameter description –Precondition –Returns condition or post condition –The throws list

An important topic: preconditions and postconditions. They are a method of specifying what a method accomplishes. Preconditions and Postconditions Data Structures and Other Objects Using Java

Preconditions and Postconditions Frequently a programmer must communicate precisely what a method accomplishes, without any indication of how the method does its work. Can you think of a situation where this would occur ?

Example You are the head of a programming team and you want one of your programmers to write a method for part of a project. HERE ARE THE REQUIREMENTS FOR A METHOD THAT I WANT YOU TO WRITE. I DON'T CARE HOW THE METHOD WORKS, AS LONG AS THESE REQUIREMENTS ARE MET.

What are Preconditions and Postconditions? One way to specify such requirements is with a pair of statements about the method. The precondition statement indicates what must be true before the method is called. The postcondition statement indicates what will be true when the method finishes its work.

Example // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. public void writeSqrt( double x)...

Example // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. public void writeSqrt( double x)... } The precondition and postcondition appear as comments in your program. They are usually placed before the method’s implementation.

Example // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. public void writeSqrt( double x)... } In this example, the precondition requires that x >= 0 be true whenever the method is called.

Example writeSqrt( -10 ); writeSqrt( 0 ); writeSqrt( 5.6 ); Which of these method calls meet the precondition ?

Example Which of these method calls meet the precondition ? The second and third calls are fine, since the argument is greater than or equal to zero. writeSqrt( -10 ); writeSqrt( 0 ); writeSqrt( 5.6 );

Example Which of these method calls meet the precondition ? But the first call violates the precondition, since the argument is less than zero. writeSqrt( -10 ); writeSqrt( 0 ); writeSqrt( 5.6 );

Example // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. public void writeSqrt( double x)... } The postcondition always indicates what work the method has accomplished. In this case, when the method returns the square root of x has been written.

Another Example // Precondition: letter is an uppercase or // lowercase letter (in the range 'A'... 'Z' or 'a'... 'z'). // Postcondition: The value returned by the // method is true if letter is a vowel; // otherwise the value returned by the method is // false. public boolean isVowel( char letter )...

Another Example isVowel( 'A' ); isVowel(' Z' ); isVowel( '?' ); What values will be returned by these method calls ?

Another Example isVowel( 'A' ); isVowel(' Z' ); isVowel( '?' ); What values will be returned by these method calls ? true false Nobody knows, because the precondition has been violated.

Another Example isVowel( '?' ); What values will be returned by these method calls ? Violating the precondition might even crash the program.

Always make sure the precondition is valid... The programmer who calls the method is responsible for ensuring that the precondition is valid when the method is called. AT THIS POINT, MY PROGRAM CALLS YOUR METHOD, AND I MAKE SURE THAT THE PRECONDITION IS VALID.

... so the postcondition becomes true at the method’s end. The programmer who writes the method counts on the precondition being valid, and ensures that the postcondition becomes true at the method’s end. THEN MY METHOD WILL EXECUTE, AND WHEN IT IS DONE, THE POSTCONDITION WILL BE TRUE. I GUARANTEE IT.

A Quiz Suppose that you call a method, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 40- day flood or other disaster? ¬You ­The programmer who wrote that torrential method ®Noah

A Quiz Suppose that you call a method, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 40- day flood or other disaster? ¬You The programmer who calls a method is responsible for ensuring that the precondition is valid.

On the other hand, careful programmers also follow these rules: When you write a method, you should make every effort to detect when a precondition has been violated. If you detect that a precondition has been violated, then print an error message and halt the program.

On the other hand, careful programmers also follow these rules: When you write a method, you should make every effort to detect when a precondition has been violated. If you detect that a precondition has been violated, then print an error message and halt the program......rather than causing a disaster.

Example // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. public void writeSqrt( double x) { if (x < 0) throw new IllegalArgumentException(“Negative x”);... Throwing an exception(described in Section 1.1) is useful.

Advantages of Using Preconditions and Postconditions Succinctly describes the behavior of a method without cluttering up your thinking with details of how the method works. At a later point, you may reimplement the method in a new way but programs (which only depend on the precondition/postcondition) will still work with no changes.

Precondition The programmer who calls a method ensures that the precondition is valid. The programmer who writes a method can bank on the precondition being true when the method begins execution. Postcondition The programmer who writes a method ensures that the postcondition is true when the method finishes executing. Summary

Running Time Analysis Does the algorithm work fast enough? How much longer does the algorithm take when the input gets longer? Which of several algorithms is fastest?

Big-O Notation O – the order of the function that specifies the relation between the number of operations that the algorithm requires and... –the ``size'' of the input, which is usually denoted as n.

Examples f(n) = 10 * n^ n + 10 O(f(n)) =n^2 f(n) = * n^ * n^1.9 O(f(n)) = n^2

Times Quadratic time –O(n 2 ) Linear time –O(n) Logarithmic Time –O(log n) –(number of digits in n) Problems

Big-O Parameter Differences Worst Case –counting the maximum # operations Best Case –fewest number of operations that might be required Average Case –average number of operations required later…...

Testing Good test data –know what a correct program should produce –choose cases to show that –test conditions likely to cause errors o-Bob Boundary conditions