# Java: Method Exercises 1

Name: _________________________________

What is the output from this program?

      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
    // methods/exercises/MethodExercises1 - What is the output?
    // Fred Swartz

    import javax.swing.*;

    class MethodExercises1 {
        public static void main(String[] args) {
            int n;

            output("a ", 23);

            n = divide(3, 2);
            output("b ", n);

            output(divide(8, 4));

            output("cs1 ", computeSomething(2, 100));
            output("cs2 ", computeSomething(10, 7));
            output("cs3 ", computeSomething(100, 100));

            System.exit(0);
        }

        /** Displays an integer by calling another method. Overloaded, */
        static void output(int i) {
            output("Result = ", i);
        }

        /** Displays a message and an integer. Overloaded. */
        static void output(String message, int i) {
            JOptionPane.showMessageDialog(null, message + i);
        }

        /** Returns the result of dividing the first parameter by second. */
        static int divide(int a, int b) {
            return a / b;
        }

        /** Does something! */
        static int computeSomething(int x, int y) {
            int result;
            if (x > y) {
                result = x % y;
            } else if (x < y) {
                result =  divide(y, x);
            } else {
                result = 1;
            }
            return result;
        }

    }