Topic 7: Design and Testing

Outline

Software Design

Overview

7.1 Software development

Four basic activities:

  1. Define the requirements
  2. Design
  3. Implementation
  4. Testing

Not necessarily sequential steps

Requirements

Design

Implementation

Testing

Design

7.2 Identifying classes and objects

Object or Primitive Data?

Generality and Specificity

Objects Not Mentioned

Besides the objects mentioned in the problem statement, other classes may be needed to get the job done.

Re-Use Existing Classes

Some of the classes we need may already exist; reuse them.

7.3 Static class members

Review:

Static members

Static Fields

Static Methods

Example

7.4 Class relationships

Dependency

Class A depends on class B if A uses some of B's methods.

Can be through references to instances of B, or through static methods.

Too many dependencies may complicate program revision.

A class can depend on itself:

a.foo(b);  // a, b instances of same class

Example

RationalNumber, RationalTester (Listings 7.3–7.4)

Aggregation

Example

StudentBody, Student, Address (Listings 7.5–7.7)

this

Examples of this

this.x = x; // in a constructor with parameter x
this.x = (here.x + there.x) / 2.0;
deltaX = (other.x - this.x);
this.foo(); // same as just foo();
button.addActionListener(this);

UML Diagrams

UML: Dependency

Dependency

Dependency

UML: Aggregation

Aggregation

Aggregation

Example

Figure 7.2 (simplified)

Figure 7.2 (simplified)

7.5 Interfaces

Implementing Interfaces

To implement an interface, a class must:

The implementing class is a subtype of the interface.

Interfaces in UML

Implements interface

Implements interface

Example

Complexity, Question, MiniQuiz (Listings 7.8–7.10)

Figure 7.3 (simplified)

Figure 7.3 (simplified)

Two Important Interfaces

  1. Comparable
  2. Iterator

Comparable Interface

One method:

public int compareTo (Object obj); 

Value of a.compareTo(b) is

Iterator Interface

Methods:

public boolean hasNext();
public Object next();

We know these.

Design and Testing

Part Two

7.6 Enumerated types: Review

Enumerated Types: More

7.7 Method design

Overview

Algorithms

Top-Down Design

Example

PigLatin, PigLatinTranslator (Listings 7.13–7.14)

Method parameters

Method arguments are passed by value

Example

ParameterTester, ParameterModifier, Num (Listings 7.15–17)

7.8 Method overloading

Constructor overloading

Constructors, like methods, can be overloaded, allowing flexibility in the creation of new objects.

Overloading is syntactic sugar

Overloading does not increase the expressive power of the language, i.e., it does not enable us to compute functions that we could not computer otherwise. Without overloading, we'd just have to invent different names for each of the variants—e.g., println0, printlnInt, etc.

7.9 Testing

What is testing?

"Testing" in the broad sense can include:

  1. Running a program with inputs and comparing the outputs with the known correct result.
  2. Program verification (proof of correctness)
    • Hard for large-scale programs
  3. Reviews ("walkthrough")
    • Let several people examine the design or code, looking for errors and other problems

When to Test?

Testing concepts

Testing strategies

Black box testing

White Box testing

Remarks

  1. Testing is tedious but necessary
  2. Automate to the max to avoid monotony and human errors
  3. Test small pieces of code before putting together into big pieces (unit testing)
  4. JUnit for unit testing in Java. ("XUnit" family)

Graphics

7.10 GUI design principles

  1. Know user needs and possible activities
  2. Avoid user input/command errors.
  3. Optimize user abilities
  4. Consistency

7.11 Layout managers

Default Layout Managers

Example

LayoutDemo, etc. (Listings 7.18–7.23)

7.12 Borders

Borders of a component can be decorated

BorderDemo (Listing 7.24)

7.13 Containment hierarchies

Layouts within layouts