This is a simple application perfect to begin your journey in learning the amazing potential that Java offers! Java is a Object Oriented Language which means that every piece of code is inside a class. If you are not familiar with Object Oriented Programming I suggest reading my post on The Principles of Object Oriented Programming.
For this application we will need to import the Scanner using “import java.util.Scanner;” this will allow us to get the user input from the console.
Due to the simplicity of this application the class we are creating will also be the main class. For the Java compiler to work properly it needs a main method as an entry point. The main method is “public static void main(String[] args){ }” Often times we will utilize the main method to instantiate objects from other classes within our project. For now let’s focus on the basics.
To create an input inside the console we must create a Scanner object assigning it a variable. We then request user input by printing a line to the console. When a user enters an input we can then grab the input by calling the scanner variable and running the nextDouble method on it. The nextDouble method ensures that the input will be a double data type. If it is not it will throw an error. It is best practice to catch these errors to allow your software system to fail gracefully. Since this is a beginner level course we will focus on functionality. In later courses we will add error handling.
Let’s take the inputs we’ve gathered and preform a simple average on them. First we will add all of the variables we’ve collected together into one variable. Then we can take that variable and divide it by the total number of variables.
Now we can print all our data to the console! Congratulations, you just made an application that takes the average based on user input!
import java.util.Scanner; // Import the Scanner class
public class Average {
public static void main(String[] args) {
//--------------------------------------------------------------
// User Input
//--------------------------------------------------------------
Scanner IntOneIn = new Scanner(System.in); // Creates a Scanner object
System.out.println("Enter First Number"); // Prints line requesting user input
double int1 = IntOneIn.nextDouble(); // Reads user input
Scanner IntTwoIn = new Scanner(System.in);
System.out.println("Enter Second Number");
double int2 = IntTwoIn.nextDouble();
Scanner IntThreeIn = new Scanner(System.in);
System.out.println("Enter Third Number");
double int3 = IntThreeIn.nextDouble();
Scanner IntFourIn = new Scanner(System.in);
System.out.println("Enter Fourth Number");
double int4 = IntFourIn.nextDouble();
//--------------------------------------------------------------
// Average Inputs
//--------------------------------------------------------------
double Sum = int1 + int2 + int3 + int4;
double Avg = Sum / 4;
//--------------------------------------------------------------
// Output
//--------------------------------------------------------------
System.out.println("\n" + "First number: " + int1);
System.out.println("Second number: " + int2);
System.out.println("Third number: " + int3);
System.out.println("Fourth number: " + int4);
System.out.println("\n" + "The sum of the numbers is " + Sum);
System.out.println("The average of the numbers is " + Avg); // Output user input
}
}