SOURCE CODE FOR StaticMethods EXAMPLES ------------------------------------------------------------ /** * Project CountDown1 * Class Main * * The main method invokes the static method * countDown. */ public class Main { public static void main(String[] args) { Main.countDown(5); // Note: since the preceding static // method call Main.countDown(5); is // within the Main class, this call // can be abbreviated as // // countDown(5); } private static void countDown(int length) { int i; for(i = length; i >= 0; i--) { System.out.print(i + "..."); } System.out.println(); System.out.println("Blastoff!"); } } // Output: 5...4...3...2...1...0... Blastoff! ------------------------------------------------------------ /** * Project CountDown2 * Class Main * * The main method invokes the static method * countDown. The blastoff message is passed * in as well as the number of seconds in the * countdown. */ public class Main { public static void main(String[] args) { Main.countDown(5, "Blastoff!"); // Note: since the preceding static // method call Main.countDown(5); is // within the Main class, this call // can be abbreviated as // // countDown(5); } private static void countDown(int length, String message) { int i; for(i = length; i >= 0; i--) { System.out.print(i + "..."); } System.out.println(); System.out.println(message); } } // Output: 5...4...3...2...1...0... Blastoff! ------------------------------------------------------------ // Project IWillNot1 // Class Main public class Main { public static void main(String[] args) { repeatLines(); } public static void repeatLines() { int i; for(i = 1; i <= 20; i++) System.out.println(i + ". " + "I will not talk in class."); } } // Output: 1. I will not talk in class. 2. I will not talk in class. 3. I will not talk in class. 4. I will not talk in class. 5. I will not talk in class. 6. I will not talk in class. 7. I will not talk in class. 8. I will not talk in class. 9. I will not talk in class. 10. I will not talk in class. 11. I will not talk in class. 12. I will not talk in class. 13. I will not talk in class. 14. I will not talk in class. 15. I will not talk in class. 16. I will not talk in class. 17. I will not talk in class. 18. I will not talk in class. 19. I will not talk in class. 20. I will not talk in class. ------------------------------------------------------------ // Project IWillNot2 // Class Main // repeatLines has the parameter line to indicate // the line to be repeated. public class Main { public static void main(String[] args) { repeatLines("I will not throw spitballs."); } public static void repeatLines(String line) { int i; for(i = 1; i <= 20; i++) System.out.println(i + ". " + line); } } // Output: 1. I will not throw spitballs. 2. I will not throw spitballs. 3. I will not throw spitballs. 4. I will not throw spitballs. 5. I will not throw spitballs. 6. I will not throw spitballs. 7. I will not throw spitballs. 8. I will not throw spitballs. 9. I will not throw spitballs. 10. I will not throw spitballs. 11. I will not throw spitballs. 12. I will not throw spitballs. 13. I will not throw spitballs. 14. I will not throw spitballs. 15. I will not throw spitballs. 16. I will not throw spitballs. 17. I will not throw spitballs. 18. I will not throw spitballs. 19. I will not throw spitballs. 20. I will not throw spitballs. ------------------------------------------------------------ // Project IWillNot3 // Class Main // repeatLines has the parameters line that indicates // the line to be repeated, and times that indicates // the number of times to repeat the line. public class Main { public static void main(String[] args) { repeatLines("I will not throw spitballs.", 15); } public static void repeatLines(String line, int times) { int i; for(i = 1; i <= times; i++) System.out.println(i + ". " + line); } } // Output: 1. I will not throw spitballs. 2. I will not throw spitballs. 3. I will not throw spitballs. 4. I will not throw spitballs. 5. I will not throw spitballs. 6. I will not throw spitballs. 7. I will not throw spitballs. 8. I will not throw spitballs. 9. I will not throw spitballs. 10. I will not throw spitballs. 11. I will not throw spitballs. 12. I will not throw spitballs. 13. I will not throw spitballs. 14. I will not throw spitballs. 15. I will not throw spitballs. ------------------------------------------------------------ /** * Project OldMcDonald * Class OldMcDonald * * Use static methods to avoid duplicate code. */ import javax.swing.JOptionPane; public class OldMcDonald { public static void main(String[] args) { String animal, sound; animal = JOptionPane.showInputDialog( "Enter an animal."); sound = JOptionPane.showInputDialog( "Enter an animal sound."); printSong(animal, sound); } public static void printSong( String theAnimal, String theSound) { printRefrain(); System.out.println( "And on his farm he had a " + theAnimal + ","); printExclamation(); System.out.println( "With a " + theSound + ", " + theSound + " here "); System.out.println( "And a " + theSound + ", " + theSound + " there."); System.out.println( "Here a " + theSound + ", there a " + theSound + ","); System.out.println( "Everywhere a " + theSound + ", " + theSound + "."); printRefrain(); } public static void printRefrain() { System.out.println("Old McDonald had a farm,"); printExclamation(); } public static void printExclamation() { System.out.println("Ee, ay, ee, ay, oh."); } } // Output: Old McDonald had a farm, Ee, ay, ee, ay, oh. And on his farm he had a cow, Ee, ay, ee, ay, oh. With a moo, moo here And a moo, moo there. Here a moo, there a moo, Everywhere a moo, moo. Old McDonald had a farm, Ee, ay, ee, ay, oh. ------------------------------------------------------------ /** * Project Square * Class Main * * Call the static method square that returns * the square of its input. */ public class Main { public static void main(String[] args) { int x, y; x = 5; y = Main.square(x); System.out.println(y); } private static int square(int a) { int b; b = a * a; return b; } } // Output: 25 ------------------------------------------------------------ // Project VowelCount // Class Main // Write and test the vowelCount method that counts // the number of vowels in the input string s. public class Main { public static void main(String[] args) { System.out.println(vowelCount("elephant")); System.out.println(vowelCount("tiger")); System.out.println(vowelCount("gorilla")); System.out.println(vowelCount("bdgt")); System.out.println(vowelCount("")); } public static int vowelCount(String s) { int i, count = 0; char x; String u; u = s.toUpperCase(); for(i = 0; i < s.length(); i++) { x = u.charAt(i); if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U') count++; } return count; } } // Output: 3 2 3 0 0 ------------------------------------------------------------ // Project UpperCaseCount // Class Main // Write and test the upperCaseCount method that counts // the number of vowels in the input string s. public class Main { public static void main(String[] args) { System.out.println(upperCaseCount("This is a test")); System.out.println(upperCaseCount("ABCDE")); System.out.println(upperCaseCount("abcde")); System.out.println(upperCaseCount("12345")); System.out.println(upperCaseCount("")); } public static int upperCaseCount(String s) { int i, count = 0; char x; for(i = 0; i < s.length(); i++) { x = s.charAt(i); if('A' <= x && x <= 'Z') count++; } return count; } } // Output: 1 5 0 0 0 ------------------------------------------------------------ // Project ExistsADigit // Class Main // Write and test the existsADigit method that returns // true if there is at least one digit in the // input string s. public class Main { public static void main(String[] args) { System.out.println(existsADigit("bsrct8corsnd")); System.out.println(existsADigit("bsrctycorsnd")); System.out.println(existsADigit("987654321234")); System.out.println(existsADigit("")); } public static boolean existsADigit(String s) { int i; char x; boolean flag = false; for(i = 0; i < s.length(); i++) { x = s.charAt(i); if('0' <= x && x <= '9') flag = true; } return flag; } } // Output: true false true false ------------------------------------------------------------ // Project AreAllAsterisks // Class Main // Write and test the areAllAsterisks method that // returns true if all of the characters in the // string s are asterisks. Note that true is returned // for a zero length input string s because each of // the characters (namely 0) in s is '*'. public class Main { public static void main(String[] args) { System.out.println(areAllAsterisks("**********")); System.out.println(areAllAsterisks("******&***")); System.out.println(areAllAsterisks("*********&")); System.out.println(areAllAsterisks("")); } public static boolean areAllAsterisks(String s) { int i; char x; boolean flag = true; for(i = 0; i < s.length(); i++) { x = s.charAt(i); if(x != '*') flag = false; } return flag; } } // Output: true false false true ------------------------------------------------------------ // Project ExistsAnX // Class Main // Write and test the existsAnX method that returns // true if at least one X or x is in the string, // false otherwise. public class Main { public static void main(String[] args) { System.out.println(existsAnX("bsrctxcorsnd")); System.out.println(existsAnX("bsrctXcorsnd")); System.out.println(existsAnX("bsrctacorsnd")); System.out.println(existsAnX("")); } public static boolean existsAnX(String s) { int i; char x; boolean flag = false; for(i = 0; i < s.length(); i++) { x = s.charAt(i); if(x == 'X' || x == 'x') flag = true; } return flag; } } // Output: true true false false ------------------------------------------------------------ // Project FirstAndLastAreEqual // Class Main // Write and test the firstAndLastAreEqual method that // Return true if the first and last character of the // input string s. Return false if s.length() == 0. public class Main { public static void main(String[] args) { System.out.println(firstAndLastAreEqual("elephant")); System.out.println(firstAndLastAreEqual("pop")); System.out.println(firstAndLastAreEqual("a")); System.out.println(firstAndLastAreEqual("")); } public static boolean firstAndLastAreEqual(String s) { int i; char first, last; if (s.length() == 0) return false; else { first = s.charAt(0); last = s.charAt(s.length() - 1); return first == last; } } } // Output: false true true false ------------------------------------------------------------