Understanding if-else Statements Summary In this lab, you will complete a prewri
ID: 3794982 • Letter: U
Question
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);
}
}
Explanation / Answer
Hi,
Please see below the java class . Please comment for any queries/feedbacks.
Thanks!
LargeSmall.java
// LargeSmall.java - This program calculates the largest and smallest of three integer values.
public class LargeSmall
{
private static int largestVal; // Largest of the three values.
private static int smallestVal; //Smallest of the three values.
public static void main(String args[])
{
int [] valueArray = {-50,53,78};
//This is the work done in housekeeping()method
//Declare and initialize variables here.
housekeeping(valueArray) ; //calling housekeeping() to set the initial values
int largest = largestVal; // Largest of the three values.
int smallest= smallestVal; //Smallest of the three values.
//This is the work done in the detailloop()method
//Write assignment if, or if else statements here as appropriate.
detailloop(valueArray); // calling detail loop to find the smallest and largest
//Assigning the values after finding out the actual largest and smallest value
largest = largestVal;
smallest= smallestVal;
//This is the work done in the endOf Job()method
//Output largest and smallest number.
endOfJob(largest,smallest); //calling endOfJob() to print the values
}
public static void housekeeping(int [] valueArray){
//Setting first element of the array as the largest and smallest values
largestVal = valueArray[0];
smallestVal = valueArray[0];
}
public static void detailloop(int [] numArray){
//looping through each element to check which is the smallest
for(int i=0; i<numArray.length ; i++){
if(smallestVal > numArray [i]){
smallestVal = numArray[i]; //If current value is less than smallestVal, set current value as smallestVal
}
else{
smallestVal = smallestVal; //no change
}
}
//looping through each element to check which is the largest
for(int i=0; i<numArray.length ; i++){
if(largestVal < numArray [i]){
largestVal = numArray[i]; //If current value is greater than largestVal, set current value as largestVal
}
else{
largestVal = largestVal; //no change
}
}
}
public static void endOfJob(int largest, int smallest){
System.out.println("The Largest value is " + largest);
System.out.println("The smallest value is " + smallest);
}
}
Sample output:
The Largest value is 78
The smallest value is -50
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.