Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two methods. The method largestNumber() should return the largest number of the three input values. The method smallestNumber() should return the smallest number of the three input values. public static int largestNumber(int num1, int num2, int num3) public static int smallestNumber(int num1, int num2, int num3)

Respuesta :

Answer:

The java program is as follows.

import java.util.Scanner;

import java.lang.*;

public class Numbers

{

//variables to hold three integers

static int num1, num2, num3;

//method to return largest of all three numbers

public static int largestNumber(int num1, int num2, int num3)

{

if(num1>num2 && num1>num3)

return num1;

else if(num2>num1 && num2>num3)

return num2;

else  

return num3;

}

//method to return smallest of all three numbers

public static int smallestNumber(int num1, int num2, int num3)

{

if(num1<num2 && num1<num3)

return num1;

else if(num2<num1 && num2<num3)

return num2;

else  

return num3;  

}

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter three numbers: ");

num1= sc.nextInt();

num2= sc.nextInt();

num3= sc.nextInt();

int largest = largestNumber(num1, num2, num3);

int smallest = smallestNumber(num1, num2, num3);

System.out.println("Largest number is "+ largest);

System.out.println("Smallest number is "+ smallest);

}

}

OUTPUT

Enter three numbers: 12 13 15

Largest number is 15

Smallest number is 12

Explanation:

1. Three integer variables are declared to hold the three user inputted numbers.  

2. The variables are declared static and declared at the class level.  

3. The method, largestNumber(), takes three numbers as parameters and returns the largest of all the three numbers. Hence, the return type of the method is int and declared as static.  

public static int largestNumber(int num1, int num2, int num3)  

4. The largest number is found using multiple if-else statements.  

5. The method, smallestNumber(), takes three numbers as parameters and returns the smallest of all the three numbers. Hence, the return type of the method is int and declared as static.  

public static int smallestNumber(int num1, int num2, int num3)  

6. The smallest number is found using multiple if-else statements.  

7. Inside main(), an object of Scanner class is created to enable user input.  

Scanner sc = new Scanner(System.in);  

8. The user is prompted to enter three numbers.  

9. The three numbers are passed as parameters to both the methods, largestNumber() and smallestNumber().  

10. Two integer variables, largest and smallest are declared. These variables hold the values returned from largestNumber() and smallestNumber() methods, respectively and displayed to the user.

The program illustrates the use of methods or functions.

Methods and functions are collection of named code blocks, that are executed when called.

The program written in Java, where comments are used to explain each line is as follows:

import java.util.*;

public class Main{

   //This defines the largestNumber method

   public static int largestNumber(int num1, int num2, int num3){

       //This initializes the largest number to num3

       int large = num3;

       //This checks if num1 is the largest

       if(num1>num2 && num1>num3){

           large = num1;

       }

       //This checks if num2 is the largest

       else if(num2>num1 && num2>num3){

           large = num2;

       }

       //This returns the largest

       return large;

   }

   //This defines the smallestNumber method

   public static int smallestNumber(int num1, int num2, int num3){

       //This initializes the smallest number to num3

       int small = num3;

       //This checks if num1 is the smallest

       if(num1<num2 && num1<num3){

           small = num1;

       }

       //This checks if num2 is the smallest

       else if(num2<num1 && num2<num3){

           small = num2;

       }

       //This returns the smallest

       return small;  

   }

   //The main method begins here

   public static void main(String[] args){

       //This creates a scanner object

       Scanner input = new Scanner(System.in);

       //This prompts the user for input

       System.out.print("Enter three numbers: ");

       //The next three lines get the inputs for the three numbers

       int num1= input.nextInt();

       int num2= input.nextInt();

       int num3= input.nextInt();

       //This prints the largest number

       System.out.println("Largest number is "+ largestNumber(num1, num2, num3));

       //This prints the smallest number

       System.out.println("Smallest number is "+ smallestNumber(num1, num2, num3));

   }

}

Read more about program methods at:

https://brainly.com/question/21579839