--------------BEFOREHAND--------------Tutorial 1: The very first steps
At
http://www.opticraft.net/index.php/topic,9505.0.htmlWe get the JDK, Eclipse, and start coding the 'Hello World' program!Tutorial 2: Learning about integers!
At
http://www.opticraft.net/index.php/topic,9697.0.htmlWe learn about integers, doubles, and even scanners!Changelog:
0.1: Made the first template for my Java tutorials
0.2: Improved on the template
0.3: Made a 'BEFOREHAND' section at the top of the template.
CURRENT: 0.3.1: Making all my posts have the same ending, beforehand, and question score guide.
-------------------JAVA TUTORIAL #3 - Making the calculator-------------------Hello people, and welcome back to tutorial #3 of my Java tutorial series. So, shall we delve right into it?
-------------------THE CODE-------------------If you took the time to remember the contents that we learnt about Java, you will remember that I taught you how to make a Scanner and how to define integers and doubles. So, using this information, how can we make a simple program? Well, before we start, I must teach you something first.
What I am about to teach you is how to add / subtract / divide / multiply different integers / doubles. This is extremely useful and will benefit us greatly in the following Java tutorials. So, how do we do it? Well, it is very simple:
class Main{
public static void main(String[] args){
int first = 10;
int second = 20;
int answer;
answer = first + second;
System.out.println(answer);
}
}
If you run this program in Eclipse, you will note that it will print out the equation of 20 + 10, which of course is 30, printing out 30. How does it do this? Well, let's have a look:
int first = 10;
int second = 20;
int answer;
When you look at this, you will see that we define a value to both 'first' and 'second', but not to 'answer'. So, because we didn't give 'answer' a value, doesn't that mean that Java will give an error? No, because when defining integers / doubles, we don't HAVE to define it. However, if we wish to accomplish anything, we need to assign at least two values to the integers.
answer = first + second;
System.out.println(answer);
If you look above, you will see that 'answer' now equals to 'first' added by 'second'. This is how we can easily add / subtract / multiply / divide numbers together. And if you notice that in the brackets of the 'println' statement, it has the integer 'answer'. This basically means that we are going to output 'answer' to the screen. If we put the 'println' statement BEFORE the addition, it would result in nothing.
answer = first + second;
//A simple addition
answer = first - second;
//A simple subtraction
answer = first * second;
//A simple multiplication
answer = first / second;
//A simple division
In case you cannot read the notes next to the '//', here is what I said:
answer = first + second;
A simple addition
answer = first - second;
A simple subtraction
answer = first * second;
A simple multiplication
answer = first / second;
A simple division
As you can see, you can add, subtract, multiply, and divide integers / doubles together with ease and simply print it off.
Since we have now learnt how to do all sorts of stuff with integers / doubles, how can we use this to our advantage? Well, we are about to learn that with this simple calculator:
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first number");
int fnum = input.nextLine;
System.out.println("Please enter the second number");
int snum = input.nextLine;
System.out.println("This is the answer");
int answer = fnum + snum;
System.out.println(answer);
}
}
At first glance, you probably won't understand it. Let's go through it together:
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first number");
int fnum = input.nextLine;
Here we are declaring a Scanner by the name of 'input' and then we are outputting 'Please enter the first number' to the screen. Whatever the user puts in is then stored in the integer 'fnum', as an integer / double can be assigned any value.
System.out.println("Please enter the second number");
int snum = input.nextLine;
System.out.println("This is the answer");
int answer = fnum + snum;
Here we are basically doing the same thing; it outputs to the screen 'Please enter the second number', whatever the user inputs is stored in 'snum', and then 'This is the answer' is displayed. What happens now is that the code automatically adds 'fnum' and 'snum' together, and stores the result in the integer 'answer'.
System.out.println(answer);
}
}
All we are doing here is displaying whatever was stored in the integer 'answer'. And that's it inside a simple calculator. Note that you can only do this once before having to restart this program, but at least now you can feel proud of yourself knowing that you built a program from what you've learnt.
To fully learn and understand this program, go ahead and rewrite this entire thing out 2 - 3 times. Try renaming the integers to something else, and multiplying the integers instead. Make the integers double, and try adding '30.21' with '0.0023'
--------------(Not necessary but advisable) PRACTISING YOUR LEARNT JAVA--------------To practise your Java so far, please answer these following question either in your head, on a piece of paper, or on a new word document:
1) How do you make a Scanner?
2) How you make a double equal to what a person inputs?
3) How do you subtract two integers together?
4) How do you make 'snum' a predetermined number and only plus the 'fnum' with it?
5) Take an educated guess as to how you would make it output 'This is your answer: (answer)'
ANSWERS:
1) How do you make a Scanner?
A: Firstly, you need to import it. To do this, you do at the very start of the code 'import java.util.Scanner;'. Secondly, you need to declare a Scanner by doing 'Scanner (name) = new Scanner(System.in);'.
2) How you make a double equal to what a person inputs?
A: You simply replace 'int' with double.
3) How do you subtract two integers together?
A: Instead of 'fnum + snum', you do 'fnum - snum'.
4) How do you make 'snum' a predetermined number and only plus the 'fnum' with it?
A: You remove the 'snum' part of the code and put in 'snum = (number)'.
5) Take an educated guess as to how you would make it output 'This is your answer: (answer)'.
A: Instead of 'System.out.println(answer);', you would do 'System.out.print(answer);'. You would then replace 'This is the answer' with 'This is the answer: '
SCORES:
< 3: Try to re-read this tutorial and keep more notes in your brain.
3: Okay, but perhaps try looking briefly over the tutorial and re-writing the code.
4: Good job! You are ready to move onto the next tutorial.
5: Outstanding! You are more than ready to move onto the next tutorial.
--------------END SECTION--------------This has been tutorial #3 of my Java for beginners series. Please leave a reply containing constructive criticism and if you need any help, don't fret to either PM me or leave a message on this forum. I will get back to you ASAP!
Next tutorial:
http://www.opticraft.net/index.php/topic,9988.0.html