Try the fastest way to create flashcards

Related questions with answers

The program given below generates five random subtraction questions. Revise the program to generate ten random addition questions for two integers between 1 and 15. Display the correct count and test time.

1   import java.util.Scanner;
2
3   public class SubtractionQuizLoop {
4       public static void main(String[] args) {
5           final int NUMBER_OF_QUESTIONS = 5; // Number of questions
6           int correctCount = 0; // Count the number of correct answers
7           int count = 0; // Count the number of questions
8           long startTime = System.currentTimeMillis();
9           String output = " "; // output string is initially empty
10          Scanner input = new Scanner(System.in);
11
12          while (count < NUMBER_OF_QUESTIONS) {
13              // 1. Generate two random single-digit integers
14              int number1 = (int)(Math.random() * 10);
15              int number2 = (int)(Math.random() * 10);
16
17              // 2. If number1 < number2, swap number1 with number2
18              if (number1 < number2) {
19                  int temp = number1;
20                  number1 = number2;
21                  number2 = temp;
22              }
23
24              // 3. Prompt the student to answer "What is number1 – number2?"
25              System.out.print(
26              "What is " + number1 + " - " + number2 + "? ");
27              int answer = input.nextInt();
28
29              // 4. Grade the answer and display the result
30              if (number1 - number2 == answer) {
31                  System.out.println("You are correct!");
32                  correctCount++; // Increase the correct answer count
33              }
34              else
35                  System.out.println("Your answer is wrong.\n" + number1
36                  + " - " + number2 + " should be " + (number1 - number2));
37
38              // Increase the question count
39              count++;
40
41              output += "\n" + number1 + "-" + number2 + "=" + answer +
42              ((number1 - number2 == answer) ? " correct" : " wrong");
43          }
44
45          long endTime = System.currentTimeMillis();
46          long testTime = endTime - startTime;
47
48          System.out.println("Correct count is " + correctCount +
49          "\nTest time is " + testTime / 1000 + " seconds\n" + output);
50      }
51  }
Question

Write a method with the following header to display three numbers in increasing order:

public static void displaySortedNumbers(
   double num1, double num2, double num3)

Write a test program that prompts the user to enter three numbers and invokes the method to display them in increasing order.

Solution

Verified
Answered 2 years ago
Answered 2 years ago
Step 1
1 of 3

To display numbers in a sorted order, we first add them to an array. We then call Arrays.sort on that array. Then we print all numbers to the console.

public static void displaySortedNumbers(
    double num1, double num2, double num3){
  // Add numbers to array
  double[] numbers = {num1, num2, num3};
  // Call Arrays.sort
  Arrays.sort(numbers);
  // Print result
  for (double num : numbers) {
    System.out.println(num);
  }
}

Create a free account to view solutions

Create a free account to view solutions

Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface 5th Edition by David A. Patterson, John L. Hennessy

Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionISBN: 9780124077263David A. Patterson, John L. Hennessy
226 solutions
Intro to Java Programming, Comprehensive Version 10th Edition by Y. Daniel Liang

Intro to Java Programming, Comprehensive Version

10th EditionISBN: 9780133761313Y. Daniel Liang
1,628 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
948 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
872 solutions

More related questions

1/4

1/7