Question

In: Computer Science

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

Solutions

Expert Solution

import java.text.DecimalFormat;

//class definition
public class Adder {

   //main class definition
   public static void main(String[] args) {
      
       //eclare and initialise variable to store sum and number converted from string
       double sum = 0, num=0;
      
       //for loop to read command line arguments
       for(int i=0;i<args.length;i++) {
           //Convert argument from string to double
           num = Double.parseDouble(args[i]);
          
           //calculate the sum
           sum = sum + num;
           }
      
       //create a decimal formal object to help round result to 2 decimal places
       DecimalFormat format = new DecimalFormat("0.0");
       //Print the result with the help of format object
       System.out.println("The sum is " + format.format(sum));
   }

}

OUTPUT


Related Solutions

Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Complete 2a-b 2a) Write a C program which displays the sum of the command line arguments....
Complete 2a-b 2a) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that can be added. Use the file name sum.c for your program. Test your program with sum 5 -10 3 and sum 8 and sum 2b) Write a C program that reads in a file and outputs it in reverse order. Use recursion. Test your program with the file reverse.txt...
(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.
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...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments...
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments w, x, y, and z. Define a boolean variable whose value is true if the four values are either in strictly ascending order (w < x < y < z) or strictly descending order (w > x > y > z), and false otherwise. Then, display the boolean variable value. NOTE 1: Do not use if statements on this program. NOTE 2: Assume that...
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...
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 a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT