In: Computer Science
1. in the code below, 2 variables (largest and smallest) are declared. use these variables to store the largest and smallest of three integer values. you must decide what other variables you will need and initialize them if appropriate.
2. write the rest of the program using assignment statements, if statements, or if else statements as appropriate. There are comments in the code that tell you where you should write your statements
3. compile and execute. Output should be:
The largest value is 78
The smallest value is -50
// LargeSmall.java - This program calculates the largest and smallest of three integer values.
public class LargeSmall
{
public static void main(String args[])
{
// This is the work done in the
housekeeping() method
// Declare and initialize variables
here.
int largest;
// Largest of the three values.
int smallest;
// Smallest of the three values.
// This is the work done in the
detailLoop() method
//Write assignment, if, or if else
statements here as appropriate.
// This is the work done in the
endOfJob() method
// Output largest and smallest
number.
System.out.println("The largest
value is " + largest);
System.out.println("The smallest
value is " + smallest);
}
}
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class LargeSmall
{
public static void main(String args[])
{
// This is the work done in the housekeeping() method
// Declare and initialize variables here.
int largest; // Largest of the three values.
int smallest; // Smallest of the three values.
// This is the work done in the detailLoop() method
//Write assignment, if, or if else statements here as appropriate.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter three integers: ");
int x = keyboard.nextInt();
int y = keyboard.nextInt();
int z = keyboard.nextInt();
if(x > y){
largest = x;
smallest = y;
}else{
largest = y;
smallest = x;
}
if(largest < z)
largest = z;
else if(smallest > z)
smallest = z;
keyboard.close();
// This is the work done in the endOfJob() method
// Output largest and smallest number.
System.out.println("The largest value is " + largest);
System.out.println("The smallest value is " + smallest);
}
}
/*
Sample run:
Enter three integers:
4 5 1
The largest value is 5
The smallest value is 1
*/