Below is the Java program that computes the largest and smallest of three integer values
Java
// 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 housekeeping() method
// Declare and initialize variables here.
int num1 = -50; // First of the three values.
int num2 = 53; // Second of the three values.
int num3 = 78; // Third of the three values.
int largest = num1; // Initialize largest with the first value.
int smallest = num1; // Initialize smallest with the first value.
// This is the work done in the detailloop() method
// Write assignment if, or if-else statements here as appropriate.
if (num2 > largest) {
largest = num2;
} else if (num2 < smallest) {
smallest = num2;
}
if (num3 > largest) {
largest = num3;
} else if (num3 < smallest) {
smallest = num3;
}
// 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);
}
}
So, the above program initializes three integer variables with the values -50, 53, and 78, and then uses if-else statements to update the largest and smallest variables rightly.
Finally, it prints out the results as required. When you run this program, you should get the output:
csharp
The largest value is 78
The smallest value is -50
See full text below
Understanding if-else Statements
Summary
In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78.
Instructions
Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate.
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. The output statement is written for you.
Execute the program. Your 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 housekeeping()method
//Declare and initialize variables here.
Int largest= 78; // Largest of the three values.
Int smallest= -50; //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 endOf Job()method
//Output largest and smallest number.
System.out.println(“The Largest value is “ + largest);
System.out.println(The smallest value is” + smallest);
}
}