Page 389 - Introduction to Programming with Java: A Problem Solving Approach
P. 389
9.6 Using Class Members in Conjunction with Instance Members 355
/********************************************************************
*
PrintUtilities.java
Dean & Dean
This class contains constants and methods for fancy printing.
********************************************************************/
*
*
*
public class PrintUtilities
{
}
// end class PrintUtilities
public static final int MAX_COL = 80; // last allowed column
public static final int MAX_ROW = 50; // last allowed row
//*****************************************************************
// Print given string horizontally centered.
public static void printCentered(String s)
{
}
// end printCentered
{
}
// end printUnderlined
int startingCol; // starting point for string
startingCol = (MAX_COL / 2) - (s.length() / 2);
for (int i=0; i<startingCol; i++)
{
}
System.out.print(" ");
Apago PDF Enhancer
System.out.println(s);
//*****************************************************************
// Print given string with dashes underneath it.
public static void printUnderlined(String s)
System.out.println(s);
for (int i=0; i<s.length(); i++)
{
}
System.out.print("-");
Figure 9.3 Example utility class that handles special-needs printing
with an implementation view of the problem and developing the program from bottom up, because our current focus is on the details of instance and class members.
See Figure 9.5. It contains an implementation of the PennyJar class. Let’s first examine PennyJar’s constant and variable declarations:
• GOAL is the target number of pennies to be saved for all penny jars combined. As such, it’s a class member and uses the static modifier. Since the goal amount is fixed, GOAL is a named constant and uses the final modifier and all uppercase letters. The GOAL is initialized to 10000, which amounts