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.
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...
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...
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
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...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Write a java program that prompt the user to insert last term’s courses and their individual...
Write a java program that prompt the user to insert last term’s courses and their individual mark and store them in an appropriate multidimensional array.
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for...
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for the check amount, then ask the user what type of tipp they would like to leave: the choices are 1 for good tip (20%), 2 for an average tip (15%), or 3 for poor tip (10%). Use their input and an if-else to calculate the tip amount. Then calculate and output the final bill: check+tip print out the bill, exactly as shown (print the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT