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

Ask Questions?

View Latest Questions


 
 

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

Java: Method Exercises 2

Name: _________________________________

What is the output from this program?

  1. ___________________________
  2. ___________________________
  3. ___________________________
  4. ___________________________
  5. ___________________________
  6. ___________________________
  7. ___________________________
  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 
// File  : methods/exercises/MethodExercises2.java
// Purpse: What is the output?
// Author: Fred Swartz - 2004 September
// Remember:
// * If a method is overloaded (more than one method by the same name),
//   choose the one with the same number and types of parameters.
// * Actual parameters (in the call) are evaluated before the method
//   is called.  This includes making calls within the parameter list.

import javax.swing.*;

class MethodExercises2 {

    //=================================================================== main
    public static void main(String[] args) {
        int n;

        output("Greetings");
        output(32);
        output("Unix era started in ", 1970);
        n = add(3, 2);
        output("The sum is ", n);
        output(something(8, 4));
        output("Ulam(20) = ", ulam(20));
        output("Ulam(5) = " , ulam(5));
    }

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

    //================================================================== output
    /** Displays a String, Overloaded. */
    static void output(String mess) {
        JOptionPane.showMessageDialog(null, mess);
    }

    //================================================================== output
    /** Displays a message and an integer. Overloaded. */
    static void output(String message, int i) {
        output(message + i);  // Calls one of the other output methods.
    }

    //==================================================================== add
    /** Returns the result of adding the first parameter to second. */
    static int add(int a, int b) {
        return a + b;
    }

    //============================================================== something
    /** Returns something. */
    static int something(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

    //=================================================================== ulam
    /** Computes next value in Ulam sequence. */
    static int ulam(int x) {
        int result;
        if (x%2 == 0) {
            result = x / 2;   // If number is even, divide it by 2
        } else  {
            result = 3*x + 1; // Odd numbers processed here.
        }
        return result;
    }
}
Copyleft 2005 Fred Swartz MIT License

Related Tags for Java: Method Exercises 2: