Chapter 6: Supplier Classes



Chapter 6: Supplier Classes

Homework: Page 204 1a, 2a, 3, 4, 5a, 6, 7

Objectives

To introduce the client-supplier (composition) relation and examine the role of supplier classes and client classes in a complete program

To show an additional class diagram notation for composition

To explore the differences between public and private declarations

To revisit the Java notation for qualified expressions

To introduce scope and lifetime rules for variables and methods

To explore the various issues of class design including selecting the best location for a feature and how to properly hide information to create thin interfaces

To examine the notions of read-only and write-only access to data and to demonstrate a technique for implementing both kinds of restricted access

To introduce the concept of reusing an identifier an examine Java rules for such reuse, including method overloading

To examine the char primitive data type for implementing single characters

To introduce the String data type for implementing a character string

To introduce the Alabel class as a means for displaying a single line of text

“The client-supplier relation is common in the retail industry.”

6.1 Clients and Suppliers in Software

A client class is one that borrows the facilities of another class (the supplier class).

Client can call a method or access a parameter of the supplier.

Example in the text is Director class (the client) using

AWindow and AOval to supply instance variables

Math supplies one of its methods

If classes supply types for one or more variables in a client class, the client is composed of those supplier classes

e.g.

Director class is composed of AWindow and AOval

This is also called aggregation. The client is an aggregate of the instance variables.

How do you add supplier code?

6.2 Another Client

Instance variables, constants, and methods of a class are said to be encapsulated members of that class.

public and private are used to control member encapsulation.

public is available anywhere

private are only accessible within the class itself

The MoneyUSA class

ObjectReference p. 166

Alternative ways to identify an object in Java.

A variable name

A nonvoid method call that returns the object

A local variable name

The name of a formal parameter

In order to use a class’s public method, you must qualify it with an object reference. (known as qualified expressions)

e.g. s.drawBus(25, 35);

6.3 Suppliers

To create a supplier class start, with the class diagram and class specification.

The idea is to know what you want to do before you start.

The example used in this section is the Personal Budget class uses the MoneyUSA class as a supplier.

This is the same method that we have been using to create a SchoolBus from ARectangle and AOval.

Please see the composition MoneyUSA, PersonalBudget, and Director in Figure 6.9

An object diagram includes a separate rectangle for each reference object.

The following code files are

1. the MoneyUSA supplier class to the

2. PersonalBudget class which is in turn a supplier to the

3. client class Director (not really the right program)

The MoneyUSA class is used to create cashOnHand, oustandingExpenses, and savings in personalBudget.

The personalBudget class is used to create myBudget in Director.

/* class invariant

A MoneyUSA object maintains a monetary amount as an integer count of

dollars, quarters, dimes, nickels and pennies AND dollars>=0

AND quarters>=0 AND dimes>=0 AND nickels>=0 AND pennies>=0 */

public class MoneyUSA {

public int dollars;

public int quarters;

public int dimes;

public int nickels;

public int pennies;

/* precondition

dol>=0 AND q>=0 AND di>=0 AND n>=0 AND p>=0

postcondition

dollars == dol AND quarters == q AND dimes == di

AND nickels == n AND pennies == p */

public MoneyUSA(int dol, int q, int di, int n, int p) {

dollars = dol;

quarters = q;

dimes = di;

nickels = n;

pennies = p;

}

/* postcondition

result == dollars*100+quarters*25+dimes*10+nickels*5+pennies */

public int valueInCents() {

return dollars*100+quarters*25+dimes*10+nickels*5+pennies;

}

/* postcondition

result == (dollars*100+quarters*25+dimes*10+nickels*5+pennies)/100 */

public double valueInDollars() {

return valueInCents() / 100.0;

}

/* postcondition

valueInCents() == old valueInCents() AND 0 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download