Question

java) write a program that takes command line argument and compute the average of their maximum...

java) write a program that takes command line argument and compute the average of their maximum and minimum. make sure there are command line arguments being passed before you attempt to compute anything

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

Code:

Output:

Note: pass command line arguments as space separated values.

Raw Code:

import java.util.*;
public class Avg {
   public static void main(String[] args) { // command line arguments are stored in String args[].
       // That's why i convert the every number in the String args[] array to double in this code.
       if(args.length == 0){ // If there is no commandline arguments are passed
           System.out.println("Please pass arguments");
       }
       else{
           double min=Double.parseDouble(args[0]); // Declaring minimum value as first number in array
           double max=Double.parseDouble(args[0]); // Declaring maximum value as first number in array
           int n = args.length; // Finding the length of the array
           for (int i=1;i<n;i++){
               double num = Double.parseDouble(args[i]); // Converting the number in String args[] array to double.
               if(num > max){ // Checking if the new number is greater then our max number or not.
                   max = num; // Changing the max value to num.
               }
               if(num < min){ // Checking if the new number is less then our min number or not.
                   min = num; // Changing the min value to num.
               }
           }
           double avg = (max + min)/2.0; // Calculating the average of max and min numbers in array.
           System.out.println("Average of maximum and minimum numbers is: " + avg); // Printing the average.
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
java) write a program that takes command line argument and compute the average of their maximum...
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
  • 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.)

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • 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...

  • I need help with a java program Write a program Enigma that takes a single String...

    I need help with a java program Write a program Enigma that takes a single String as a command line argument. Enigma should read the file specified by the String argument, add 5 to each byte, and leave the altered data values in a file whose name is the command line argument. Note that this "updating in place" is the most difficult part of this lab: java Enigma sophie.dat should read file sophie.dat, and upon completion, leave the modified data...

  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • 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...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

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