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)
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)); } }
Get Answers For Free
Most questions answered within 1 hours.