Question

Write a program Adder.java that calculates and prints the sum of command line parameters, so if...

  1. Write a program Adder.java that calculates and prints the sum of command line parameters, so if you make the command line parameters be 2 5 22 then the program prints 29.

  2. Write a program ShowFiles.java that assumes the command line parameters are each a text file name directly readable by the program. For each file named, print out the three heading lines in the format shown, and then each line of the file contents.

    If file q.txt contains

    What is the answer

    to the universe

    and everything?

    and file ans.txt contains

    The answer is

    42.

    Then calling ShowFiles with parameters:

    q.txt ans.txt

    prints:

    ---------------------

    q.txt

    ---------------------

    What is the answer

    to the universe

    and everything?

    ---------------------

    ans.txt

    ---------------------

    The answer is

    42.

    You will need Scanner method hasNextLine for your file reading while-loop.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

public class Adder {
public static void main(String[] args) {
   int sum=0;
   //iterating through the string array
   for(String str:args){
       //converting it into int and adding to sum
       sum=sum+Integer.parseInt(str);
   }
   System.out.println("Sum is : "+sum);
}
}

Answer 2:

import java.io.File;
import java.util.Scanner;

public class ShowFiles {
   public static void main(String[] args) {
       //iterating through file names
       for (String str : args) {
           showContent(str);
       }
   }

   private static void showContent(String aStr) {
       //printing header
       System.out.println("What is the answer\nto the universe\nand everything?");
       System.out.println("------------------\n"+aStr+"\n------------------");
       try {
           //opening file
           Scanner sc = new Scanner(new File(aStr));
           //iterating through reach line
           while (sc.hasNextLine()) {
               System.out.println(sc.nextLine());
           }
       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

   }
}

Add a comment
Know the answer?
Add Answer to:
Write a program Adder.java that calculates and prints the sum of command line parameters, so if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • write a script named word_counts.sh that prints out how many words are on each line of...

    write a script named word_counts.sh that prints out how many words are on each line of standard input, as well as the total number of words at the end. The script should take no arguments. Instead, the script must work by reading every line of standard input (using a while loop) and counting the number of words on each line separately. The script is intended to work with standard input coming from a pipe, which will most often come from...

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows...

    Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, the names stored in a file. The coordinates to print the names will be also read from another file. Here is an example of what your graphic output should look like with the two files that are provided for test purposes. Drawing Panel Adam Bernie Cameron Daniel Fanny Gerard Harold Issac Jackie Raitlyn Note that the colors...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT