Question

Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...

Write a function (java static method) that computes the maximum of two integer.


maxOfTwo(1, 2) → 2
maxOfTwo(2, 1) → 2
maxOfTwo(1, 1) → 1

**(to start use):

public int maxOfTwo(int a, int b) {

And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters.


maxOfThree(1, 2, 3) → 3
maxOfThree(0, 2, 1) → 2
maxOfThree(88, 4, 5) → 88

**(to start use):

public int maxOfThree(int a, int b, int c) {

Homework Answers

Answer #1
public class Max {

    public int maxOfTwo(int a, int b) {
        if (a > b)
            return a;
        else
            return b;
    }

    public int maxOfThree(int a, int b, int c) {
        return maxOfTwo(maxOfTwo(a, b), c);
    }

    public static void main(String[] args) {
        System.out.println(new Max().maxOfTwo(1, 2));
        System.out.println(new Max().maxOfTwo(2, 1));
        System.out.println(new Max().maxOfTwo(1, 1));

        System.out.println(new Max().maxOfThree(1, 2, 3));
        System.out.println(new Max().maxOfThree(0, 2, 1));
        System.out.println(new Max().maxOfThree(88, 4, 5));
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to...
Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to reverse the order of all the items in q.
Write a Java swap method that could swap values of two integer variables. Do not use...
Write a Java swap method that could swap values of two integer variables. Do not use any predefined method. Also write a method call that swaps the values of the following two variables. int first = 7, second = 5;
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows: public static int countNumAlphabetic ( String str ) { } Sample Output: countNumAlphabetic("mA13Th9zB86") returns 8
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...