Home Java Java-tips Flow Methods Examples - Method and loop review

View Latest Questions

Examples - Method and loop review
Posted on: July 26, 2006 at 12:00 AM
The examples in this program are intended for reviewing methods and loops.

# Examples - Method and loop review

Another review examples of Method and loops.

Learn Methods and loops in more detail.

The examples in this program are intended for reviewing methods and loops.

 ```// SampleMethods.java -- Shows loop examples inside methods... // Author: Fred Swartz - 2005 Dec 11 import javax.swing.*; import java.util.*; public class SampleMethods { //======================================================= primeFactor // Returns a factor of n, or 0 if n is prime. // Uses simple algorithm of dividing by all numbers up to n. // Could improve efficiency many ways. public static int primeFactor(int n) { for (int divisor = 2; divisor < n; divisor++) { if ((n % divisor) == 0) { return divisor; // Divisible implies not prime! } } return 0; // Must be prime if nothing was able to divide it. } //======================================================= countVowels // Or could use substring, ||, switch, nested for loops, ... public static int countVowels(String text) { int vowelCount = 0; for (int pos = 0; pos < text.length(); pos++) { if ("aeiouAEIOU".indexOf(text.charAt(pos)) >= 0) { vowelCount++; } } return vowelCount; } //========================================================= summation // Returns sum off all numbers from 1 to n. // Uses loop instead of just returning (n * (n+1)) / 2 public static int summation(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } //========================================================== reverse // Reverse string. Should use StringBuilder for efficiency. public static String reverse(String s) { String rev = ""; for (int pos = 0; pos < s.length(); pos++) { rev = s.substring(pos, pos+1) + rev; } return rev; } //================================================= readAndAddDialog public static int readAndAddDialog() { int sum = 0; while (true) { // Loop until Cancel or close box clicked. String input = JOptionPane.showInputDialog(null, "Input"); if (input == null) { break; //***************************** EXIT LOOP } int num = Integer.parseInt(input); sum += num; } return sum; } //================================================= readAndAddScanner public static int readAndAddScanner() { Scanner in = new Scanner(System.in); int sum = 0; System.out.println("Enter numbers to sum. Non-number terminates."); while (in.hasNextInt()) { // Loop until no integer int num = in.nextInt(); sum += num; } return sum; } //=============================================================== main public static void main(String[] args) { assert primeFactor(2) == 0 : "primeFactor(2)"; assert primeFactor(4) != 0 : " primeFactor(4)"; assert primeFactor(112) != 0 : "primeFactor(112)"; assert primeFactor(113) == 0 : "primeFactor(113)"; assert countVowels("") == 0 : "countVowels(\"\")"; assert countVowels("a") == 1 : "countVowels(\"a\")"; assert countVowels("e") == 1 : "countVowels(\"e\")"; assert countVowels("i") == 1 : "countVowels(\"i\")"; assert countVowels("o") == 1 : "countVowels(\"o\")"; assert countVowels("u") == 1 : "countVowels(\"u\")"; assert countVowels("A") == 1 : "countVowels(\"A\")"; assert countVowels("E") == 1 : "countVowels(\"E\")"; assert countVowels("I") == 1 : "countVowels(\"I\")"; assert countVowels("O") == 1 : "countVowels(\"O\")"; assert countVowels("U") == 1 : "countVowels(\"U\")"; assert countVowels("x") == 0 : "countVowels(\"x\")"; assert countVowels("x") == 0 : "countVowels(\"x\")"; assert countVowels("nw s th tm fr...") == 0 : "countVowels(\"nw s th tm fr..\")"; assert countVowels("now is the time for...") == 6 : "countVowels(\"now is the time for...\")"; assert summation(1) == 1 : "summation(1)"; assert summation(2) == 3 : "summation(2)"; assert summation(3) == 6 : "summation(3)"; assert summation(4) == 10 : "summation(4)"; assert reverse("").equals("") : "reverse(\"\")"; assert reverse("a").equals("a") : "reverse(\"a\")"; assert reverse("ab").equals("ba") : "reverse(\"ab\")"; assert reverse("abc").equals("cba") : "reverse(\"abc\")"; int r1 = readAndAddDialog(); JOptionPane.showMessageDialog(null, "Sum is " + r1); int r2 = readAndAddScanner(); System.out.println("Sum is " + r2); } } ```
Copyleft 2005 Fred Swartz MIT License