/*This program takes two integers as command-line arguments (they are initially strings),
and returns the sum, difference (based on the order they're entered in the command line), product and quotient (also based on the order they're entered in the command line) of those two integers.

For example, in the Windows cmd, "java Test 10 2" prints the following:

Sum: 12
Difference: 8
Product: 20
Quotient: 5.0

*/

public class Test
{
	public static void main(String[] args)
	{
                int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]);

		int sum = a + b;
		int difference = a - b;
		int product = a * b;
		double quotient = a / b;
		System.out.println("Sum: "+sum);
		System.out.println("Difference: "+difference);
		System.out.println("Product: "+product);
		System.out.println("Quotient: "+quotient);
	}
}

Do you have a question about java programming?
What message does your program put out if there are not two arguments?

Edited 5 Years Ago by NormR1: n/a

/*This is another version of the same program, but instead of two integer arguments,
this takes in four arguments, and finds the sum, maximum, minimum, mean, median and
mode of those four arguments.  The mode is the first element by default, so if there
is no mode (i.e each element only occurs once in the array), the mode variable will
be set to the first element in the arguments array created to store the four command
line arguments. 

For example, "java Test 34 23 23 312" prints the following:

Sum: 392
Maximum: 312
Minimum: 23
Mean: 98.0
Median: 28.0
Mode: 23

This version shows how command line arguments can be used to obtain quick and easy
statistical information.*/


public class Test
{
	public static void main(String[] args)
	{
		int[] arguments = new int[4];
		int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]);
		int c = Integer.parseInt(args[2]);
		int d = Integer.parseInt(args[3]);
		
		arguments[0] = a;
		arguments[1] = b;
		arguments[2] = c;
		arguments[3] = d;
		
		int sum = a + b + c + d;
		int maximum = getMax(arguments);
		int minimum = getMin(arguments);
		double mean = sum/(arguments.length);
		int mode = getMode(arguments);
		double median = getMedian(arguments);
		
		System.out.println("Sum: "+sum);
		System.out.println("Maximum: "+maximum);
		System.out.println("Minimum: "+minimum);
		System.out.println("Mean: "+mean);
		System.out.println("Median: "+median);
		System.out.println("Mode: "+mode);
		
		
	}
	
	//Returns the maximum integer in the array
	public static int getMax(int[] array)
	{
		int max = 0;
		for(int i = 0; i < array.length; i++)
		{
			if(array[i] > max)
			{
				max = array[i];
			}
		}
		
		return max;
	}
	
	//Returns the minimum integer in the array
	public static int getMin(int[] array)
	{
		int min = array[0];
		for(int j = 1; j < array.length; j++)
		{
			if(array[j] < min)
			{
				min = array[j];
			}
		}
		
		return min;
	}
	
	//Returns the number of occurrences of an element in an integer array
	public static int getCount(int element, int[] array)
	{
		int count = 0;
		for(int k = 0; k < array.length; k++)
		{
			if(array[k] == element)
			{
				count++;
			}
		}
		
		return count;
	}
	
	//Returns the mode in an integer array 
	public static int getMode(int[] array)
	{
		int mode = array[0];
		for(int m = 1; m < array.length; m++)
		{
			if(getCount(array[m], array) > getCount(mode, array))
			{
				mode = array[m];
			}
		}
		
		return mode;
	}
	
	//Bubble sorts an integer array
	public static void bubbleSort(int[] array)
	{
		for(int i = 0; i < array.length - 1; i++)
		{
			if(array[i] > array[i+1])
			{
				int temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
			}
		}
	}
	
	//Sorts an integer array with an even number of elements (a simple "divide and conquer")
	public static void sort(int[] array)
	{
		int mid = (array.length) / 2;
		
		//"Splits the array into two smaller arrays"
		int[] arr1 = new int[mid]; 
		int[] arr2 = new int[mid];
		for(int i = 0; i < mid; i++)
		{
			arr1[i] = array[i];
		}
		for(int n = mid; n < array.length; n++)
		{
			arr2[n - mid] = array[n];
		}
		
		//Bubble sorts the two smaller arrays
		bubbleSort(arr1);
		bubbleSort(arr2);
		
		//Merges the two arrays back into the original 
		for(int j = 0; j < mid; j++)
		{
			array[j] = arr1[j];
		}
		for(int k = 0; k < arr2.length; k++)
		{
			array[k + mid] = arr2[k];
		}
		
	}
		
		
			
		
	
	//Returns the median of the elements in an integer array
	public static int getMedian(int[] array)
	{
		int mid = (array.length) / 2;
		int median;
		sort(array);
		if(array.length % 2 == 0)
		{
			median = (array[mid-1] + array[mid])/2;
		}
		else{
			median = array[mid];
		}
		
		return median;
	}
			
			
}

What message does your program put out if there are not enough arguments?

Do you have a question about java programming?
What message does your program put out if there are not two arguments?

I was working on the other version, so I didn't see your reply earlier, so I apologize. It doesn't give me any messages if there are more than two; it only takes the first two, and leaves the rest alone. If there are less than two, it gives me a java.lang.ArrayIndexOutOfBoundsException message.

Edited 5 Years Ago by jamd200: n/a

it gives me a java.lang.ArrayIndexOutOfBoundsException message.

Is that a friendly program?
How about a "how to use me" message if the number of arguments are incorrect.

Is that a friendly program?
How about a "how to use me" message if the number of arguments are incorrect.

Yeah, that would be a good idea. I'm a little new at programming in general, so I often tend to forget about user-friendliness when I code these. I just try to make them work.

I just try to make them work.

That's probably ok for the first 2 weeks of your personal use. But then you could forget and then what? It's very easy to add a help message and you and others will appreciate it, instead of getting that annoying ArrayIndexOutOfBoundsException message.
Here you're putting your code out for others that have less experience to see. The idea should be to show good techniques.

I added a try-catch block to this one to display a more friendly message than the ArrayIndexOutOfBoundsException message when less than four arguments are entered. Feedback on this is welcome.

/*This is another version of the same program, but instead of two integer arguments,
this takes in four arguments, and finds the sum, maximum, minimum, mean, median and
mode of those four arguments.  The mode is the first element by default, so if there
is no mode (i.e each element only occurs once in the array), the mode variable will
be set to the first element in the arguments array created to store the four command
line arguments. 

For example, "java Test 34 23 23 312" prints the following:

Sum: 392
Maximum: 312
Minimum: 23
Mean: 98.0
Median: 28.0
Mode: 23

This version shows how command line arguments can be used to obtain quick and easy
statistical information.*/


public class Test
{
	public static void main(String[] args)
	{
		try{
			int[] arguments = new int[4];
			int a = Integer.parseInt(args[0]);
			int b = Integer.parseInt(args[1]);
			int c = Integer.parseInt(args[2]);
			int d = Integer.parseInt(args[3]);
		
			arguments[0] = a;
			arguments[1] = b;
			arguments[2] = c;
			arguments[3] = d;
		
			int sum = a + b + c + d;
			int maximum = getMax(arguments);
			int minimum = getMin(arguments);
			double mean = sum/(arguments.length);
			int mode = getMode(arguments);
			double median = getMedian(arguments);
		
			System.out.println("Sum: "+sum);
			System.out.println("Maximum: "+maximum);
			System.out.println("Minimum: "+minimum);
			System.out.println("Mean: "+mean);
			System.out.println("Median: "+median);
			System.out.println("Mode: "+mode);
		}
                //Message received when less than four arguments are entered.
		catch(Exception e)
		{
			System.out.println("There can only be four arguments entered for this program.");
                        System.out.println("At the next prompt in this command line,");
			System.out.println("enter 'java Test' followed by the four (and only four) arguments");
			System.out.println("that you want to enter for this program.");
		}
			
		
		
	}
	
	//Returns the maximum integer in the array
	public static int getMax(int[] array)
	{
		int max = 0;
		for(int i = 0; i < array.length; i++)
		{
			if(array[i] > max)
			{
				max = array[i];
			}
		}
		
		return max;
	}
	
	//Returns the minimum integer in the array
	public static int getMin(int[] array)
	{
		int min = array[0];
		for(int j = 1; j < array.length; j++)
		{
			if(array[j] < min)
			{
				min = array[j];
			}
		}
		
		return min;
	}
	
	//Returns the number of occurrences of an element in an integer array
	public static int getCount(int element, int[] array)
	{
		int count = 0;
		for(int k = 0; k < array.length; k++)
		{
			if(array[k] == element)
			{
				count++;
			}
		}
		
		return count;
	}
	
	//Returns the mode in an integer array 
	public static int getMode(int[] array)
	{
		int mode = array[0];
		for(int m = 1; m < array.length; m++)
		{
			if(getCount(array[m], array) > getCount(mode, array))
			{
				mode = array[m];
			}
		}
		
		return mode;
	}
	
	//Bubble sorts an integer array
	public static void bubbleSort(int[] array)
	{
		for(int i = 0; i < array.length - 1; i++)
		{
			if(array[i] > array[i+1])
			{
				int temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
			}
		}
	}
	
	//Sorts an integer array with an even number of elements (a simple "divide and conquer")
	public static void sort(int[] array)
	{
		int mid = (array.length) / 2;
		
		//"Splits the array into two smaller arrays"
		int[] arr1 = new int[mid]; 
		int[] arr2 = new int[mid];
		for(int i = 0; i < mid; i++)
		{
			arr1[i] = array[i];
		}
		for(int n = mid; n < array.length; n++)
		{
			arr2[n - mid] = array[n];
		}
		
		//Bubble sorts the two smaller arrays
		bubbleSort(arr1);
		bubbleSort(arr2);
		
		//Merges the two arrays back into the original 
		for(int j = 0; j < mid; j++)
		{
			array[j] = arr1[j];
		}
		for(int k = 0; k < arr2.length; k++)
		{
			array[k + mid] = arr2[k];
		}
		
	}
		
		
			
		
	
	//Returns the median of the elements in an integer array
	public static int getMedian(int[] array)
	{
		int mid = (array.length) / 2;
		int median;
		sort(array);
		if(array.length % 2 == 0)
		{
			median = (array[mid-1] + array[mid])/2;
		}
		else{
			median = array[mid];
		}
		
		return median;
	}
			
			
}

If I may add, I don't believe that the approach of using a try-catch for handling the length of the arguments is correct:

catch(Exception e)
		{
			System.out.println("There can only be four arguments entered for this program.");
                        System.out.println("At the next prompt in this command line,");
			System.out.println("enter 'java Test' followed by the four (and only four) arguments");
			System.out.println("that you want to enter for this program.");
		}

In this case it is ok, but in general no. For example. With the above code you catch all the exceptions: catch(Exception e) { Meaning that if some other exception happens you will still display the same message. In this case of course I don't see any other possible exception caught, but it is not correct to ignore the exception instance 'e'.

Also you should never catch exceptions that can be avoided such as ArrayIndexOutOfBoundsException. You know that this exceptions happens when you try to access arrays, but I have never seen anyone loop like this:

int [] array = new int[4];
try {
  for (int i=0;i<1000000000;i++) {
     System.out.println(array[i]);
  }
} catch (ArrayIndexOutOfBoundsException e) {
}

A better approach would be:

if (args.length!=4) {
  System.out.println("There can only be four arguments entered for this program.");
  System.out.println("At the next prompt in this command line,");
  System.out.println("enter 'java Test' followed by the four (and only four) arguments");
  System.out.println("that you want to enter for this program.");

  System.exit(0);
}

int[] arguments = new int[4];
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);

..

Also NormR1 is correct at his last comment. You don't need an array like this: int[] arguments = new int[4]; But like this: int[] arguments = new int[args.length]; Then use a loop to convert the args to integers and put them in the array.

Comments
Nice details.I was too lazy.

Thank you for your feedback. Ok, I made it so that it takes in any number of arguments, but I kept the try-catch block just in case someone enters a non-numerical argument. Tell me what you think of this one:

public class Test
{
	public static void main(String[] args)
	{
		try{
			int[] arguments = new int[args.length];
			
			for(int i = 0; i < arguments.length; i++)
			{
				arguments[i] = Integer.parseInt(args[i]);
			}
		
			int sum = 0;
			
			for(int j = 0; j < arguments.length; j++)
			{
				sum += arguments[j];
			}
			
			int maximum = getMax(arguments);
			int minimum = getMin(arguments);
			double mean = sum/(arguments.length);
			int mode = getMode(arguments);
			double median = getMedian(arguments);
		
			System.out.println("Sum: "+sum);
			System.out.println("Maximum: "+maximum);
			System.out.println("Minimum: "+minimum);
			System.out.println("Mean: "+mean);
			System.out.println("Median: "+median);
			System.out.println("Mode: "+mode);
		}
		catch(Exception e)
		{
			System.out.println("You may have entered a non-numerical argument.");
		}
			
		
		
	}
	
	//Returns the maximum integer in the array
	public static int getMax(int[] array)
	{
		int max = 0;
		for(int i = 0; i < array.length; i++)
		{
			if(array[i] > max)
			{
				max = array[i];
			}
		}
		
		return max;
	}
	
	//Returns the minimum integer in the array
	public static int getMin(int[] array)
	{
		int min = array[0];
		for(int j = 1; j < array.length; j++)
		{
			if(array[j] < min)
			{
				min = array[j];
			}
		}
		
		return min;
	}
	
	//Returns the number of occurrences of an element in an integer array
	public static int getCount(int element, int[] array)
	{
		int count = 0;
		for(int k = 0; k < array.length; k++)
		{
			if(array[k] == element)
			{
				count++;
			}
		}
		
		return count;
	}
	
	//Returns the mode in an integer array 
	public static int getMode(int[] array)
	{
		int mode = array[0];
		for(int m = 1; m < array.length; m++)
		{
			if(getCount(array[m], array) > getCount(mode, array))
			{
				mode = array[m];
			}
		}
		
		return mode;
	}
	
	//Bubble sorts an integer array
	public static void bubbleSort(int[] array)
	{
		for(int i = 0; i < array.length - 1; i++)
		{
			if(array[i] > array[i+1])
			{
				int temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
			}
		}
	}
	
	
	//Returns the median of the elements in an integer array
	public static int getMedian(int[] array)
	{
		int mid = (array.length) / 2;
		int median;
		bubbleSort(array);
		if(array.length % 2 == 0)
		{
			median = (array[mid-1] + array[mid])/2;
		}
		else{
			median = array[mid];
		}
		
		return median;
	}
			
			
}

Where is the "how to use me" message if there are not enough args?

Comments
Thanks for helping me out with my code. - jamd200

Thank you for your feedback. Ok, I made it so that it takes in any number of arguments, but I kept the try-catch block just in case someone enters a non-numerical argument.

That is good thinking! But is best to user the NumberFormatException. Again with this code if you have an exception other than "the user has entered non numeric" you will display the same message. In this case, you are correct. Nothing else would be thrown.
Your code works.

But in case you don't know about the NumberFormatException it is the exception that you get when you try to call the Integer.parseInt with non numeric values.
So you could alter your code like this:

catch(NumberFormatException nfe) {
  System.out.println("You may have entered a non-numerical argument: "+nfe.getMessage());
}

And as NormR1 said:
Also I think that you should check that happens if the user doesn't enter any arguments. In this case, arguments will have length zero and you will have an exception when you try to divide in order to get the average.
You could add an if statement at the beginning checking if the length of the args is positive

Edited 5 Years Ago by javaAddict: n/a

Comments
Thank you for you help. - jamd200

Ok, hopefully, not many more changes have to be made to this. I added a static method that just prints the "How to" message, so that I don't have to retype it at every "catch" or "if" block that requires it. Also, I decided to make the integers into double-precision instead, since statistical data are usually floating-point, not integers. I made them integers in the beginning for simplicity, but in a practical application, double or float are much more suitable, so I changed it accordingly. Probably the next issue is the sorting method, since bubble sort isn't very efficient, but this is a simple application, so this might not be too high of a priority. Additionally, to make this more complete, I added the ability to calculate standard deviation, which is very important in statistical analysis.

/*This is another version of the same program, but instead of two numerical arguments,
this takes in any number of arguments, and finds the sum, maximum, minimum, mean, median,
mode and standard deviation of those arguments.  The mode is the first element by default,
so if there is no mode (i.e each element only occurs once in the array), the mode variable will
be set to the first element in the arguments array created to store the command
line arguments. 

For example, "java Test 34 23 23 312" prints the following:

Sum: 392.0
Maximum: 312.0
Minimum: 23.0
Mean: 98.0
Median: 28.5
Mode: 23.0
Standard deviation: 123.63454209888108

This version shows how command line arguments can be used to obtain quick and easy
statistical information.*/

import java.lang.Math.*;

public class Test
{
	public static void main(String[] args)
	{
		try{
			if(args.length <= 0)
			{
				System.out.println("You entered no arguments.");
				printHowTo();
			}
			else
			{
				double[] arguments = new double[args.length];
	
				for(int i = 0; i < arguments.length; i++)
				{
					arguments[i] = Double.parseDouble(args[i]);
				}
		
					
				double sum = getSum(arguments);
				double maximum = getMax(arguments);
				double minimum = getMin(arguments);
				double mean = sum/(arguments.length);
				double mode = getMode(arguments);
				double median = getMedian(arguments);
				double stdDev = getStdev(arguments);
		
				System.out.println("Sum: "+sum);
				System.out.println("Maximum: "+maximum);
				System.out.println("Minimum: "+minimum);
				System.out.println("Mean: "+mean);
				System.out.println("Median: "+median);
				System.out.println("Mode: "+mode);
				System.out.println("Standard deviation: "+stdDev);
			}
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("You may have entered a non-numerical argument: "+nfe.getMessage());
			printHowTo();
		}
			
		
		
	}
	
	//Prints a "How to use this program" message in the event of an error
	public static void printHowTo()
	{
		System.out.println("\nHOW TO USE THIS PROGRAM");
		System.out.println("----------------------------------------------------");
		System.out.println("\nTo use this program properly, do the following:");
		System.out.println("\nAt the prompt, enter 'java Test' followed by the arguments you wish to enter.");
		System.out.println("The arguments must be numbers, as this is a program that makes calculations using the numerical data you enter in the arguments.");
	}
	
	//Returns the sum of the elements in a numerical array
	public static double getSum(double[] array)
	{
		double sum = 0;
			
		for(int j = 0; j < array.length; j++)
		{
			sum += array[j];
		}
		
		return sum;
	}
	
	//Returns the maximum number in the array
	public static double getMax(double[] array)
	{
		double max = 0;
		for(int i = 0; i < array.length; i++)
		{
			if(array[i] > max)
			{
				max = array[i];
			}
		}
		
		return max;
	}
	
	//Returns the minimum number in the array
	public static double getMin(double[] array)
	{
		double min = array[0];
		for(int j = 1; j < array.length; j++)
		{
			if(array[j] < min)
			{
				min = array[j];
			}
		}
		
		return min;
	}
	
	//Returns the number of occurrences of an element in a numerical array
	public static int getCount(double element, double[] array)
	{
		int count = 0;
		for(int k = 0; k < array.length; k++)
		{
			if(array[k] == element)
			{
				count++;
			}
		}
		
		return count;
	}
	
	//Returns the mode in a numerical array 
	public static double getMode(double[] array)
	{
		double mode = array[0];
		for(int m = 1; m < array.length; m++)
		{
			if(getCount(array[m], array) > getCount(mode, array))
			{
				mode = array[m];
			}
		}
		
		return mode;
	}
	
	//Bubble sorts a numerical array
	public static void bubbleSort(double[] array)
	{
		for(int i = 0; i < array.length - 1; i++)
		{
			if(array[i] > array[i+1])
			{
				double temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
			}
		}
	}
	
	
	//Returns the median of the elements in a numerical array
	public static double getMedian(double[] array)
	{
		int mid = (array.length) / 2;
		double median;
		bubbleSort(array);
		if(array.length % 2 == 0)
		{
			median = (array[mid-1] + array[mid])/2;
		}
		else{
			median = array[mid];
		}
		
		return median;
	}
	
	//Returns the mean of the elements in a numerical array
	public static double getMean(double[] array)
	{
		double sum = getSum(array);
		int population = array.length;
		return sum/population;
	}
		
	
	//Returns the standard deviation of the elements in a numerical array
	public static double getStdev(double[] array)
	{
		int population = array.length;
		double average = getMean(array);
		double sumSquares = 0;
		
		for(int s = 0; s < population; s++)
		{
			sumSquares += (array[s] - average) * (array[s] - average);
		}
		
		double stDev = Math.sqrt(sumSquares/population);
		
		return stDev;
	}
			
			
}

Edited 5 Years Ago by jamd200: n/a

This question has already been answered. Start a new discussion instead.