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

Ask Questions?

View Latest Questions


 
 

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

Java Notes

Method Exercises 5

Name: _________________________________

What is the output from this program?

  1. ___________________________
  2. ___________________________
  3. ___________________________
  4. ___________________________
  5. ___________________________
  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 
// File  : flow-methods/exercises/MethodExercises5.java
// Author: Fred Swartz
// Date  : 13 Oct 2005

import javax.swing.*;

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

        output("B. " + convertInt(88));

        String x = convertInt(divide(8, 4));

        int n = 5;
        output("C. ", inc(inc(inc(n))));
        output("D. ", n);
        output("E. ", computeSomething(80, 100));
    }

    /** Displays an integer by calling another method, */
    private static String convertInt(int i) {
        return "Result = " + i;
    }

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

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

    /**  */
    private static int inc(int n) {
        return n+1;
    }

    /** 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 =  y - x;
        } else {
            result = 0;
        }
        return result;
    }
}

Related Tags for Java: Method Exercises 5: