In: Computer Science
b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java.
c) Write separate methods to perform arithmetic operations of sum, average, product, max and min of three integer numbers. These 5 methods should have three integer parameters to take the three integer values. They should be valuereturn methods, and return the arithmetic result in proper data type when they are invoked (called). The average method should return a double value, and all the other methods should return integer values.
d) In the main method, prompt and accept user inputs for three integer numbers, and invoke (call) the five methods for output display.
e) Program top comments, indentation and programming styles are all required.
ArithmeticMethods.java :
import java.util.Scanner; // Scanner class is used to get user input.
public class ArithmeticMethods
{
/*
Method to get sum of three number
*/
static int sum(int n1, int n2, int n3) {
int sum = n1 + n2 + n3; // calculating sum of three numbers.
return sum; // returning sum of three numbers.
}
/*
Method to get average of three numbers.
*/
static double average(int n1, int n2, int n3) {
/*
formula to calculate average = (total sum / total number)
*/
int sum = n1 + n2 + n3; // calculating sum of three numbers.
double average = (double)sum / 3; // calculating average of three numbers.
return average; // returning average of three numbers.
}
/*
Method to get product of three numbers.
*/
static int product(int n1, int n2, int n3) {
int product = n1 * n2 * n3; // calculating product of three numbers.
return product; // returning product of three numbers.
}
/*
Method to get minimum value among three numbers.
*/
static int max(int n1, int n2, int n3) {
/*
Check if n1 is greater than or equals to remaining two values
if yes then n1 is greater than n1 is max among remaining values (return n1)
*/
if(n1 >= n2 && n1 >= n3) {
return n1;
}
/*
Check if n2 is greater than or equals to remaining two values
if yes then n2 is max among remaining values (return n2)
*/
else if (n2 >= n1 && n2 >= n3) {
return n2;
}
/*
if above two conditions fails than n3 will be max among remaining values. (return n3)
*/
else {
return n3;
}
}
/*
Method to get min value among three values.
*/
static int min(int n1, int n2, int n3) {
/*
Check if n1 is less than or equals to remaining two values
if yes then n1 is min among remaining values (return n3)
*/
if(n1 <= n2 && n1 <= n3) {
return n1;
}
/*
Check if n2 is less than or equals to remaining two values
if yes then n2 is min among remaining values (return n2)
*/
else if (n2 <= n1 && n2 <= n3) {
return n2;
}
/*
if above two conditions fails than n3 will be min among remaining values (return n3)
*/
else {
return n3;
}
}
public static void main(String[] args) {
int n1,n2,n3; // Integer Variables to hold three Integer user input
Scanner sc = new Scanner(System.in); //Scanner Object to get user input.
// Prompt for enter First Number and store entered Number in n1 Variable
// sc.nextInt() Method is used to get Integer input.
System.out.println("Enter First Number: ");
n1 = sc.nextInt();
// Prompt for enter Second Number and store entered Number in n1 Variable
// sc.nextInt() Method is used to get Integer input.
System.out.println("Enter Second Number: ");
n2 = sc.nextInt();
// Prompt for enter Third Number and store entered Number in n1 Variable
// sc.nextInt() Method is used to get Integer input.
System.out.println("Enter Third Number: ");
n3 = sc.nextInt();
/*
Call five Method that we created one by one by passing three user input as parameter
all five Method we created will return some values (depending upon input)
print returned values on console using println.
*/
System.out.println("Sum of three number is: "+sum(n1,n2,n3));
System.out.println("Average of three number is: "+average(n1,n2,n3));
System.out.println("Product of three number is: "+product(n1,n2,n3));
System.out.println("Max of three number is: "+max(n1,n2,n3));
System.out.println("Min of three number is: "+min(n1,n2,n3));
}
}
Sample Output 1 :
Sample Output 2 :
Please refer to the screenshot of the code below to understand indentation of the code :