import java.util.Scanner; public class Lab12 { public static void main(String[]
ID: 3642013 • Letter: I
Question
import java.util.Scanner;public class Lab12
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
int a = 1, b = 3, c = 5;
double x = 2.2, y = 4.4, z = 6.6, ans;
ans = average(a, b);
System.out.println(" average(a, b) = " + ans);
ans = average(a, b, c);
System.out.println(" average(a, b, c) = " + ans);
ans = average(x, y);
System.out.println(" average(x, y) = " + ans);
ans = average(x, y, z);
System.out.println(" average(x, y, z) = " + ans);
}
public static double average(int n1, int n2)
{
return (n1 + n2) / 2;
}
// Overloaded methoid Definition(s) here ...
}
Problem Description
Read through the existing code to understand what it does and what is left for you to do. Notice that it is incomplete and will not compile as is.
Your job is to add as many overloaded method definitions, for the method average, as needed so that the program compiles and makes sense.
Once you have written your overloaded method definitions:
Make sure that your programs compile and run without errors or warnings.
Run your program enough times to check all the choices for correctness.
Next, answer the following questions, modifying your program file as needed:
Do you really need the int parameter version(s) of average, and why?
Do you really need the three parameter version of average, i.e. is average(average(a, b), c) == average(a, b, c), and why?
Is average(1, 2.0, 3) legal, and if so which version is used, and why?
Explanation / Answer
import java.util.Scanner;
public class Lab12 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int a = 1, b = 3, c = 5;
double x = 2.2, y = 4.4, z = 6.6, ans;
ans = average(a, b);
System.out.println(" average(a, b) = " + ans);
ans = average(a, b, c);
System.out.println(" average(a, b, c) = " + ans);
ans = average(x, y);
System.out.println(" average(x, y) = " + ans);
ans = average(x, y, z);
System.out.println(" average(x, y, z) = " + ans);
ans = average(average(a,b), c);
System.out.println(" average(a, b, c) = " + ans);
ans = average(1, 2.0, 3);
System.out.println(" average(1, 2.0, 3) = " + ans);
}
public static double average(int n1, int n2) {
return (n1 + n2) / 2.0;
}
public static double average(int a, int b, int c) {
return ((a + b + c) / 3.0);
}
public static double average(double a, double b) {
return ((a + b) / 2.0);
}
public static double average(double a, double b, double c) {
return ((a + b + c) / 3.0);
}
}
Do you really need the int parameter version(s) of average, and why?
No you do not. When you call average() with int arguments rather than doubles, the double parameter version of average can still be called because ints can be cast to doubles.
Do you really need the three parameter version of average, i.e. is average(average(a, b), c) == average(a, b, c), and why?
Yes you do. The average of 3 different numbers is going to be different than the average of the mean of two numbers and a third number. average(average(a, b), c) is not the same as average(a, b, c)
Is average(1, 2.0, 3) legal, and if so which version is used, and why?
Yes it is legal. average(double a, double b, double c) is used because it is the only method that can accept a double argument. The other two arguments are simply cast to doubles.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.