Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: Write your code in the file TwoSmallest.java. Write your test cases in ass

ID: 3674500 • Letter: J

Question

JAVA:

Write your code in the file TwoSmallest.java. Write your test cases in assign2-testcases.txt. We wish to write a program that takes a set of numbers and determines which are the two smallest. Ask the user for the following information, in this order: A terminating value (real number). The user will enter this value again later, to indicate that he or she is finished providing input. 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 (if the sequence contains duplicate numbers) Using the Io module

**in ALL cases of error on input, RE-ASK the user for the input until it is correctly entered.** for example meaning if they only input one number after terminating value, they must star over from the BEGINNING.

NO USE OF SCANNER.

Explanation / Answer

Hi below i have written the code for your reference,

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class Program {

public static void main(String[] args) throws Exception {
//Scanner - accepts user input
Scanner scanner = new Scanner(System.in);
int input = 0;

List<Integer> list = new ArrayList<Integer>();

System.out.println("Input number(s). " +
"Input -1 to terminate:");

//Accept numbers while input is not -1
while((input=scanner.nextInt()) != -1){
list.add(input);
}

//if -1, lets get the 2 smallest numbers.
//sort the list in ascending order.
Collections.sort(list);


System.out.println("Smallest numbers:");

switch(list.size()){
case 0:
return;//Nothing to output
case 1://If only one input
System.out.println(list.get(0));
break;
default://If 2 or more inputs
System.out.println(list.get(0) +
" " + list.get(1));
}
}
}