chapter 3 designing interactive classes
Download
Skip this Video
Download Presentation
Chapter 3: Designing interactive classes.

Loading in 2 Seconds...

play fullscreen
1 / 105

Chapter 3: Designing interactive classes. - PowerPoint PPT Presentation


  • 117 Views
  • Uploaded on

Chapter 3: Designing interactive classes. Objectives. After studying this chapter you should understand the following: the role of responsibilities in the design of an object; the categorization of an object’s responsibilities as knowing responsibilities or doing responsibilities;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 3: Designing interactive classes.' - beth


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
objectives
Objectives
  • After studying this chapter you should understand the following:
    • the role of responsibilities in the design of an object;
    • the categorization of an object’s responsibilities as knowing responsibilities or doing responsibilities;
    • the difference between local variables and instance variables;
    • the structure of a complete program in Java;
    • the structure of a test system;
    • the purpose of named constants.

NH-Chapter 3

objectives1
Objectives
  • Also, you should be able to:
    • analyze the role a simple object plays in a given problem and list its responsibilities;
    • use Java to specify the features of an object based on its responsibilities;
    • use Java to implement a simple class that has been specified;
    • implement a complete Java program using a simple text-based interface for an object;
    • implement a simple tester for testing a class implementation;
    • define named constants.

NH-Chapter 3

designing with objects
Designing with objects
  • Two questions when we design an OO system :
      • what are the objects?
      • what features should these objects have?
  • Our Initial goal: learn to design and implement simple objects.
  • We will ssume objects are there for the picking.

NH-Chapter 3

designing with objects1
Designing with objects
  • Objects are designed to support system functionality.
  • System specification are distributed as responsibilities to objects identified.

NH-Chapter 3

object responsibilities
Object responsibilities
  • Think in terms of
    • what object must know.
    • what object must do.

NH-Chapter 3

object responsibilities1
Object responsibilities
  • Knowing responsibilities include:
    • knowing properties of the entity object is modeling;
    • knowing about other objects with which it needs to cooperate.

NH-Chapter 3

object responsibilities2
Object responsibilities
  • Doing responsibilities include:
    • computing particular values;
    • performing actions that modify its state;
    • creating and initializing other objects;
    • controlling and coordinating the activities of other objects.

NH-Chapter 3

from responsibilities to class features
From responsibilities to class features
  • “Knowing responsibilities”
    • translate into data the object must maintain or
    • Translate into queries.
  • “Doing responsibilities”
    • translate into commands or
    • Translate into queries.

NH-Chapter 3

designing a class
Designing a class
  • To design a class:
    • determine an object’s responsibilities,
    • classify them as knowing or doing responsibilities.

NH-Chapter 3

example design of nim game
Example: Design of Nim game
  • Game: Players take turns removing sticks from a pile. Each player in turn removes one, two, or three sticks. The player who removes the last stick loses.

NH-Chapter 3

design of nim game
Design of Nim game
  • Two objects for the picking:
    • Player
    • Pile of sticks
  • Pile and Player are part of the model of problem.
  • Model aspects of game independently of how is presented to a user or how a user interacts with it.

NH-Chapter 3

designing pile
Designing Pile
  • Pile a very simple object:
    • keeps track of how many sticks remain.
  • Pile responsibilities:
    • know:
      • number of sticks remaining
    • do:
      • reduce number of sticks (remove sticks)

NH-Chapter 3

designing player
Designing Player
  • Player takes a turn in the game to remove sticks from Pile.
  • Player responsibilities:
    • know:
      • this Player’s name.
      • how many sticks this Player removed on his/her most recent turn.
    • do:
      • take a turn by removing sticks from the Pile

NH-Chapter 3

from knowing responsibilities to queries
From knowing responsibilities to queries
  • Class: Pile
  • queries:sticks the number of sticks remaining in this Pile, a non-negative integer

NH-Chapter 3

from knowing responsibilities to queries1
From knowing responsibilities to queries
  • Class: Player
  • queries:
    • name : this Player’s name, a String
    • sticksTaken : number of sticks this Player removed on his/her most recent turn ( 1, 2, or 3).

NH-Chapter 3

from doing responsibilities to commands
From doing responsibilities to commands
  • Class: Pile
  • commands:remove : reduce number of sticks by specified amount (number)

NH-Chapter 3

from doing responsibilities to commands1
From doing responsibilities to commands
  • Class: Player
  • commands:takeTurn remove 1, 2, or 3 sticks from the specified Pile (pile)

