JAVA Write a program with a main method and two other methods. 1.Main Method: -U
ID: 3814705 • Letter: J
Question
JAVA
Write a program with a main method and two other methods.
1.Main Method:
-User enters three numbers
-Print out all results
2.calcSum Method (first, second, third): ¥
-Get three numbers as parameters ¥
-if first is less than second, first = first +second
-If second is less than third, third = third – second
- Return the sum of these three numbers
3. calcAvg Method (first, second, third): ¥
-Get three numbers as an input
-Return the average of these three numbers as double value
Output:
Enter first number:3
Enter second number:5
Enter third number:7
The summation of all numbers: 15
The average of all numbers: 5.0
Explanation / Answer
import java.util.Scanner;
public class JavaTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter 3 Numbers: ");
int first, second, third , sum;//variables to store the numbers and sum
double avg;//variable to store the avg
Scanner scan = new Scanner(System.in);
first = scan.nextInt();//read first number from the user
second = scan.nextInt();//read second number from the user
third = scan.nextInt();//read third number from the user
sum = calcSum(first, second, third);// call the method calcSum and return the sum of three number
avg = calcAvg(first, second, third);// call the method calcAvg and return the average of three number
System.out.println("The summation of all numbers: "+sum);// print the sum
System.out.println("The average of all numbers: "+avg);//print the average
}
//method to calculate the sum of three numbers and return the sum
private static int calcSum(int first, int second, int third) {
if(first<second){
first = first +second;
}
if(second<third){
third = third - second;
}
int sum =first + second + third;
return sum;
}
//method to calculate the average of three numbers and return the average
private static double calcAvg(int first, int second, int third) {
double avg =(first+second+third)/3;
return avg;
}
}
-----------------output-------------
Enter 3 Numbers:
3
7
5
The summation of all numbers: 15
The average of all numbers: 5.0
----------------output------------------
Note; Feel free to ask question in case of doubts. God bless you!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.