Page 342 - Introduction to Programming with Java: A Problem Solving Approach
P. 342

                308 Chapter 8 Software Engineering
   }
// end class Shirt
Use the private access modifier for a helper method.
// Helper method prompts for and inputs user's selection
private String selectColor(String colorType)
 {
}
// end selectColor
Scanner stdIn = new Scanner(System.in);
String color; // chosen color, first a letter, then a word
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 =
break;
case 'r':
color =
break;
case 'y':
color =
"white";
  Apago PDF Enhancer
"yellow";
} // end switch
return color;
"red";
Figure 8.4b Shirt class—part B: selectColor helper method
8.4 Encapsulation (With Instance Variables and Local Variables)
We say that a program exhibits encapsulation if its data is hidden; that is, 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.
Encapsulation Implementation Guidelines
There are two main techniques for implementing encapsulation:
• First, break a big problem into separate classes where each class defines a set of encapsulated data that describe the current state of an object of that class. Encapsulate this object-state data by using the
 


























































   340   341   342   343   344