In: Computer Science
write a java program to evaluate the highest number input by user, cannot use math class, must use overloading . Must compute four computeHighNum methods. Here is what I have.
import java.util.Scanner;
public class Overload {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number1 = input.nextInt();
int number2 = input.nextInt();
int number3 = input.nextInt();
//int maximum = maximum(number1, number2 );
System.out.printf("Highest integer is ");
// int number1 = input.nextInt();
// int number2 = input.nextInt();
// int number3= input.nextInt();
// int maximum = maximum(int number1, int number2, int
number3);
// System.out.print("Highest number is %d%n",max());
//double number6 = input.nextDouble();
//double number7 = input.nextDouble();
//double number8 = input.nextDouble();
//double int = maximum( number1, number2, number3);
//System.out.println("The highest number is "+ maximum);
}
public static int computeHighNum(int x, int y, int z){
System.out.print("Enter 3 intergers, seperated by a space, on a
single line: ");
int maximumValue = x;
if(y > maximumValue){
maximumValue = y;
}
if(z > maximumValue){
maximumValue = z;
}
return maximumValue;
}
public static int computeHighNum(int x, int y){
System.out.printf("Enter 2 integers, seperated by a space, on a
single line: \n");
int maximumValue = x;
if (y > maximumValue){
maximumValue = y;
}
return maximumValue;
}
public static double computeHighNum(double x, double y, double
z){
System.out.println("Enter 3 doubles, separated by a space, on a
single line: \n");
double maximumValue = x;
if(y > maximumValue){
maximumValue = y;
}
if(z > maximumValue){
maximumValue = z;
}
return maximumValue;
}
public static double computeHighNum(double x, double y){
System.out.print("Enter 2 doubles, separated by a space, on a
single line: \n");
double maximumValue = x;
if (y > maximumValue){
maximumValue = y;
}
return maximumValue;
}
}
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
import java.util.Scanner;
public class Overload {
public static void main(String[] args){
int number1, number2, number3, maximum;
double num1, num2, num3, max;
Scanner input = new Scanner(System.in);
System.out.println("Enter 2 integers, seperated by a space, on a single line: ");
number1 = input.nextInt();
number2 = input.nextInt();
maximum = computeHighNum(number1, number2);
System.out.println("Highest integer is " + maximum);
System.out.println("\nEnter 3 intergers, seperated by a space, on a single line: ");
number1 = input.nextInt();
number2 = input.nextInt();
number3 = input.nextInt();
maximum = computeHighNum(number1, number2, number3);
System.out.println("Highest integer is " + maximum);
System.out.println("\nEnter 2 doubles, separated by a space, on a single line: ");
num1 = input.nextDouble();
num2 = input.nextDouble();
max = computeHighNum(num1, num2 );
System.out.println("Highest double value is " + max);
System.out.println("\nEnter 3 doubles, separated by a space, on a single line: ");
num1 = input.nextDouble();
num2 = input.nextDouble();
num3 = input.nextDouble();
max = computeHighNum(num1, num2, num3);
System.out.println("Highest double value is " + max);
}
public static int computeHighNum(int x, int y){
int maximumValue = x;
if (y > maximumValue){
maximumValue = y;
}
return maximumValue;
}
public static int computeHighNum(int x, int y, int z){
int maximumValue = x;
if(y > maximumValue){
maximumValue = y;
}
if(z > maximumValue){
maximumValue = z;
}
return maximumValue;
}
public static double computeHighNum(double x, double y){
double maximumValue = x;
if (y > maximumValue){
maximumValue = y;
}
return maximumValue;
}
public static double computeHighNum(double x, double y, double z){
double maximumValue = x;
if(y > maximumValue){
maximumValue = y;
}
if(z > maximumValue){
maximumValue = z;
}
return maximumValue;
}
}
OUTPUT: