Averaging Numbers As discussed in Section 7.4 of the text book, when you run a J
ID: 3575438 • Letter: A
Question
Averaging Numbers
As discussed in Section 7.4 of the text book, when you run a Java program called Foo, anything typed on the command line after “java Foo” is passed to the main method in the args parameter as an array of strings.
Write a program Average.java that just prints the strings that it is given at the command line, one per line. If nothing is given at the command line, print “No arguments”.
Modify your program so that it assumes the arguments given at the command line are integers. If there are no arguments, print a message. If there is at least one argument, compute and print the average of the arguments. Note that you will need to use the parseInt method of the Integer class to extract integer values from the strings that are passed in. If any non-integer values are passed in, your program will produce an error, which is unavoidable at this point.
Test your program thoroughly using different numbers of command line arguments.
Currently I have:
import java.util.Scanner;
public class Average {
int[] ConvertArguments(String[] args) throws NumberFormatException {
int[] nums = new int[args.length];
for (int i = 0; i < args.length; ++i) {
nums[i] = Integer.parseInt(args[i]);
}
return nums;
}
private static void PrintArguments (String[] args)
{
for (int i = 0; i < args.length; ++i)
System.out.println(args[i]);
}
private static double calculateAverage;
int[] nums;
double sum = 0.0;
for (int i = 0; i < nums.length; ++i){
sum += nums[i];
return ((sum) / (nums.length));
}
public static void mainString[]
{
if (args.length > 0) {
PrintArguments(args);
int[] nums = ConvertArguments(args);
System.out.println("The average of the numbers is :" + calculateAverage(nums));
}
else
System.out.println("No arguments! ");
}
}
Any help is appreciated and points will be rewarded for correct answer. Thanks!
Explanation / Answer
import java.util.Scanner;
//Class Average defined
public class Average
{
//Method to handle number format exception
static int[] ConvertArguments(String[] args) throws NumberFormatException
{
//Decalares an array of entered number length
int[] nums = new int[args.length];
//Converted the inputted data to integer
for (int i = 0; i < args.length; ++i)
{
nums[i] = Integer.parseInt(args[i]);
}
//Returns the number array
return nums;
}
//Prints the arguments entered by the user in command line
private static void PrintArguments (String[] args)
{
for (int i = 0; i < args.length; ++i)
System.out.println(args[i]);
}
//Calculates the average of inputed numbers
private static double calculateAverage(int nums [])
{
double sum = 0.0;
//Calculates the total
for (int i = 0; i < nums.length; ++i)
sum += nums[i];
//Calculates and returns the average
return (sum) / (nums.length);
}
public static void main(String[] args)
{
//Checks for the number of arguments if it is more than zero number of arguments then print the array, converts to integer and calculates the average
if (args.length > 0)
{
PrintArguments(args);
int[] nums = ConvertArguments(args);
System.out.println("The average of the numbers is :" + calculateAverage(nums));
}
//If zero number of arguments then print message
else
System.out.println("No arguments! ");
}
}
Output:
E:One>javac Average.java
E:One>java Average
No arguments!
E:One>java Average 10 20 30
10
20
30
The average of the numbers is :20.0
E:One>java Average 10 p 30
10
p
30
Exception in thread "main" java.lang.NumberFormatException: For input string: "p"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Average.ConvertArguments(Average.java:13)
at Average.main(Average.java:40)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.