Topic 7: Design and Testing
Outline
- Software design
- Focus on object-oriented design
- Testing
- Graphics
Software Design
Overview
- Stages of software development
- Identification of classes and objects needed
- How to design classes
- Static members
- Relations between classes (depends, aggregation)
- Interfaces
- Enumerated types
- Method design
7.1 Software development
Four basic activities:
- Define the requirements
- Design
- Implementation
- Testing
Not necessarily sequential steps
Requirements
- State the problem, formally
- What should the program do?
Design
- How will the program do it?
- Building bridge without design?
- Time invested in design is well spent
- Easier to fix errors now than later
Implementation
- Coding, translating the design into a particular programming language
- "Too many programmers focus on implementation exclusively when actually it should be the least creative of all development activities."
Testing
- Making sure the program fulfills its requirements
- Run multiple times, scrutinize the outputs
Design
- Identifying classes and objects
- Static class members
- Relationships between classes
- Interfaces
- Enumerated types
- Method design
- Method overloading
7.2 Identifying classes and objects
- What are the classes?
- Nouns suggest objects, hence classes
- Verbs suggest methods
- Adjectives?
Object or Primitive Data?
- Should salary be an
int (primitive) or some type of object?
- Sometimes tricky
- Are there lower and upper bounds?
Generality and Specificity
- Strike a balance
- Appliances: toaster, dishwasher, mixer, blender?
- Maybe just a general Appliance class with a couple of subclasses
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:
- Fields (instance variables)
- Declared in the class
- One value per instance
- Constructors
- Methods
- Local variables
- Declared in a constructor or method
Static members
- Static fields
- Static methods
Static Fields
- Also called class variables
- Declared in the class, with the word
static
- All instances share one value
- Can be referenced through class:
Apricot.color
Math.PI
- May or may not be a constant
Static Methods
- Declared
static
- Can be called through the class
Apricot.fall()
Math.sqrt(10.0)
main method of any application
- Cannot refer to non-static fields (instance variables)
Example
- Listings 7.1–7.2: SloganCounter, Slogan
- Static variable
count
- Static method
getCount
7.4 Class relationships
- Relationships between classes
- Three kinds:
- Dependency
- Aggregation
- Inheritance (later)
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)
- Constructor
- Methods
reciprocal, add, subtract, multiply, divide, equals
- Private (why?) methods
reduce and gcd
- Explain with 4/5, 2/3
Aggregation
- An object has parts; it is made up of other objects
- Fields referring to other objects
- A car has a motor, steering wheel, doors, tires, …
- Has-a relationship
Example
StudentBody, Student, Address (Listings 7.5–7.7)
- Aggregate object Student
- Has Addresses
- Has Strings (often ignored)
this
- Reserved word
- Special variable in Java
- Like
self in Python, but not declared
- Refers to the object whose constructor or method is running
- Can be used to avoid shadowing of fields by local variables
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 = Unified Modeling Language
- Graphical language for system analysis and design
- Rumbaugh, Booch, Jacobs 1996+
7.5 Interfaces
- An interface declares constants and abstract methods
Abstract methods are declared without an implementation, i.e., method body
public interface Hittable
{
public static final int LUCKY = 7;
public int hit (int a, int b);
}
Implementing Interfaces
To implement an interface, a class must:
- Define all methods of the interface
- Declare that it implements the interface
The implementing class is a subtype of the interface.
Example
Complexity, Question, MiniQuiz (Listings 7.8–7.10)
Two Important Interfaces
- Comparable
- Iterator
Comparable Interface
One method:
public int compareTo (Object obj);
Value of a.compareTo(b) is
- < 0 if a < b
- == 0 if a = b
- > 0 if a > b.
Iterator Interface
Methods:
public boolean hasNext();
public Object next();
We know these.
Design and Testing
Part Two
- Enumerated types
- Methods
- Testing
- GUI
7.6 Enumerated types: Review
Enumerated Types: More
- Fields
- Constructors
- Static method
values() returns array of values
- Example: Season, SeasonTester (Listings 7.11–7.12)
- Other methods …
7.7 Method design
Overview
- Algorithms
- Top-down design
- How parameters are passed
Algorithms
- Algorithm: a step by step procedure for solving a problem
- Often developed in pseudocode
- Frees us from syntax of a programming language
- Design algorithms first, then code
Top-Down Design
- A way to solve complex problems
- Split problem into subproblems
- Solve each subproblem
- Combine solutions to subproblems
- Subproblem still too complex?
- Also called method decomposition
Example
PigLatin, PigLatinTranslator (Listings 7.13–7.14)
- Top-down design:
- How to translate a sentence?
- Translate each word
- What does word start with?
- Combine translations of words
- Helping methods are declared
private (why?)
Method parameters
Method arguments are passed by value
- Formal parameter receives a copy of actual argument's value
- But for reference types, what is copied is the reference (address) of the object
- The object is not copied
- Caller can mutate the object through its reference
- Primitive data: caller can mutate only the copy, not the original
Example
ParameterTester, ParameterModifier, Num (Listings 7.15–17)
- Contrasts passing of primitive and reference arguments
7.8 Method overloading
- Occurs when we define two methods with the same name in the same class (or super/subclass)
- Must have different signatures
- Number, type, and order of parameters
- Return type is not part of signature
- Cannot be changed independently
- Example:
println is overloaded several ways:
- 0-argument version
- 1-argument versions for each primitive type and for
Object.
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
- Strategies
- Unit testing with JUnit
What is testing?
"Testing" in the broad sense can include:
- Running a program with inputs and comparing the outputs with the known correct result.
- Program verification (proof of correctness)
- Hard for large-scale programs
- Reviews ("walkthrough")
- Let several people examine the design or code, looking for errors and other problems
When to Test?
- Think about testing early in the design process
- Goal is to find errors; earlier is better
Testing concepts
- Test case
- Test suite
- Regression testing
Testing strategies
- Black box testing
- Design tests according to requirements
- White box (glass box) testing
- Design tests with full knowledge of implementation
Black box testing
- Test inputs fall into equivalence categories
- Example: negative and non-negative numbers for a square root function
- Example: payroll calculation with overtime
- Include "edge" cases at and near (off by 1) the boundaries of equivalence categories
- Include a values from inside each category
White Box testing
- Try to exercise every statement of the code
- Craft inputs to take all branches of an
if statement or loop
Remarks
- Testing is tedious but necessary
- Automate to the max to avoid monotony and human errors
- Test small pieces of code before putting together into big pieces (unit testing)
- JUnit for unit testing in Java. ("XUnit" family)
Graphics
- GUI design
- Layout managers
- Containment hierarchies
7.10 GUI design principles
- Know user needs and possible activities
- Avoid user input/command errors.
- Optimize user abilities
- Consistency
7.11 Layout managers
- Arrange the components within a container
- Varieties:
BorderLayout
BoxLayout (vertical or horizontal)
CardLayout
FlowLayout
GridLayout (two-dimensional)
GridBagLayout
Default Layout Managers
- Each kind of container has a default layout manager
JFrame: BorderLayout
JPanel: FlowLayout
- Call
setLayout to override the default
Example
LayoutDemo, etc. (Listings 7.18–7.23)
JTabbedPane provides the "main switch"
7.12 Borders
Borders of a component can be decorated
BorderDemo (Listing 7.24)
7.13 Containment hierarchies
Layouts within layouts
- Box diagram
- Tree diagram
- See Figures 7.12–7.13.
- Can be helpful in understanding the GUI structure