# Java Notes

# Examples - Method and loop review

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

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    // 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);
        }
    }