Question

In: Computer Science

(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the...

(Intro/Basic) JAVA

Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.

Solutions

Expert Solution

Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Working of the code

  • String[] args is an array of strings which stores java command line arguments.
  • Since args array is a string array ,the values passed must be parsed into double
  • parseDouble() method of Double class is used for this.
  • A double variable max is declared to hold maximum value and it is initialized with the first value passed(ie args[0]
  • for loop is used to iterate through each value passed by command line
  • If number is greater than max,then number will be set as new max.
  • After the termination of loop,max value is printed.

Source code

public class Main {
    public static void main(String[] args) {
        //declaring a double variable max
        //initializing max with the first value passed(args[0])
        double max = Double.parseDouble(args[0]);
        //for loop iterate through each value passed by command line
        for (int i = 1; i < args.length; i++) {
            //parsing string value into double
            double number = Double.parseDouble(args[i]);
            //checking if number is greater than max
            if (number > max)
                //if yes set number as new max
                max = number;
        }
        //printing result
        System.out.println("Maximum value among entered numbers: " + max);
    }
}

Screen shot of the code

Screen shot of the output


Related Solutions

Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a program Median.java to read each file whose name is specified in the command-line arguments....
Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal...
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal integer literals. Each of these decimal integer literals will be expressed using decimal digits, only, and none of the decimal integer literals will be prefixed by a plus or minus sign. None of the decimal integer literals will be greater than ULONG_MAX. The program prints the integers in order, least-to-greates and also prints the sum of the integers.
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
Write a script named countmatches that expects at least two arguments on the command line. The...
Write a script named countmatches that expects at least two arguments on the command line. The first argument is the pathname of a dna file containing a valid DNA string with no newline characters or white space characters of any kind within it. (It will be terminated with a newline character.) This dna file contains nothing but a sequence of the bases a, c, g, and t in any order. The remaining arguments are strings containing only the bases a,...
16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the...
16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the program will read...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT