In: Computer Science
TASK:
Based upon the following code:
import java.util.Scanner; // Import the Scanner class public class Main { public static void main( String[] args ) { Scanner myInput = new Scanner(System.in); // Create a Scanner object System.out.println("Enter (3) digits: "); int W = myInput.nextInt(); int X = myInput.nextInt(); int Y = myInput.nextInt();
} } |
Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and output below:
import java.util.Scanner; // Import the Scanner class public class Main { public static void main( String[] args ) { // Create a Scanner object Scanner myInput = new Scanner(System.in); // Accept the user's input System.out.println("Enter (3) digits: "); int W = myInput.nextInt(); int X = myInput.nextInt(); int Y = myInput.nextInt(); // <TODO> - Sort the integers int first = 0; int second = 0; int third = 0; // Report the sorted numbers System.out.println( "The sorted values are:" ); System.out.println( first + " " + second + " " + third); } } |
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
import java.util.Scanner; // Import the Scanner class
public class Main {
public static void main( String[] args ) {
// Create a Scanner object
Scanner myInput = new Scanner(System.in);
// Accept the user's input
System.out.println("Enter (3) digits: ");
int W = myInput.nextInt();
int X = myInput.nextInt();
int Y = myInput.nextInt();
int first = 0;
int second = 0;
int third = 0;
if(W>X && W>Y) //condition for w is greather.
{
if(X>Y)
{
first=Y;
second=X;
third=W;
}
else
{
first=X;
second=Y;
third=W;
}
}
else if(X>W && X>Y) //condition for x is
greather.
{
if(W>Y)
{
first=Y;
second=W;
third=X;
}
else
{
first=W;
second=Y;
third=X;
}
}
else
{
if(W>X)
{
first=X;
second=W;
third=Y;
}
else
{
first=W;
second=X;
third=Y;
}
}
// Report the sorted numbers
System.out.println( "The sorted values are:" );
System.out.println( first + " " + second + " " + third);
}
}
OUTPUT:
SCREENSHOT OF THE CODE: