Math 101: Doing Arithmetic
Episode #3 of the course An introduction to Java programming by Alex Theedom
Welcome to the third of ten lessons about the Java programming language. I hope you are still enthused from the last lesson. Let’s jump straight in. Today, you are going to write code that performs mathematical calculations and prints the result to the console.
Step 1: Preparation
• Go to the online IDE, codiva.io, sign in, and click the New Project button.
• Call today’s project, Lesson 3.
• Create a class file called, Calculator.
• Delete the line, System.out.println(“Hello Codiva”);, to make space for your code.
Now you are ready to start coding
Step 2: Start Coding
You are going to write three lines of code today that adds two numbers together.
• First, you need to tell the program what two numbers you want to add together. This is done by declaring two variables and assigning them the two numbers we want to add together. So, the first line of code will define a variable and the variable type. It will look like this: int x = 5;. Don’t forget the semicolon at the end of the code line.
Let’s dissect this code snippet.
○ The int tells the program that the variable is an integer. An integer is a whole number.
○ Then you define a variable. In this case, the variable is called x.
○ Then you assign the number 5 to the variable with the assignment operator =.
It’s just like algebra, but more fun! Now the number 5 is stored in the variable x.
• The second line of code will define the second variable in just the same way as the first. It will look like this: int y = 10;. Notice that this time, the variable name is the letter y. You now have two lines of code that look like this:
class Calculator {
public static void main(String[] args) {
int x = 5;
int y = 10;
}
}
Next, you need to add the two numbers together. This is actually quite logical, and you might be able to guess this next step.
• To add two numbers that are stored in variables, you use the + operator to write code like this: x + y. Now you will need to assign the result of this equation to another variable. Let’s call it result. You now have code that looks like this: int result = x + y.
The result of 10 added to 5 is stored in the result variable, which is also declared as an integer. With the result of the calculation stored in the results variable, you will want to print it to the console so you can see the actual result. You might guess how this is done. You have already used code that prints text to the console. Well, you can use the same code to print numbers to the console.
• To print the value stored in the variable result, you just pass the result variable to the System.out.println(); statement, like so: System.out.println(result);.
Now your program is complete.
Step 3: Check and Run Your Program
Here is the code in full so you can check that you entered it all correctly:
class Calculator {
public static void main(String[] args) {
int x = 5;
int y = 10;
int result = x + y;
System.out.println(result);
}
}
Run your program by clicking the Run button in the top right-hand corner of the screen. If all went well, you will see the number 15 appear in the console at the bottom of the screen.
Step 4: Extend Your Calculator Program
Instead of adding together whole numbers, you can add together decimal numbers. Decimal number types are float. To add two floats, simple replace the int with float wherever you find it.
You can extend this code even further by doing multiplication by using the * (asterisk) instead of the + (addition) sign, division by using the / (forward slash), and subtraction by using the – (minus) sign.
Now you have a fully functioning calculator application.
Congratulations, you have successfully completed your third Java program!
In Lesson 4, you will learn how to write code that repeats code. So, see you then.
Recommended book
Java: A Beginner’s Guide by Herbert Schildt
Share with friends