Question

Write a Java program to determine the larger of 2 numbers and the largest of 3...

Write a Java program to determine the larger of 2 numbers and the largest of 3 numbers. Each section must use its own method. Method overloading must be used for this exercise.

[10]

Method for 2 numbers public static double max(double p, double q) Method for 3 numbers public static double max(double p, double q, double r)

Homework Answers

Answer #1
public class Max {

    // 1
    public static double max(double p, double q) {
        if (p > q) {
            return p;
        } else {
            return q;
        }
    }

    // 2
    public static double max(double p, double q, double r) {
        return max(p, max(q, r));
    }

    public static void main(String[] args) {
        System.out.println(max(4, 9));
        System.out.println(max(7, 3));
        System.out.println(max(2, 7, 9));
        System.out.println(max(9, 2, 7));
        System.out.println(max(6, 7, 2));
    }
}
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
Write a java 3x3 matrix program that does the following: 1) multiply 2)add 3)subtract 4)scalar product...
Write a java 3x3 matrix program that does the following: 1) multiply 2)add 3)subtract 4)scalar product 5)does calculation for a^3 - b^3 6)does calculation for 2a^ + 3b^2 an example for the adding method will look something like this public static double[][] multiplyMatrix(double [][] a, double [][] b)
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 a java program to display a given range of integer numbers (0 to 9) in...
Write a java program to display a given range of integer numbers (0 to 9) in the following order: Example : given (5 to 9), the program displays 56789 67895 78956 89567 95678 The first line displays the min to max range. Each line that follows should display the next higher number except when it hits the maximum number. In that situation, the line wraps back to the minimum sequence. For this problem write a static method to do the...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose input is the number of steps, and whose output is the miles walked. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); Ex: If the input is: 5345 the output is: 2.67 Your program must define and call a method: public static double stepsToMiles(int userSteps)
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!
Write a Java program that gets a 4x4 array of numbers and that calculates the sum...
Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner. Examples of resulting programs are: Enter a 4x4 matrix row by row: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 The sum of numbers in the diagonal is: 16 The average of numbers in...
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 Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write a program that takes two numbers from the Java console representing, respectively, an investment and...
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT