Demo - Static Functions and Overloading

package StaticClassDemo; /** * Static Header Class * Class methods for use to generate a program header and footer */ public class HeaderClass { //variable to keep track of the times one of the class methods was called private static int numberTimesUsed = 0; //Static Utility to print a header - take a string argument to display public static String PrintHeader(String pText) { String myReturn = ""; myReturn += PrintLine(); myReturn += pText + "\n"; myReturn += PrintLine(); numberTimesUsed++; return myReturn; } //overloaded version that has no argument public static String PrintHeader() { String myReturn = ""; myReturn += PrintLine(); myReturn += "Welcome to my Program\n"; myReturn += PrintLine(); numberTimesUsed++; return myReturn; } //Static Utility to print a footer public static String PrintFooter() { String myReturn = ""; myReturn += PrintLine('*'); myReturn += "End of Program\n"; myReturn += PrintLine('*'); numberTimesUsed++; return myReturn; } //Getter to return Utility Method was used public static int getTimesUsed() { return numberTimesUsed; } //Private Methods to use in the class //Method prints a line of Characters private static String PrintLine() { return "######################################\n"; } //overloaded PrintLine Function that prints a character private static String PrintLine(char myChar) { String myReturn = ""; for(int i = 0; i < 25; i++) { myReturn += myChar; } return myReturn + "\n"; } }//end of class
package StaticClassDemo; /** * @author: Kevin Roark * Static Header Class Demo main */ public class HeaderClassDemo { public static void main(String[] args) { //print the header System.out.println(HeaderClass.PrintHeader()); //now use the overloaded version System.out.println(HeaderClass.PrintHeader("This is an overloaded method")); //now use the print Footer System.out.println(HeaderClass.PrintFooter()); System.out.println("The methods of the Header Class were used: " + HeaderClass.getTimesUsed()); } }

 

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark