For this lab you will write a program with a main method and two other methods.
ID: 3760633 • Letter: F
Question
For this lab you will write a program with a main method and two other methods. The main method handles all the user input and all the println statements. It asks the user to enter 3 numbers. The main method should call the two other methods and pass the three numbers to both of them. The main method should print out all the results.
The first other method is called calcLargestSum and it returns the sum of the two largest of the three numbers.
The second method is called calcMiddleNumber and it returns the middle number. (The one that is neither the smallest nor the largest.) You can assume that none of the numbers are the same.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int calcLargestSum(int[] numbers)
{
int sum=0;
for(int i=0;i<3;i++)
sum = numbers[i]+sum;
int max = 0;
int temp = sum;
for(int i=0;i<3;i++)
{
temp = sum;
temp = temp-numbers[i];
if(max<temp)
max = temp;
}
return max;
}
public static int calcMiddleNumber(int[] numbers)
{
if(numbers[0]>numbers[1])
{
if(numbers[0]>numbers[2])
{
if(numbers[2]>numbers[1])
return numbers[2];
else
return numbers[1];
}
else
return numbers[2];
}
else
{
if(numbers[1]>numbers[2])
{
if(numbers[2]>numbers[0])
return numbers[2];
else
return numbers[0];
}
else
return numbers[0];
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner input = new Scanner(System.in);
int[] number = new int[3];
System.out.println("Input 3 numbers : ");
for(int i=0;i<3;i++)
{
System.out.println("Input number " + (i+1));
number[i] = input.nextInt();
}
System.out.println("Largest sum is : "+calcLargestSum(number));
System.out.println("Middle number is : "+calcMiddleNumber(number));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.