Add three numbers
Here's your first Java code - it implements the algorithm for adding three numbers. See if you can understand it, and note how the algorithm is written into the code. It's usual to write down the algorithm as comments (which the computer doesn't run) and build the code around it.
/** * Program to calculate the mean of three numbers * @author Andy Evans * The next two lines are important for the code to run - we'll look at these * in the practical **/ class Calculate { public static void main (String args[]) { // Get 3 numbers. // "ints" are integer numbers. int numberOne = 7; int numberTwo = 23; int numberThree = 42; // Sum the numbers. int sum = numberOne + numberTwo + numberThree; // Divide the sum by 3. // "doubles" are numbers with decimal places. double result = sum/3; // Print the result. System.out.println(result); } }
Once you get used to running Java code, you might want a copy of this in a suitable version for compiling, in which case here's the source code version: Calculate.java.
When you're ok with this lot, go back to the materials.