Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write your code in the file TwoSmallest.java. We wish to write a program that takes a set of numbers and determines which are the two

Write your code in the file TwoSmallest.java.

We wish to write a program that takes a set of numbers and determines which are the twosmallest.

Ask the user for the following information, in this order:

1. A terminating value (real number). The user will enter this value again later, to indicate that he or she is finished providing input.

2. A sequence of real numbers. Keep asking for numbers until the terminating value is entered.

Compute and output the smallest and second-smallest real number, in that order. It is possible for the smallest and second-smallest numbers to be the same (only if the sequence contains duplicate numbers).

In ALL cases of error on input, RE-ASK the user for the input until it is correctly entered.

Example:

Java TwoSmallest

 123 [this is the terminating value, not part of the set of numbers] 17 23.5 10 15.2 30 8 16 123 [this is the terminating value again, indicating that the user is done] RESULT: 8.0 RESULT: 10.0  

Above is the prompt for the program I need to write in Java. We have not yet learned arrays and we are not supposed to be using methods other than the main method. Here is my code so far and I know I am on the right track but when I do test cases I realizied that if my terminating value is lower than the actual smallest and second smallest values, my program prints out the terminating value and the (supposed to be) smallest value as my second smallest value. If someone could help me and just show me what my code lacks to account for that error and write the psuedo-code next to it I would really appreciate it! Thank you!

Here is my code thus far:

public class TwoSmallest{

public static void main(String[] args) {

double num; //numbers that are being compared as the smallest and second smallest

double smallest; //smallest #

double secondSmall; //second smallest #

System.out.println("Enter the terminating value:");

int term = IO.readInt(); //the value that once entered again, outputs the result (two smallest #s)

System.out.println("Enter a number:");

num = IO.readDouble(); //user enters a #

while(num == term){ //first comparable number cant be the same as the terminating value

System.out.println("Enter a different value:");

num = IO.readInt();

}

int count = 0; //counts how many numbers the user inputs

smallest = num;

secondSmall = num; //after user inputs only 1 number, this number is both the smallest & second smallest

//need to get more than 1 input so we start a do-while loop

do{ //want this to run at least once

num = IO.readDouble();

if( num < smallest) {

secondSmall = smallest;

smallest = num;

/*if the new number entered is less than the previous smallest, then that newly entered number is

now the new smallest and the previous smallest is now the new second smallest */

}else if ( num < secondSmall && smallest < num) {

secondSmall = num;

/* if the new number entered is less than the previous second smallest and the previous

smallest is still less than the new number, then that newly entered number is the new second smallest */

}

count++; //add 1 to count the user enters a number (that isnt the term value)

}while (num != term);

if (count < 2) {

IO.reportBadInput();

return;

}else{

IO.outputDoubleAnswer(smallest);

IO.outputDoubleAnswer(secondSmall);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions