211k views
3 votes
Write a Java application that reads three integers from the user using a Scanner. Then, create separate functions to calculate the sum, product and average of the numbers, and displays them from main. (Use each functions' 'return' to return their respective values.) Use the following sample code as a guide.

User BeWarned
by
5.8k points

1 Answer

7 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");

int n1 = input.nextInt();

System.out.print("Enter a number: ");

int n2 = input.nextInt();

System.out.print("Enter a number: ");

int n3 = input.nextInt();

System.out.println("The sum is: " + calculateSum(n1, n2, n3));

System.out.println("The product is: " + calculateProduct(n1, n2, n3));

System.out.println("The average is: " + calculateAverage(n1, n2, n3));

}

public static int calculateSum(int n1, int n2, int n3){

return n1 + n2 + n3;

}

public static int calculateProduct(int n1, int n2, int n3){

return n1 * n2 * n3;

}

public static double calculateAverage(int n1, int n2, int n3){

return (n1 + n2 + n3) / 3.0;

}

}

Step-by-step explanation:

In the main:

Ask the user to enter the numbers using Scanner

Call the functions with these three numbers and display results

In the calculateSum function, calculate and return the sum of the numbers

In the calculateProduct function, calculate and return the product of the numbers

In the calculateAverage function, calculate and return the average of the numbers

User Josip Ivic
by
6.4k points