November 21, 2024, 09:42:31 pm

Author Topic: Java for beginners (3) - Making the calculator  (Read 4104 times)

0 Members and 1 Guest are viewing this topic.

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Java for beginners (3) - Making the calculator
« on: May 11, 2012, 10:51:05 am »
--------------BEFOREHAND--------------

Tutorial 1: The very first steps
At http://www.opticraft.net/index.php/topic,9505.0.html
We 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.html
We 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:

Code: [Select]
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:

Code: [Select]
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.

Code: [Select]
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.

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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.

Code: [Select]
         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'.

Code: [Select]
         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
« Last Edit: May 19, 2012, 10:28:05 am by bigbeno37 »


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.

bern019

  • Jr. Member
  • *
  • Posts: 55
    • View Profile
Re: Java for beginners (3) - Making the calculator
« Reply #1 on: May 12, 2012, 02:39:56 pm »
Hello BigBeno37. As I was coding, I have received errors in tutorial 3. Here is a link to check my errors and for assistance.   

http://i1261.photobucket.com/albums/ii581/yajeetz/Keepit.png

Thanks. I have also been receiving problems on tutorial 2.

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Re: Java for beginners (3) - Making the calculator
« Reply #2 on: May 12, 2012, 09:27:43 pm »
Please pardon me for my mistake. Simply reverse the calculation (answer = first + second; instead of first + second = answer;). The problem has been fixed in the post. And as for your problem regarding tutorial #2, what is your problem?
« Last Edit: May 12, 2012, 09:29:47 pm by bigbeno37 »


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.

DeeKay

  • Owner
  • Champion Member
  • *****
  • Posts: 2162
    • View Profile
Re: Java for beginners (3) - Making the calculator
« Reply #3 on: May 13, 2012, 01:26:05 am »
Although there may be thousands of videos out there for this, I think you should make a video tutorial and just see how it goes :)

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Re: Java for beginners (3) - Making the calculator
« Reply #4 on: May 13, 2012, 06:37:46 am »
Really? And post it here? Sure, I guess, but I often say 'um', 'uh' and the like. I also stutter as well, so I am not sure if I can do it. But coming from you I shall take it as a compliment and I will begin working on it.  ;D

Thanks xDeeKay!

EDIT: A video tutorial series shall begin shortly. I will post both a video tutorial AND a written tutorial on here.
« Last Edit: May 13, 2012, 07:15:12 am by bigbeno37 »


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Re: Java for beginners (3) - Making the calculator
« Reply #5 on: May 14, 2012, 06:59:06 am »
VIDEO FOR EPISODE 1 IS UP! GO VISIT IT AT http://www.youtube.com/watch?v=-NULQU2U2qU&feature=youtu.be


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.