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)
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers)...
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers) with variable number of arguments. The method should be called with different numbers of parameters and does arrange the numbers in descending order. Call the method within main method of the driver classand display the results.
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);...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. you have to ask the user for the size of the array and ask the user to input the values in the main. The function has the following prototype. int maximum ( int *p [ ], int n);
Task 6: Create a function in Java that counts the number of even digits in a...
Task 6: Create a function in Java that counts the number of even digits in a positive integer. public static int countEvenDigits(int a); // Ex, countEvenDigits(1234) => 2 Task 7: Create a generic function that returns digits in a given range. The ones place corresponds to power 0, the tens place corresponds to power 1, the hundreds place corresponds to power 2, and so on. Think 10^3 => 1000 => thousands place. (Do not use exponents in your code.) //...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n);