196k views
3 votes
Write a Java program that reads two numbers from the console. Based on the example in the textbook, write five methods (min, max, sum, difference, and product) to calculate and return the appropriate results. For the difference method, use the absolute value method and the round method from the Math class to refine your results to the nearest whole positive integer.

User Dredok
by
5.0k points

2 Answers

2 votes

Answer:

Sample run :

Enter two numbers:

11.75 1.5

The minimum of 11.75 and 1.5 is 1.5

The maximum of 11.75 and 1.5 is 11.75

The sum of 11.75 and 1.5 is 13.25

The difference of 11.75 and 1.5, , rounded to the nearest whole integer, is 10

The product of 11.75 and 1.5 is 17.625

Step-by-step explanation:

import java.util.Scanner;

public class Reader {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter two numbers: ");

double num1 = scan.nextDouble();

double num2 = scan.nextDouble();

System.out.println("The minimum of " + num1 + " and " + num2 + " is "

+ min(num1, num2));

System.out.println("The maximum of " + num1 + " and " + num2 + " is "

+ max(num1, num2));

System.out.println("The sum of " + num1 + " and " + num2 + " is "

+ sum(num1, num2));

System.out.println("The difference of " + num1 + " and " + num2

+ ", , rounded to the nearest whole integer, is "

+ difference(num1, num2));

System.out.println("The product of " + num1 + " and " + num2 + " is "

+ product(num1, num2));

scan.close();

}

private static double product(double n1, double n2) {

return n1 * n2;

}

private static double sum(double n1, double n2) {

return n1 + n2;

}

// using the absolute value method and the round method from the Math class

// to refine the results to the nearest whole positive integer.

private static int difference(double n1, double n2) {

double diff;

diff = n1 - n2;

diff = Math.abs(diff);

int intDiff = (int) Math.round(diff);

return intDiff;

}

private static double max(double n1, double n2) {

if (n1 > n2) {

return n1;

} else {

return n2;

}

}

static double min(double n1, double n2) {

if (n1 < n2) {

return n1;

} else {

return n2;

}

}

}

User Mike Precup
by
5.1k points
1 vote

Answer:

import java.util.Scanner;

public class num6 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter first number");

int a = in.nextInt();

System.out.println("Enter second number");

int b = in.nextInt();

//Calling the methods inside the output statement

System.out.println("Min is "+min(a,b));

System.out.println("Max is "+ max(a,b));

System.out.println("Sum is "+sum(a,b));

System.out.println("Product is "+product(a,b));

System.out.println("Absolute difference is "+difference(a,b));

}

//Min Method

static int min(int a, int b){

if (a<b){

return a;

}

else {

return b;

}

}

//Max Method

static int max(int a, int b){

if (a>b){

return a;

}

else {

return b;

}

}

//Sum Method

static int sum(int a, int b){

return a+b;

}

//Diference Method

static int difference(int a, int b){

int diff = Math.abs(a-b);

return diff;

}

//Product Method

static int product(int a, int b){

int prod = a*b;

return prod;

}

}

Step-by-step explanation:

  • Using Java programming language
  • Use the scanner class to prompt and receive two values from the user (integers a and b)
  • Create the four methods as required (Please see the comments in the code)
  • In the difference method use Math.abs() to get the absolute value of the subtraction ensuring that you get a positive number returned
User Drew Turner
by
5.6k points