Question

In: Computer Science

JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...

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.

  • First, the program will prompt the user for each of the three numbers.
  • Next, find the smallest value of the three.
  • Then decide which of the other two is the next smallest.
  • Then have the program display the three numbers in ascending order.

Be sure to do the following:

  1. Determine what the input will be, it's data type, and assign a variable for each value.
  2. Determine what the output will be. Do you need new variables for the output or can you use the input again? There is no right or wrong answer here. It’s also possible you may have to develop your algorithms before you can decide whether you will use new variables for your output or just use the input variables. You can always change your mind if you find you need to.
  3. Do you need any formulas?
  4. Create an initial algorithm of the basic steps.
  5. Develop the refined algorithm from the initial algorithm.

Be sure to include with your program as documentation:

  1. The Program Prologue
  2. The Input data
  3. The Output data
  4. Any Formulas you may have.
  5. The Initial Algorithm
  6. The Refined Algorithm

NOTE 1:

  • DO NOT use the "return 0" code, end, break, or exit to force an exit from within your IF/ELSE structure.  Declare all variables within the data declaration section.
    • Let the program progress to the end of the main function.
    • The end of the main function is the only place that the “return 0” should be placed.
    • A SWITCH statement is the only place that the break command should be used.
  • DO NOT use the "continue" statement.

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:

  1. First number: 3
    Second number: 2
    Third number: 1
  2. First number: 3
    Second number: 1
    Third number: 2
  3. First number: 1
    Second number: 2
    Third number: 3
  4. First number: 1
    Second number: 3
    Third number: 2
  5. First number: 2
    Second number: 1
    Third number: 3
  6. First number: 2
    Second number: 3
    Third number: 1

Solutions

Expert Solution

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:

=======================


Related Solutions

Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter a six-digit integer. b) Take the integer and break it up into two pieces of three-digits each (make sure you keep the order of digits). c) Display each 3-digit piece on a separate line with a proper message before each piece. For example, if the user enters  450835 as the integer, then the program should display the following output: Right 3-digit piece: 835...
Write by hand a full java program that will prompt the user for the length and...
Write by hand a full java program that will prompt the user for the length and width of a square, calculate and display the area of that square rounded to the nearest tenth with an appropriate message.
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Answer in JAVA Write a program that would prompt the user to enter an integer. The...
Answer in JAVA Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT