Question

In: Computer Science

Question 3 - If Statements - Intermediate Put your ducks in a row. If ye dare....

Question 3 - If Statements - Intermediate

Put your ducks in a row. If ye dare.

Boolean comparisons provide the basis for that entire (critically important) 'sorting' category of operations (specifically, comparison sorting). Comparison sorts are those methods of organizing data in which one element is compared against another, and the relative position of one or both elements is determined by the comparison result.

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);

  }

}

<TODO - Program output>

Solutions

Expert Solution

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();

    // Sort the integers

    int first = 0;

    int second = 0;

    int third = 0;
  
    if(W<X){
        if(W<Y){
            first = W;
            if(X<Y){    // W<X<Y
                second = X;
                third = Y;
            }
            else{       // W<Y<=X
                second = Y;
                third = X;
            }
        }
        else{           // Y<=W<X
            first = Y;
            second = W;
            third = X;
        }
    }
    else if(X<Y){
        first = X;
        if(W<Y){        // X<=W<Y
            second = W;
            third = Y;
        }
        else{           // X<Y<=W
            second = Y;
            third = W;
        }
    }
    else{               // Y<=X<=W
        first = Y;
        second = X;
        third = W;
    }

    // Report the sorted numbers

    System.out.println( "The sorted values are:" );

    System.out.println( first + " " + second + " " + third);

}

}

Sample output 1

Sample output 2

Sample output 3

Sample output 4

Sample output 5


Related Solutions

1. Make your own example of a 5-by-3 matrix that is in echelon row but do...
1. Make your own example of a 5-by-3 matrix that is in echelon row but do not reduce row echelon form. 2. Repeat for a 2-by-6 matrix 3. Make a 4-by-4 rref matrix whose first column contains only zeros
Question text An urban farmer is pondering whether to invest in ducks or chickens to raise...
Question text An urban farmer is pondering whether to invest in ducks or chickens to raise for eggs that she plans to sell to friends and neighbors. The materials needed to make a good henhouse and chicken run cost $560. A simple setup for ducks is slightly higher, or $620, because they require water at all times. Ducklings and chicks are about the same in price—she figures that $20 is needed to get four females of either species. A 50-pound...
Problem 3 (a) There are 3 male and 3 female students sitting in a row, and...
Problem 3 (a) There are 3 male and 3 female students sitting in a row, and they chose their seats randomly. What is the probably of the event "No two males sit together and no two females sit together"? (Hint: The gender of the seated students can only be FMFMFM or MFMFMF.) (b) Compute the probability of randomly drawing ve cards from a deck, and getting two Aces. (Hint: A deck has 52 cards, and 4 of them are Aces.)
Question 3 (1 point) Which of the following statements regarding inventory is true? Question 3 options:...
Question 3 (1 point) Which of the following statements regarding inventory is true? Question 3 options: a) Under IFRS, companies must capitalize borrowing costs, whereas ASPE allows companies to choose whether to capitalize or expense them. b) There are no differences between IFRS and ASPE. c) Under IFRS, companies must capitalize shipping costs, whereas ASPE allows companies to choose whether to capitalize or expense them. d) Under IFRS, companies must capitalize manufacturing overhead, whereas ASPE allows companies to choose whether...
Question 3 Read the following three statements and then answer the question below. In 2019, the...
Question 3 Read the following three statements and then answer the question below. In 2019, the UK lost its ‘measles-free’ status. The basic reproduction number for measles (R0) is given as between 12 and 18, in different studies. The immunisation rate in the UK for measles dipped to a low point in 2003 to 80% coverage and since then has risen to 91% in 2019. Question 5 Your friend is suffering from a fever and respiratory symptoms. She visits her...
18-3 For the case study problem, use helical gears and design the intermediate shaft. Compare your...
18-3 For the case study problem, use helical gears and design the intermediate shaft. Compare your results with the spur gear design presented in this chapter. Case Study Problem Specification: Section 1–18, p. 34, presents the background for this case study involving a speed reducer. A two-stage, compound reverted gear train such as shown in Fig. 18–1 will be designed. In this chapter, the design of the intermediate shaft and its components is presented, taking into account the other shafts...
Put your opinion in 3 paragraphs????    How has the situation changed since the beginning of...
Put your opinion in 3 paragraphs????    How has the situation changed since the beginning of 2017? (new President) “We will have two simple rules when it comes to this massive rebuilding effort, buy American and hire American,” said President Donald J. Trump. In April 2017, President Trump signed an executive order promoting American industry and protecting it from unfair competition. With that being said, President Trump is taking action to enforce laws promoting American industry and targeting companies (such...
Find the determinant of A by (repeatedly) expanding on the column or row of your choice....
Find the determinant of A by (repeatedly) expanding on the column or row of your choice. A = 1 0 0 0 0 3 0 2 -1 2 0 0 0 1 3 0 0 0 5 2 2 4 0 0 -2 0 0 0 5 0 0 0 0 0 4 6
3. Let the experiment be the toss of three dice in a row. Let X be...
3. Let the experiment be the toss of three dice in a row. Let X be the outcome of the first die. Let Y be the outcome of the 2nd die. Let Z be the outcome of the 3rd die. Let A be the event that X > Y , let B be the event that Y > Z, let C be the event that Z > X. (a) Find P(A). (b) Find P(B). (c) Find P(A ∩ B). (d)...
For each part of this question, complete the statement or statements by typing in your answers...
For each part of this question, complete the statement or statements by typing in your answers in the provided spaces: If the probabilities: that event A occurs is .55; that event B occurs is .60; and, that both A and B occur is .35, The probability (up to 4 decimal places if needed) that events A or B (or both) occur is ______ The probability (up to 4 decimal places if needed) that event B occurs given that A occurs...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT