Hello everyone; A simple program using Eclipse IDE to find the Average of both Integer and Decimal point numbers. The program allows the user to decide total number of numbers the user wants to find the average of and lets the user input the numbers. The user is not limited to predefined numbers in the program.
Line 1: Import Statement. All methods in the swing class are imported, saving the user the time of importing one by one.
Line 3: Package Name. The name of the new Package.
Line 4: Main Method. Every program needs a main method to run.
Line 5: Variable Declaration. A String variable to hold a value of String type.
Line 6: Variable Declaration and Initialization. A Double Variable to hold a Decimal Point value. The Variable is set to 0.
Line 7: Variable Declaration. A double Variable to hold a Decimal Point value.
Line 8: Variable Declaration. A String variable to hold a value of String type.
Line 9: Variable Declaration. A Double Variable Declaration to hold a Decimal Point value
Line 10: Variable Declaration. A Double Variable to hold a Decimal Point value. The Variable is set to 0.
Line 11: Variable Declaration. A Double Variable Declaration to hold a Decimal Point value
Line 13: Exception Handling try Statement. This statement tells the program what to do if the user ends the program prematurely.
Line 14: Assignment Statement. The variable NumbersTotalString is assigned a value. The showInputDialog method is called from the JOptionPane class. Variable will hold whatever value user inputs.
Line 15: Conversion. The .parseDouble() Method is called to convert a String Value to a Decimal point Value. The new Decimal Point value is then stored in a Double Variable.
Line 17: While Loop. While Loops can be used to execute a block of code multiple times while a condition is true.
Line 18: Assignment Statement. The variable NumberString is assigned a value. The showInputDialog method is called from the JOptionPane class. Variable will hold whatever value user inputs.
Line 19: Conversion. The .parseDouble() Method is called to convert a String Value to a Decimal point Value. The new Decimal Point value is then stored in a Double Variable.
Line 20: Arithmetic Operation. The values of sum and NumberDouble is added and stored in the sum value.
Line 21: Loop Counter variable. This determines when the Loop condition is met. When the condition is met, the program exits the loop
Line 22: While Loop Closing Brace.
Line 24: Arithmetic Operation. The Average is calculated.
Line 26: A Pop Up message to display the Average.
Line 27: Exception Handling try Statement closing brace.
Line 29: Exception Handling Catch Statement. The program displays a message if the try statement is not executed successfully.
Line 30: The program displays a message if the try statement is not executed successfully.
Line 31: Exception Handling Catch Statement Closing Brace.
Line 32: Main Method Closing Brace.
Line 33: Package Closing Brace.
User is prompted to enter number of numbers to find Average. For illustration purposes, user inputs 3.
User inputs First Number
User inputs second number
User inputs third number
Average is calculated and displayed on the screen.
A message is displayed if the user ends the program prematurely.
Code Block
import javax.swing.*;
public class Average {
public static void main (String args []) {
String numbersTotalString;
double sum = 0;
double average;
String numberString;
double numbersTotalDouble;
double counter = 0;
double numberDouble;
try {
numbersTotalString = JOptionPane.showInputDialog("How many numbers do you wish to find the average?");
numbersTotalDouble = Double.parseDouble(numbersTotalString);
while (counter < numbersTotalDouble) {
numberString = JOptionPane.showInputDialog("Enter a number");
numberDouble = Double.parseDouble(numberString);
sum = sum + numberDouble;
counter++;
}
average = sum / numbersTotalDouble;
JOptionPane.showMessageDialog(null, "The Average of the numbers is " + average);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You have exited the program");
}
}
}
GitHub Account
Source Codes from GitHub Account
Thank you for reading.
Posted on Utopian.io - Rewarding Open Source Contributors