In: Computer Science
JAVA
Program 2: In Order
Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order.
Be sure to do the following:
Be sure to include with your program as documentation:
NOTE 1:
NOTE 2:
1. Declare all variables within the data declaration section of
each class and method. (-5)
2 Do not get input on the same line as a variable
declaration. (-5)
3. Do not place an equation for computation on the same line as
declaring a variable. (-5)
4. Do not place an equation for computation on the
same line an input statement. (-5)
5. Do not place any
computations within an output statement.
(-5)
Test your program with the following sets of data:
Algorithms:
---------------------
Step1: Input Data into three Variables
Step2: Find Smallest of three number and put it in number1
Step3: Find Next Smallest Number and put it in number2
Step4: Display Data
==============================================
Code Is Given Below:
------------------------------------
import java.util.Scanner;
//class that will arrange three number in ascending order
public class MinToMax {
public static void main(String[] args) {
Scanner scn=new
Scanner(System.in);//creating scanner object to get user
input
//declare variable of int
type
int number1;
int number2;
int number3;
int min;
//taking input from user
System.out.print("First number:
");
number1=scn.nextInt();
System.out.print("Second number:
");
number2=scn.nextInt();
System.out.print("Third number:
");
number3=scn.nextInt();
//finding smallest of three numbers
and placed it in number 1 variable
if (number1 <= number2
&& number1 <= number3) {
min=number1;
}
else if (number2 <= number1 && number2
<= number3) {
min=number2;
number2=number1;
number1=min;
}
else {
min=number3;
number3=number1;
number1=min;
}
//find next smallest number and
place it in number2 variable
if(number2<number3) {
}
else if(number3<number2) {
min=number3;
number3=number2;
number2=min;
}
//printing result
System.out.println("Asecnding order
is : "+number1+","+number2+","+number3);
}
}
Output:
-----------------
Code Snapshot:
=======================