How to Write a Java Program to Calculate the Mean

Table of contents:

How to Write a Java Program to Calculate the Mean
How to Write a Java Program to Calculate the Mean
Anonim

Nowadays, knowing how to calculate the arithmetic mean of a set of numbers is a very important operation. The average is used in many mathematical operations, so it is a basic calculation to be able to master. However, if we are dealing with a very large set of numbers, it is much easier to use a program to perform the calculation. This guide shows you how to create a simple Java program that averages the entered set of numbers.

Steps

Write a Program in Java to Calculate the Mean Step 1
Write a Program in Java to Calculate the Mean Step 1

Step 1. Plan your schedule

Planning your schedule before you start creating it is a vital step. Think about all the operations it will have to perform and the purpose for which it is created. Will the program have to work with very large numbers? If the answer is yes, then use a 'long' data type instead of just 'int'.

Try manually averaging a small set of small numbers. This will give you a better understanding of how your program will work

Write a Program in Java to Calculate the Mean Step 2
Write a Program in Java to Calculate the Mean Step 2

Step 2. Write the code

In order to calculate the average, you need to know the following information:

  • There sum of all the numbers entered in input by the user.
  • The total number of numbers entered by the user.

    For example, if the sum of the numbers supplied were 100 and the number of the elements supplied 10, then the mean would be equal to 100/10 i.e. 10.

  • We can therefore deduce that the formula for calculating the mean is:

    Average = Sum of the input numbers / Total of the numbers entered

  • To get all this information (input) from the user, you can try using Java's Scanner class.

    Since you will receive a set of multiple numbers as input, try using a loop to manage this part of the program. In the example code, a 'for' loop is used, but you can try to implement a program that uses the 'while' loop

Write a Program in Java to Calculate the Mean Step 3
Write a Program in Java to Calculate the Mean Step 3

Step 3. Calculate the average

To do this, use the formula deduced in the previous steps and insert it into the program code. Make sure the variable that stores the average value is of type float. Otherwise the result may not be mathematically correct.

  • This is because the float data type is a floating point number, which uses 32-bit single precision. This means that it also considers the decimal part of a number during mathematical operations. So using a float variable, the result of the following mathematical operation, 5/2 (5 divided by 2), will be 2, 5.

    • If to store the result of the same calculation (5/2), we had used an int variable, we would have obtained 2 as a solution to our problem.
    • However, the variables in which you are going to store the sum of the numbers entered by the user and the number of elements entered, being integers, can be stored in variables of type int. By using a float variable for the 'average', Java will automatically perform the conversion from int to float. Then the result will be displayed in float 'format', rather than integer (int).
    Write a Program in Java to Calculate the Mean Step 4
    Write a Program in Java to Calculate the Mean Step 4

    Step 4. Display the result of your calculation on the screen

    After the program has calculated the average, you can show it to the user. To do this you can use the Java method System.out.print or System.out.println (to print on the screen starting from a new line).

    Sample Code

    import java.util. Scanner; public class main_class {public static void main (String args) {int sum = 0, inputNum; int counter; float mean; NumScanner = new Scanner (System.in); Scanner charScanner = new Scanner (System.in); System.out.println ("Type the number of elements you want to average."); counter = NumScanner.nextInt (); System.out.println ("Please enter" + counter + "numbers:"); for (int x = 1; x <= counter; x ++) {inputNum = NumScanner.nextInt (); sum = sum + inputNum; System.out.println (); } mean = sum / counter; System.out.println ("The average of the" + counter + "numbers entered is" + mean); }}

    import java.util. Scanner; / * * This implementation of the program allows the user to continue entering * numbers until he has entered all the necessary numbers. * The string 'sentinel' is used to make the program * determine when the user has finished entering the input. * The 'Integer.parseInt (String s)' function parses the input string and returns the numbers * contained in the string. (For example Integer.parseInt ("462") == 462). * Important note: when using this method for input variables * do not compare strings using the operators * "==" or "! =". This would compare the memory addresses * where the strings are stored. * Use the s.equals (String t) method which returns 'true' if the two strings 's' and 't' are equal. * Instead, the! S.equals (String t) method returns true if two strings 's' and 't' are different. * / public class main_class {public static void main (String args) {String sentinel = ""; int sum = 0; int counter = 0; double mean = 0.0; NumScanner Scanner = new Scanner (System.in); System.out.println ("Enter the numbers to add. Type \" d / "when done."); System.out.print ("Enter a number:"); sentinel = NumScanner.next (); System.out.println (); while (! sentinel.equals ("d") &&! sentinel.equals ("D")) {sum + = Integer.parseInt (sentinel); counter ++; System.out.print ("Enter a number:"); sentinel = NumScanner.next (); System.out.println (); } mean = (sum * 1.0) / counter; System.out.println (); System.out.println ("The arithmetic mean of the numbers entered is:" + mean + "."); }}

    Advice

    • Try expanding your program so that it can do more math.
    • Try to create a graphical user interface (GUI) so that the program is more interactive and easier to use.

Recommended: