Menu

Write Java Program Write Java Application Reads Three Integers User Displays Sum Product A Q43866085

Write a Java program:

Write a Java application that reads three integers from the userand displays the sum, product, average, smallest, largest, andmedian of the numbers. Use the following sample code as aguide.

import java.util.Scanner;

public class IntegerOps {

public staticvoid main(String[] args)

{

int num1, num2, num3;

int sum,;

// Use Scanner class to read the three integer

sum = sum(num1, num2, num3); //The sum methodcalculates the sum

//Call product method to calculate the product

//Call product method to calculate the average (real number)

//Call product method to calculate the smallest number

//Call product method to calculate the largest number

//Call product method to calculate the median of the 3numbers

System.out.println(“Sum = ” +sum);

// Print the product, average, smallest, largest, and median inseparate lines

}

/**

* This method calculates the sum of 3 integers

* @param x the first integer

* @param y the second integer

* @param z the third integer

* @return the sum of the 3 integers

*/

public staticint sum(int x,int y, int z)

{

return x + y + z;

}

}

Below is an example of how to calculate the median of 3integers.

public class Test {

public staticvoid main(String[] args) {

int x= 6;

int y = 4;

int z = 8;

int larger1, smaller1, larger2, smaller2,median;

smaller1 = x; //assuming the smaller of x & y is x

larger1 = y; //assuming the larger of x & y is y

smaller2 = x; //assuming the smaller of x & z is x

larger2 = z; //assuming the larger of x and z is z

if(y < x) //fix the above assumptions of thex & y if needed

{

smaller1 = y;

larger1 = x;

}

if(z < x) //fix the above assumptions of thex & z if needed

{

smaller2 = z;

larger2 = x;

}

median = smaller2; //assume the median is smaller2. We will fixit later if needed

if (smaller1 == smaller2) //if the smaller ofthe two pairs is the same, then the median is one of thefollowing

{

if(larger1 < larger2) //the median cannot bethe largest number, so it is the smaller of the two largest

median = larger1;

else

median = larger2;

}

else if (smaller1 >smaller2) //if the smaller of the two pairs are not equal, then thelarger of the two is the median

median = smaller1;

//could have added an else here, but is really the aboveassumption of median=smaller2 so it is omitted.

System.out.printf(“median=%d”,median);

}

}

Expert Answer


Answer to Write a Java program: Write a Java application that reads three integers from the user and displays the sum, product, av…

OR