NH-Chapter 3

pile and player constructors
Pile, and Player constructors
  • How are the Player’s name and the initial number of sticks in the Pile determined?
  • Set these values when objects are created:Constructors.
  • Constructors can have parameters:
    • Player’s name parameter in Player’s constructor.
    • Initial number of sticks parameter in Pile constructor.

NH-Chapter 3

pile specifications
Pile specifications
  • nimGame
  • Class Pile

public class Pile

A pile of sticks for playing simple nim.

  • Constructors

public Pile (int sticks)

Create a new Pile, with the specified number of sticks. sticks must be non-negative.

NH-Chapter 3

pile specifications1
Pile specifications
  • Queries

publicint sticks ()

Number of sticks remaining in this Pile.

  • Commands

publicvoid remove (int number)

Reduce the number of sticks by the specified amount. number must be non-negative and not greater than the number of sticks remaining.

NH-Chapter 3

player specifications
Player specifications
  • nimGame
  • Class Player

public class Player

A player in the game simple nim.

  • Constructors

public Player (String name)

Create a new Player with the specified name.

NH-Chapter 3

player specifications1
Player specifications
  • Queries

public String name ()

The name of this Player.

publicint sticksTaken ()

The number of sticks this Player removed on this Player’s most recent turn: 1, 2, or 3.

NH-Chapter 3

player specifications2
Player specifications
  • Commands

publicvoid takeTurn (Pile pile)

Remove 1, 2, or 3 sticks from the specified Pile.

NH-Chapter 3

implementing the class pile
Implementing the class Pile
  • Data maintained by Pile is number remaining sticks.
  • Instance variable sticksLeft is initialized in constructor and value returned by query sticks:

privateint sticksLeft; // sticks left in the Pile

public Pile (int sticks) {

sticksLeft = sticks;

}

publicint sticks () {

return sticksLeft;

}

NH-Chapter 3

implementing the class pile1
Implementing the class Pile
  • Command remove is specified with an int parameter number, indicating sticks to be removed:
  • Executing command reduces instance variable sticksLeft by value client supplies in number.

publicvoid remove (int number) {…

publicvoid remove (int number)

sticksLeft = sticksLeft - number;

}

NH-Chapter 3

implementing the class player
Implementing the class Player
  • Player needs to know name and number of sticks taken on most recent turn.
  • Variables should be initialized in the constructor.

private String name; // this Player’s name

privateint sticksTaken; // sticks taken on this Player’s most recent turn

public Player (String name) {

this.name = name;

this.sticksTaken = 0;

}

NH-Chapter 3

implementing the class player1
Implementing the class Player
  • Queries simply return the values of the instance variables:

public String name () {

return name;

}

publicint sticksTaken () {

return sticksTaken;

}

NH-Chapter 3

invoking a method acting as client
Invoking a method: acting as client
  • General form for invoking a command is
  • General form for invoking a query:

objectReference.queryName(arguments)

objectReference.commandName(arguments);

NH-Chapter 3

implementing player s taketurn
Implementing Player’s takeTurn

/**

* Remove 1, 2, or 3 sticks from the specified Pile.

* The Pile must not be empty.

*/

