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 (40 Marks)-----finance corporate The investment advisors always said “Don’t put all your eggs in...
Question 3 -----finance corporate The investment advisors always said “Don’t put all your eggs in one basket”. Do you agree? Discuss your viewpoints within 500 words. Use examples to illustrate your arguments and justify your conclusion.
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...
Question 3. A 1-year European put option on a stock with strike price of $50 is...
Question 3. A 1-year European put option on a stock with strike price of $50 is quoted as $7; a 1-year European call option on the same stock with strike price $30 is quoted as $5. Suppose you long one put and short one call (one option is on 100 share). a) Draw the payoff diagram for your put position and call position. b) After 1-year, stock price turns out to be $45. What is your total payoff? What is...
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...
Question 3 (10 marks) For your answer to Question 3, consider two examples of decisions that...
Question 3 For your answer to Question 3, consider two examples of decisions that you have made in the past month. You may use examples from different settings such as from your academic studies, from work, from your personal life. Choose examples that allow you to apply both basic/individual and team decision making concepts covered in our course materials. Are there methods of decision making that you find are more common during the current period when our lives are affected...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT