Home Java Java-tips Flow Methods Q-exercises Java: Method Exercises 1

Ask Questions?

View Latest Questions


 
 

Java: Method Exercises 1
Posted on: April 17, 2011 at 12:00 AM
This page discusses - Java: Method Exercises 1

Java Notes

Method Exercises 1

Name: _________________________________

What is the output from this program?

  1. ___________________________
  2. ___________________________
  3. ___________________________
  4. ___________________________
  5. ___________________________
  6. ___________________________
  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 
// File   : flow-methods/exercises/MethodExercises1.java
// Version: 24 Apr 2005

import javax.swing.*;

class MethodExercises1 {
    public static void main(String[] args) {
        output("A. ", 23);

        int n = divide(3, 2);
        output("B. ", n);

        outputInt(divide(8, 4));

        output("C. ", computeSomething(2, 100));
        output("D. ", computeSomething(10, 7));
        output("E. ", computeSomething(100, 100));
    }

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

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

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

    /** Does something! */
    private 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;
    }
}

Related Tags for Java: Method Exercises 1: