2006 pearson addison-wesley. all rights reserved 3.2.1 design by prototype programmers often...

9
© 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype rammers often approach a problem by creating a series of prototypes. Each otype is intended to make progress toward the desired final program. Example: an email program Prototype 1: only allows the user to read email messages. Prototype 2: allows the user to read and write text-only messages. Prototype 3: adds support for attachments to Prototype 3. Prototype 4: adds facilities for an address book and message flagging Prototype 5: has better user interface in response to customer testin . . . A prototype is a program with only partial functionality. (i.e., it is incomplete with respect to the requirements) Definition

Upload: sharlene-heath

Post on 19-Jan-2018

213 views

Category:

Documents


0 download

DESCRIPTION

© 2006 Pearson Addison-Wesley. All rights reserved Debugging The process of identifying and correcting program errors is called debugging. The compiler captures syntax errors. However, programmers need to capture their own logic errors. Two programmer “ tricks ” for locating logic errors comment out code bracket selected regions of code with /*... */ and observe the behavior of the resulting program. intersperse System.out.println s include println instructions to display selected parts of the state and analyze the output of the resulting program.

TRANSCRIPT

Page 1: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.1

Design by PrototypeProgrammers often approach a problem by creating a series of prototypes. Each prototype is intended to make progress toward the desired final program.

Example: an email programPrototype 1: only allows the user to read email messages.Prototype 2: allows the user to read and write text-only messages.Prototype 3: adds support for attachments to Prototype 3.Prototype 4: adds facilities for an address book and message flagging.Prototype 5: has better user interface in response to customer testing.. . .

A prototype is a program with only partial functionality.

(i.e., it is incomplete with respect to the requirements)

Definition

Page 2: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.2

Program Requirements:Create the following window.

How would you design this program using a prototyping approach?

Page 3: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.3

DebuggingThe process of identifying and correcting program errors is called debugging.

The compiler captures syntax errors. However, programmers need to capture their own logic errors.

Two programmer “tricks” for locating logic errorscomment out code

bracket selected regions of code with /* ... */ and observe the behavior of the resulting program.

intersperse System.out.printlnsinclude println instructions to display selected parts of the state and analyze the output of the resulting program.

Page 4: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.4

Commenting out codeConsider the following erroneous attempt at a canoe prototype.

Desired window

Actual window

The faulty code is on the next page...

Page 5: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.5

import java.awt.Color;import javax.swing.JFrame;public class Driver { private JFrame theWindow; private Rectangle hull; private Oval bow, stern, cover;

/** post: a window is constructed and a canoe on a white background displayed. */ public Driver() { theWindow = new JFrame( "Canoe" ); theWindow.setBounds( 30, 30, 280, 240 ); theWindow.setLayout(null); theWindow.setVisible(true); theWindow.getContentPane().setBackground( Color.white );

hull = new Rectangle( 40, 90, 200, 51 ); hull.setBackground( Color.lightGray ); theWindow.add( hull, 0 );

bow = new Oval( 20, 80, 40, 60 ); bow.setBackground( Color.lightGray ); theWindow.add( bow, 0 ); stern = new Oval( 220, 80, 40, 60 ); stern.setBackground( Color.lightGray ); theWindow.add( stern, 0 );

cover = new Oval( 20, 6, 240, 100 ); cover.setBackground( Color.white ); theWindow.add( cover ); theWindow.repaint(); }}

What if we comment out the code involving cover?

Page 6: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.6

import java.awt.Color;import javax.swing.JFrame;public class Driver { private JFrame theWindow; private Rectangle hull; private Oval bow, stern, cover;

/** post: a window is constructed and a canoe on a white background displayed. */ public Driver() { theWindow = new JFrame( "Canoe" ); theWindow.setBounds( 30, 30, 280, 240 ); theWindow.setLayout(null); theWindow.setVisible(true); theWindow.getContentPane().setBackground( Color.white );

hull = new Rectangle( 40, 90, 200, 51 ); hull.setBackground( Color.lightGray ); theWindow.add( hull, 0 );

bow = new Oval( 20, 80, 40, 60 ); bow.setBackground( Color.lightGray ); theWindow.add( bow, 0 ); stern = new Oval( 220, 80, 40, 60 ); stern.setBackground( Color.lightGray ); theWindow.add( stern, 0 ); /* cover = new Oval( 20, 6, 240, 100 ); cover.setBackground( Color.white ); theWindow.add( cover ); */ theWindow.repaint(); }}

Commenting out code is good for narrowing down the error’s source.

Be careful not to attempt nestingof comments.

Page 7: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.7

System.out.printlnJava includes a special method (println) that can be used to output the state of any object.

Desired window

Actual window

System.out.println( objectReference );

The faulty code is on the next page...

Page 8: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.8

import java.awt.Color;import javax.swing.JFrame;public class Driver { private JFrame theWindow; private Rectangle hull; private Oval bow, stern, cover;

/** post: a window is constructed and a canoe on a white background displayed. */ public Driver() { theWindow = new JFrame( "Canoe" ); theWindow.setBounds( 30, 30, 280, 240 ); theWindow.setLayout(null); theWindow.setVisible(true); theWindow.getContentPane().setBackground( Color.white );

hull = new Rectangle( 40, 90, 200, 51 ); hull.setBackground( Color.lightGray ); theWindow.add( hull, 0 );

bow = new Oval( 20, 80, 40, 60 ); bow.setBackground( Color.lightGray ); theWindow.add( bow, 0 ); stern = new Oval( 20, 80, 40, 60 ); stern.setBackground( Color.lightGray ); theWindow.add( stern, 0 );

cover = new Oval( 20, 6, 240, 100 ); cover.setBackground( Color.white ); theWindow.add( cover, 0 ); theWindow.repaint(); }}

Page 9: 2006 Pearson Addison-Wesley. All rights reserved 3.2.1 Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each

© 2006 Pearson Addison-Wesley. All rights reserved 3.2.9

import java.awt.Color;import javax.swing.JFrame;public class Driver { private JFrame theWindow; private Rectangle hull; private Oval bow, stern, cover;

/** post: a window is constructed and a canoe on a white background displayed. */ public Driver() { theWindow = new JFrame( "Canoe" ); theWindow.setBounds( 30, 30, 280, 240 ); theWindow.setLayout(null); theWindow.setVisible(true); theWindow.getContentPane().setBackground( Color.white );

hull = new Rectangle( 40, 90, 200, 51 ); hull.setBackground( Color.lightGray ); theWindow.add( hull, 0 ); System.out.println( hull ); bow = new Oval( 20, 80, 40, 60 ); bow.setBackground( Color.lightGray ); theWindow.add( bow, 0 ); stern = new Oval( 20, 80, 40, 60 ); stern.setBackground( Color.lightGray ); theWindow.add( stern, 0 ); System.out.println( bow ); System.out.println( stern ); cover = new Oval( 20, 6, 240, 100 ); cover.setBackground( Color.white ); theWindow.add( cover, 0 ); theWindow.repaint(); }}