public void takeTurn (Pile pile) { …

  • takeTurn method must:
      • determine how many sticks to take;

Give Player the move strategy to take 1 stick.

      • remove them from the Pile;

pile.remove(1);

      • store the number removed in sticksTaken instance variable.

sticksTaken = 1;

NH-Chapter 3

implementing player s taketurn1
Implementing Player’s takeTurn

/**

* Remove 1, 2, or 3 sticks from the specified Pile.

* The Pile must not be empty.

*/

public void takeTurn (Pile pile) {

pile.remove(1);

sticksTaken = 1;

}

NH-Chapter 3

parameters v s arguments
Parameters v.s. arguments
  • Arguments are provided in a method invocation by expressions. Thus we could write something like

pile.remove(sticksTaken + 1);

or even

pile.remove(2*sticksTaken+2);

NH-Chapter 3

parameters v s arguments1
Parameters v.s. arguments
  • Arguments must match in number, type and order:
  • An invocation of move must provide two arguments, an int and a double in that order.

publicvoid move (int direction, double distance) {

}

object.move(90, 2.5);

NH-Chapter 3

commands and queries
Commands and queries
  • A command invocation is a form of statement.
  • A query, which produces a value, is an expression.
  • If myCounter is a Counter object and i an int,

i = myCounter.currentCount();

i = myCounter.currentCount()+10;

NH-Chapter 3

example maze game
Example: Maze game
  • Player must find his/her way through a set of connected rooms to reach some goal.
    • there will be tricks player must figure out;
    • creatures of various kinds to be defeated along the way.

NH-Chapter 3

example maze game1
Example: Maze game
  • Objects for the picking:
    • player,
    • maze denizens,
    • rooms.

NH-Chapter 3

designing explorer
Designing Explorer
  • Explorer responsibilities:
    • know:
      • his/her name
      • location in the maze
      • amount of annoyance done when poking an opponent
      • amount of annoyance he/she can endure before being defeated.

NH-Chapter 3

designing explorer1
Designing Explorer
  • Responsibilities into properties
    • namename of the Explorer
    • locationroom in which Explorer is in
    • strengthmeasure of Explorer’s offensive ability
    • tolerancemeasure of what it takes to defeat Explorer

NH-Chapter 3

designing explorer2
Designing Explorer
  • Type of value for each Explorer’s property:
    • Name: String
    • Location: Room
    • Strength:int
    • Tolerance:int

Explorer

String

String name

Room location

Room

int

strength

10

int

tolerance

100

NH-Chapter 3

designing explorer3
Designing Explorer
  • Explorer responsibilities:
    • do:
      • change location in the maze (move from room to room)
      • fight a maze Denizen
  • commands to to perform these actions.
    • movechange location
    • poke poke a Denizen

NH-Chapter 3

designing explorer4
Designing Explorer
  • Both commands will have parameters:
    • movechange location (new location)
    • pokepoke a Denizen (denizen to poke)

NH-Chapter 3

a constructor for the class explorer
A constructor for the class Explorer
  • Need constructor for creating Explorer instances.
  • During creation properties must be initialized.
  • Require values for name, location, strength, and tolerance be provided as argument.
  • Constructor for Explorer has four parameters:

create new Explorer (name, location, strength, tolerance)

NH-Chapter 3

explorer specification
Explorer specification
  • mazeGame
  • Class Explorer
      • public class Explorer
        • A maze game player.
  • Constructors
      • public Explorer (String name, Room location, int strength, int tolerance)
        • Create a new Explorer with specified name, initial location, strength,and tolerance.
        • Annoyance (hit points) required to defeat this Explorer.

NH-Chapter 3

explorer specification1
Explorer specification
  • Queries
      • public String name ()
        • Name of this Explorer.
      • public Room location ()
        • Room in which this Explorer is currently located.
      • publicint strength ()
        • Annoyance (hit points) this Explorer causes when poking an opponent.
      • publicint tolerance ()
        • Annoyance (hit points) required to defeat this Explorer.

NH-Chapter 3

explorer specification2
Explorer specification
  • Commands
      • publicvoid move (Room newRoom)
        • Move to the specified Room.
      • publicvoid takeThat (int hitStrength)
        • Receive a poke of the specified number of hit points.
      • publicvoid poke (Denizen opponent)
        • Poke the specified Denizen.

NH-Chapter 3

implementing class explorer
Implementing class Explorer
  • Explorer objects have four properties:name, location, strength, and tolerance.
  • Use instance variables to store these values:

private String name; // name

private Room location; // current location

privateint strength; // current strength (hit points)

privateint tolerance; // current tolerance (hit points)

  • These variables are initialized in the constructor.

NH-Chapter 3

implementing class explorer1
Implementing class Explorer
  • Queries return current values of instance variables.
  • Example: query location returns value stored in location.

public Room location () {

return location;

}

NH-Chapter 3

implementing class explorer2
Implementing class Explorer
  • Implementing method move, argument value is stored in instance variable location:

public void move (Room newRoom) {

location = newRoom;

}

NH-Chapter 3

implementing class explorer3
Implementing class Explorer
  • Method takeThat is similar to Pile method remove. Argument value, hitStrength, is subtracted from instance variable tolerance and stored in tolerance:

public void takeThat (int hitStrength) {

tolerance = tolerance - hitStrength;

}

NH-Chapter 3

implementing class explorer4
Implementing class Explorer
  • Method poke invokes Denizen’s method takeThat:

public void poke (Denizen opponent) {

opponent.takeThat(strength);

}

NH-Chapter 3

local variables in methods
Local variables in methods
  • local variable:method variable created as part of method execution; used to hold intermediate results needed during computation.

NH-Chapter 3

local variables in methods1
Local variables in methods
  • Assume that RetailItem includes instance variables, with the obvious meanings:
  • Want to implement RetailItem method netPrice:

publicdouble netPrice ()

The cost of this RetailItem after discount, and including tax.

privatedouble basePrice;

privatedouble discountRate;

privatedouble taxRate;

NH-Chapter 3

local variables in methods2

Local variables

Local variables in methods

public double netPrice () {

double discount;

double subtotal;

double tax;

discount = basePrice * discountRate;

subtotal = basePrice - discount;

tax = subtotal * taxRate;

returnsubtotal + tax;

}

NH-Chapter 3

slide57
Defined inside a method.

Exists while method executes.

Must be initialized before used. otherwise, compiler error.

Accessed only from the method.

Meaningful only during method execution.

Contains some intermediate value needed only during execution of method; value is not part of object’s state.

Defined outside any method.

Exists as long as the object exists.

Initialized in a constructor.

Accessed from any class method.

Has meaningful value during life of object, whether or not object is actively doing something.

Represents an object’s property; its value is part of object’s state.

Local variables

Instance variables

NH-Chapter 3

putting together a complete system
Putting together a complete system
  • A complete system includes
    • model
    • user interface.
  • Example:
    • model Rectangle instance displayed on a computer screen; use int to measure dimensions.
    • User interface object: instance of class RectangleViewer, with one command, displayRectangle.

NH-Chapter 3

putting together a complete system1
Putting together a complete system
  • displayRectangle: writes Rectangle’s length, width, area, and perimeter to the screen.
  • RectangleViewer queries Rectangle for display data.

NH-Chapter 3

designing rectangle
Designing Rectangle
  • Rectangle know
    • Width
    • Length
  • Rectangle do
    • Compute area
    • Compute perimeter

NH-Chapter 3

rectangle specifications
Rectangle specifications

figures

Class Rectangle

public class Rectangle

Constructors

public Rectangle (int length, int width)

Create a new Rectangle with the specified length and width.

Queries

publicint length ()

The length of this Rectangle.

publicint width ()

The width of this Rectangle.

publicint area ()

The area of this Rectangle.

publicint perimeter ()

The perimeter of this Rectangle.

NH-Chapter 3

specifying rectangleviewer
Specifying RectangleViewer

publicvoid displayRectangle (Rectangle rectangle)

  • Write the length, width, area, and perimeter of the specified Rectangle to standard output.

NH-Chapter 3

system out println
System.out.println
  • Simple way to write output to the display.
  • Object System.out has a method
  • Executing the method results in its argument being written to the screen.

publicvoid println (String s)

Write a line containing the specified String to standard output.

NH-Chapter 3

implementing displayrectangle
Implementing displayRectangle

public void displayRectangle (Rectangle rectangle) {

int outputValue;

outputValue = rectangle.length();

System.out.println("length: " + outputValue);

outputValue = rectangle.width();

System.out.println("width: " + outputValue);

outputValue = rectangle.area();

System.out.println("area: " + outputValue);

outputValue = rectangle.perimeter();

System.out.println("perimeter: " + outputValue);

}

NH-Chapter 3

main method
main Method
  • Need a public class, containing a method main.
  • main method: top-level method that initiates execution of a system.
  • Method main is specified as:

publicstaticvoid main (String[] argv)

NH-Chapter 3

main method1
main Method
  • main used to
    • Create Rectangle and RectangleViewer,
    • Command RectangleViewer to display Rectangle.

NH-Chapter 3

main class
Main class

/**

* A simple system to display a Rectangle’s properties.

*/

publicclass RectangleDisplay {

// Run the program.

publicstaticvoid main (String[] argv) {

Rectangle theModel;

RectangleViewer theUserInterface;

theModel = new Rectangle(100,50);

theUserInterface = new RectangleViewer();

theUserInterface.displayRectangle(theModel);

}

}

NH-Chapter 3

the method tostring
The method toString
  • Useful to include in model classes a query to return a String representation of an object’s state.
  • In Java, this method is specified by convention as:

public String toString ()

A String representation of the object.

NH-Chapter 3

the method tostring1
The method toString
  • For example, define the method in the class Rectangle :

/**

* A String representation of this object.

*/

public String toString () {

return "Rectangle: length = " + length +

" width = " + width;

}

NH-Chapter 3

testing
Testing
  • Unit testing: test class being implemented to make sure it behaves as expected.
  • Functional testing: test entire system to ensure that it meets customer’s specifications.

NH-Chapter 3

test driven implementation
Test driven implementation
  • Incremental test-driven implementation: most effective means of reducing time required to track down and correct bugs.
  • Test-driven aspects
    • The process is to code a little, test a little.
    • Implementation is “test-driven.”: write test for a feature before implementing the feature.

NH-Chapter 3

test driven implementation example
Test driven implementation example
  • Stubbed implementation of TrafficSignal

publicclass TrafficSignal {

publicstaticfinalint GREEN = 0;

publicstaticfinalint YELLOW = 1;

publicstaticfinalint RED = 2;

privateint light;

public TrafficSignal () {

}

publicint light () {

return 0;

}

publicvoid change () {

}

}

Left blank

Dummy return value

Left blank

NH-Chapter 3

trafficsignaltest class
TrafficSignalTest class
  • TrafficSignalTest
    • Client of TrafficSignal
    • Creates instance of TrafficSignal
    • Queries and commands TrafficSignal instance
    • User interface object, reporting test results to user.

NH-Chapter 3

trafficsignaltest class1
TrafficSignalTest class
  • The only property of a TrafficSignalTest properties
    • TrafficSignal to be tested.
    • TrafficSignalTest function is to test the TrafficSignal.
  • Specify a single command for the class:

publicvoid runTest ()

Test a TrafficSignal.

NH-Chapter 3

trafficsignaltest class2
TrafficSignalTest class
  • How does the TrafficSignalTest get a TrafficSignal to test?
    • TrafficSignal could be an argument, either to TrafficSignalTest constructor or to method runTest;
    • TrafficSignal could be created by TrafficSignalTest, either in its constructor or in the method runTest

NH-Chapter 3

trafficsignaltest class3
TrafficSignalTest class

class TrafficSignalTest {

private TrafficSignal signal; // the object to test

//Create a TrafficSignalTest

public TrafficSignalTest () {

signal = new TrafficSignal();

}

// Run the test.

public void runTest () {

}

}

NH-Chapter 3

the initializing class
The initializing class

/**

* A simple test system for the class TrafficSignal.

*/

publicclass Test {

/**

* Run a TrafficSignal test.

*/

publicstaticvoid main (String[] argv) {

TrafficSignalTest test;

test = new TrafficSignalTest();

test.runTest();

}

}

NH-Chapter 3

writing tests
Writing tests
  • Use private methods testing each feature of TrafficSignal.
  • Place invocations of these methods in runTest()

NH-Chapter 3

testinitialstate
testInitialState

privatevoid testInitialState () {

System.out.println("testInitialState:");

System.out.println( "Initial light: " + signal.light());

}

publicvoid runTest () {

testInitialState();

}

  • May run test, getting misleading correct results; class TrafficSignal has only been stubbed.

NH-Chapter 3

trafficsignal constructor and query
TrafficSignal constructor and query
  • For class TrafficSignal need to implement
    • TrafficSignal constructor
    • Query light: testInitialState invokes it.
  • Run the test, getting correct result because we have correctly implemented the constructor and query.

public TrafficSignal () {

light = TrafficSignal.GREEN; //initial state

}

public int light () {

return light;

}

NH-Chapter 3

testing command change
Testing command change
  • Need to build a test for the command change.
    • give the command at least three times and see light cycle from green back to green.
  • Put this test in its own method and invoke it from runTest:

publicvoid runTest () {

testInitialState();

testChange();

}

NH-Chapter 3

testing command change1
Testing command change

private void testChange () {

System.out.println("testChange:");

System.out.println("Starting light: " + signal.light());

signal.change();

System.out.println("After 1 change: " + signal.light());

signal.change();

System.out.println("After 2 changes: " + signal.light());

signal.change();

System.out.println("After 3 changes: " + signal.light());

}

NH-Chapter 3

testing command change2
Testing command change
  • Recompiling and running TrafficSignalTest produces:
  • Recall, change() is stubbed as a do-nothing method.

testInitialState:

Initial light: 0

testChange:

Starting light: 0

After 1 change: 0

After 2 changes: 0

After 3 changes: 0

NH-Chapter 3

implementing command change
Implementing command change
  • Recompiling and running produces successful test.

public void change () {

light = (light + 1) % 3;

}

NH-Chapter 3

trafficsignal and trafficsignaltest
TrafficSignal and TrafficSignalTest
  • Have simple test system for the class TrafficSignal:
    • Can modify existing implementation,
    • Can add new tests to system,
    • Existing tests will help ensure that additions or modifications don’t break implementation.

NH-Chapter 3

static methods
Static methods
  • Methods not dependent on the state of an object.
    • Defined as static,
    • Associated with the class rather with a class instance.

NH-Chapter 3

static methods1
Static methods
  • Predefined class Math methods are all static.
  • Math includes a square root function specified as:

public static double sqrt (double a)

The positive square root of the specified value.

NH-Chapter 3

static method invocation
Static method invocation
  • A static function is invoked by prefixing the name of the class rather than the name of a class instance.
  • Diagonal method for Rectangle is written as:

public double diagonal () {

double a = (double)(length*length + width*width);

return Math.sqrt(a);

}

NH-Chapter 3

final features
Final features
  • Class named constants are defined as static final:
  • Specification static implies that feature is associated with the class not with an instance of the class.
  • Keyword final means that value to which identifier is bound cannot be changed.
  • Use class name when referring to feature:

publicstaticfinalint GREEN = 0;

light = TrafficSignal.GREEN;

NH-Chapter 3

importing static features
Importing static features
  • Can import static features of a class.
    • Can import one static features.

import static Math.sqrt;

import static TrafficSignal.GREEN;

  • This allows imported identifiers to be used without being prefixed with the class name

return sqrt(a);

light = GREEN;

NH-Chapter 3

importing static features1
Importing static features
  • Can import static features of a class.
    • Can import all static features defined in a class.

import static Math.*;

import static TrafficSignal.*;

  • This allows imported identifiers to be used without being prefixed with the class name

NH-Chapter 3

summary
Summary
  • designing with objects.
    • ask what should the object know and what should the object do.
    • enumerate the “knowing responsibilities” and the “doing responsibilities” of the object.

NH-Chapter 3

summary1
Summary
  • Object responsibilities are determined by the object’s role in supporting the functionality of the system.
  • System functionality is distributed to the comprising objects by assigning a set of responsibilities to each object.

NH-Chapter 3

summary2
Summary
  • “Knowing responsibilities” translate to queries when the object is defined.
  • “Doing responsibilities” translate into queries or commands.
  • Itemizing an object’s responsibilities determines the object’s features and its relationship to other objects that comprise the system.

NH-Chapter 3

summary3
Summary
  • We developed several examples in which one object acts as client to another.
  • Client object was provided with a reference to a server as a method argument.
  • Client was then able to use server by invoking its methods.

NH-Chapter 3

summary4
Summary
  • A command invocation is a form of statement.
  • The format for invoking a command is
  • Processor executes method body associated with command, which generally results in the object changing state.

object.command (arguments);

NH-Chapter 3

summary5
Summary
  • A query invocation is a form of expression, since it computes and returns value.
  • The format is similar to a command invocation:

object.query (arguments)

NH-Chapter 3

summary6
Summary
  • A constructor invocation creates a new object, and returns a reference to the newly created object.
  • The format is

newclass (arguments)

NH-Chapter 3

summary7
Summary
  • When a method with formal parameters is invoked,
    • method variable is allocated for each formal parameter
    • Method initialized with the argument provided by the client.
  • A local variable is another kind of method variable.
    • Local variables are created when a method is invoked
    • used to hold intermediate results needed during the computation.

NH-Chapter 3

summary8
Summary
  • Method variables are different from instance variables.
  • Instance variables
    • contain data that is part of the object’s state.
    • They are created when the object is created,
    • should always contain a meaningful value.
  • Method variables
    • created when a method is invoked,
    • contain values that are used only for a specific computation.

NH-Chapter 3

summary9
Summary
  • Developed a simple but complete system containing
    • a model object
    • user interface object.
  • To complete program, introduced a class with a single method named main.

NH-Chapter 3

summary10
Summary
  • main is the method that is executed when the program is run.
  • Its only function is to create the initial objects and get the system started.

NH-Chapter 3

summary11
Summary
  • In example,
    • main creates a model object ,
    • main creates user interface object,
    • main “starts” user interface, passing it model as argument.

NH-Chapter 3

summary12
Summary
  • We concluded with a brief introduction to unit testing.
  • To reduce time spent debugging and improve programmer efficiency, we adopt a test-driven implementation strategy.
  • Idea: develop a test for each feature or bit of functionality before implementing the feature or adding the functionality.

NH-Chapter 3

summary13
Summary
  • We construct a class to be used to test another existing class.
  • An instance of testing class invokes commands and queries of object under test, and displays information about its state.
  • By comparing actual results with expected results we can determine if the object under test behaves correctly.

NH-Chapter 3

ad