Java Reference
In-Depth Information
Some Text for #3
MsgSize for PrintfileErrorMsg = 16
15.
Let's create a class data member in PrintfileErrorMsg that hides the variable
with the same name in MessageText . You will experiment with this variable
and see how it affects the classes that use PrintfileErrorMsg . Using the text
editor, open the PrintfileErrorMsg.java file, and add these bolded lines (pay
particular attention to the super. and this. identifiers for msgSize ).
//
//
// PrintfileErrorMsg
//
//
class PrintfileErrorMsg extends ErrorMsg {
private static int outputLineSize = 80;
public int linesToSkip = 0;
private int charsToSkip = 0;
// Create a version of this variable that hides the one in TextMessage.
public int msgSize;
// Define a setErrorMsg method that establishes the number of
// chars to output in order to center the error msg.
public void setErrorMsg (String inputMsg) {
super.setErrorMsg (inputMsg);
charsToSkip = (outputLineSize - super.msgSize) / 2;
this.msgSize = super.msgSize + charsToSkip;
...
At the bottom of the class definition insert the bolded lines as follows:
// Print out the error message.
System.out.println (getErrorMsg ());
// Print out the two msgSize variables.
System.out.println ("this.msgSize = " + this.msgSize + ",
super.msgSize = " + super.msgSize);
16. Save and compile this class:
javac PrintfileErrorMsg.java
Search WWH ::




Custom Search