Respuesta :
Answer:
Below is the program TwoSmallest.java with each step explanation in form of comments.
public class TwoSmallest { // class definition
//main class having string args as a parameter
public static void main(String[] args)
{ if (args.length < 2)
{ //string length must not be less than 2 for proceeding
System.out.println("Please provide double values as command line arguments");
}
else {
// first two entries of string are checked for min1 and min2 and stored in variables with data type double
double min1 = Double.parseDouble(args[0]), min2 = Double.parseDouble(args[1]), num;
//when min1 will be greater than min2 it will be stored temporary in variable temp having data type double
if (min1 > min2) {
double temp = min1;
min1 = min2;
min2 = temp; //value of temp will be stored in min2
}
//loop will check for each entry remaining until the last character of string
for (int i = 2; i < args.length; i++) {
num = Double.parseDouble(args[i]);
if (num < min1) {
min2 = min1;
min1 = num;
} else if (num < min2) {
min2 = num;
}
}
//min1 will give the 1st minimum number and min2 will give 2nd minimum.
System.out.println(min1);
System.out.println(min2);
//both characters will be printed in sequence
}
}
}
The program is an illustration of arrays and loops.
The program in Java, where comments are used to explain each line is as follows:
import java.util.*;
public class Main {
public static void main(String args[]) {
//This declares the number of arguments
int n;
//This creates a scanner object
Scanner input = new Scanner(System.in);
//This gets input for the number of arguments
n = input.nextInt();
//This declares an array to get all arguments
double nums []= new double [n];
//The following array gets input from the user
for (int i=0; i<n;i++){
nums[i] = input.nextDouble();
}
//This sorts the array
Arrays.sort(nums);
//This prints the output header
System.out.println("The two smallest are: ");
//The following prints the two smallest numbers
for (int i=0; i<2;i++){
System.out.println(nums[i]);
}
}
}
At the end of the program, the smallest and the second-smallest numbers are displayed.
See attachment for sample run
Read more about similar programs at:
https://brainly.com/question/18120830
