/** * An overloading example

* Write a method numberOfCharacters that counts how many characters are * needed to print its argument.
* The method should work with an int, a long, and a String. */ public class OverloadingExample { /** * postcondition: * result == number of characters to print a long * @param n the long to print */ public int countCharactersToPrint(long n) { int count=1; // at least one character if (n<0){ count++; // for the minus sign n=-n; } while( (n=n/10)!=0 ) count++; return count; // Or more clever // replace all of the above with // return (n+"").length(); } /** * postcondition: * result == number of characters to print an integer * @param n the integer to print */ public int countCharactersToPrint(int n) { return countCharactersToPrint((long)n); } /** * postcondition: * result == number of characters to print a String * @param s the String to print */ public int countCharactersToPrint(String s) { if (s==null) return 4; // null is printed else return s.length(); } }