Chapter 8 - Software Engineering
Chapter 8 - Software Engineering
Coding-Style Conventions
Helper Methods
Encapsulation (with Instance Variables and Local
Variables)
Algorithmic Design with OOP
Top-down Design
Stubs
Merging Driver Method into Driven Class
Accessing Instance Variables Without Using this
2
Coding-Style Conventions
/*********************************************************
* StudentDriver.java
* Dean & Dean
*
* This class acts as a driver for the Student class.
*********************************************************/
s1 = new Student();
s1.setFirst("Adeeb");
s1.setLast("Jarrah");
s2 = new Student("Chun", "HeeJoo");
s2.printFullName();
} // end main
} // end class StudentDriver
3
Coding-Style Conventions
/*********************************************************
* Student.java
* Dean & Dean
*
* This class handles processing of a student's name.
*********************************************************/
import java.util.Scanner;
//*******************************************************
public Student()
{ }
Coding-Style Conventions
//*******************************************************
Coding-Style Conventions
// This method verifies that last starts with an uppercase
// letter and contains lowercase letters thereafter.
//*******************************************************
Helper Methods
As with all examples discussed so far, methods
normally use the public access modifier.
public methods are considered to be part of the
class's interface.
Sometimes, you'll want to create a method that
should not be part of the interface; instead it just
supports the operation of other methods within its
own class.
Those methods are called helper methods and they
should use the private access modifier.
8
Helper Methods
Suppose you're asked to write a program that handles order entries for
sports-uniform shirts. For each shirt order, the program should prompt
the user for a shirt's primary color and its trim color. For each color
selection, the program should perform the same input validation. It
should verify that the entered color is one of three values – w, r, or y,
for white, red, or yellow. That input validation code is non-trivial. It's in
charge of:
Prompting the user for a color entry.
Checking whether the entry is valid.
Repeating the prompt if the entry is invalid.
Converting the single-character color entry to a full-word color value.
Rather than having the complete input validation code appear every
time color selection is needed, you should use a helper method to
implement the color-selection task.
With a helper method, the color-selection code appears only once in
the program, and whenever color selection is needed, the color-
selection method is called.
9
Helper Methods
import java.util.Scanner;
//*********************************************************
public Shirt()
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter person's name: ");
this.name = stdIn.nextLine();
this.primary = selectColor("primary");
this.trim = selectColor("trim");
} // end constructor
No need for a reference
//*********************************************************
variable dot prefix here.
public void display()
{
System.out.println(this.name + "'s shirt:\n" +
this.primary + " with " + this.trim + " trim");
} // end display
//*********************************************************
10
Helper Methods
Use the private access modifier for a helper method.
// Helper method prompts for and inputs user's selection
do
{
System.out.print("Enter shirt's " + colorType +
" color (w, r, y): ");
color = stdIn.nextLine();
} while (!color.equals("w") && !color.equals("r") && !color.equals("y"));
switch (color.charAt(0))
{
case 'w':
color = "white";
break;
case 'r':
color = "red";
break;
case 'y':
color = "yellow";
} // end switch
return color;
} // end selectColor
} // end class Shirt
11
Helper Methods
In the Shirt constructor from two slides ago, note how we call
the selectColor method without a reference variable prefix:
this.primary = selectColor("primary");
Encapsulation
A program exhibits encapsulation if its data is
hidden; i.e., if its data is difficult to access from the
“outside world.”
Why is encapsulation a good thing?
Since the outside world isn't able to directly access the
encapsulated data, it's more difficult for the outside world
to mess things up.
How is encapsulation implemented?
Break a big problem into separate classes where each class
holds a set of encapsulated data (those data items are
called instance variables, and they should use the
private access modifier).
Break a class's tasks into separate methods where each
method holds a set of encapsulated data (those data items
are called local variables).
13
Encapsulation
Declaring data as instance variables within a class is one form
of encapsulation, and declaring data as local variables within a
method is another form of encapsulation. Which is the
stronger (more hidden) form of encapsulation?
An instance variable may be shared by all the methods in the
class, whereas a local variable is local to one method, so a local
variable is more encapsulated.
Thus, in the interest of encapsulation, you should use local
variables, as opposed to instance variables, whenever
possible.
Declare a variable locally within a method:
important
If the variable needs to persist (remain alive) only for the duration of
that particular method.
Declare a variable as an instance variable:
If the variable stores a fundamental attribute of the class's objects.
If the variable needs to persist for the duration of the class's objects
(i.e., it needs to persist longer than the duration of one particular
method execution).
14
Stubs
After writing your entire program in pencil on paper, enter your
program on a PC using stubs for your subtask methods. A stub
is a dummy method like this:
public void drawSides()
{
System.out.println("In drawSides");
}
Benefit of using stubs:
If the program is initially entered with stubs, then the programmer
will be able to more easily get an initial successful compilation
(and that leads to a "warm fuzzy" feeling).
Debugging is easier.
After the program is successfully compiled and run with stubs, then
the programmer replaces the stubs with actual code one method at a
time.
Ideally, after each method is replaced, it's thoroughly debugged.
That way, if a bug appears, it should be easy to find since you know
it's probably in the newly replaced method.
20
So far, we've split each of our OOP programs into two separate
files - a file that describes an object (e.g., Mouse, Car) and a
driver file (e.g., MouseDriver, CarDriver).
It's legal to merge the two files into one file, but we've refrained
from doing that because using two separate files leads to a
more understandable program.
We'll continue to use separate files for most programs, but for
short programs that don't do much except illustrate a concept,
we'll merge the main method into the driven class.
Why?
It's a matter of convenience - there's one less file to create and
there's slightly less code to enter.
21
//*******************************************************
//*******************************************************
//*******************************************************
//************************************************
//************************************************
//************************************************