Java Tutorial Pt.2 of many! - Building Basic Calculator

Discussion in 'Archives' started by diabl0, Sep 18, 2010.

Java Tutorial Pt.2 of many! - Building Basic Calculator
  1. Unread #1 - Sep 18, 2010 at 5:07 PM
  2. diabl0
    Joined:
    Jul 25, 2008
    Posts:
    902
    Referrals:
    0
    Sythe Gold:
    0

    diabl0 Apprentice
    Banned

    Java Tutorial Pt.2 of many! - Building Basic Calculator

    I am sure you have read my last thread about "Hello World". If you have not, please check it out.

    Today we will be working on creating a little basic calculator.

    Okay, as you learned in the last tutorial,
    When you open a New class file in Eclipse you will See:

    Well, in this tutorial we will be doing something new, We will be using a scanner.
    What the scanner will be used for in java is basically allowing user input.

    So we need to import the scanner code. So above your first line of code you should add:

    What this is doing is importing the scanner, so it will allow us to use it in our code.

    [​IMG]
    Okay, so now that we have got our scanner imported we are good to go!
    Remember, under your class line you should always add your main. So lets do that. So now you should have:

    [​IMG]
    Okay, let's get started with our coding.

    Lets go ahead and add our scanner under our main:
    PS. Scanner balls (Balls can be renamed to whatever you want).

    So now our code should be:
    [​IMG]

    Okay so lets go ahead and add a double. So under the scanner code lets add:
    What the double means is that unlike an int, which has to be a whole number, with double it allows it to be a fraction or whatever, such as 5.98 or 78.33. whereas int only allows numbers such as 5 or 3 or 6.

    The first means first number, second means second number, and of course answer.

    [​IMG]

    Okay, so now that is added under the scanner code, lets go ahead and let the user input their first number.

    So under the double code, lets add:
    This prints to the screen for the user to enter the first number.
    [​IMG]
    So now that it reads that, lets allow the user to input a number. Under that code lets add:

    This right here stores the first number entered into your scanner data. Notice the scanner name (Balls) up there.

    [​IMG]

    Okay so basically the same thing for second number:

    [​IMG]

    Okay, so now we are almost done. We have got it to print out what we have wanted it to... but how do we get the answer to print out? Easy, First we need to tell the code to add the two together to get answer. so lets add this under the second double line:

    Thats right, this adds first number and second number to produce answer. This can easily be changed, instead of add, just remove "+" and add one of the following, -, *, /.

    Okay so now, how do we get it to print onto the screen? Easy, remember how we told the program to print out what we wrote? Well lets do the same thing except with a twist. You will see, lets add this now for our final line:

    Okay so you noticed what we did there? Instead of having "" inside, and typing in there what we want it to show, we just typed the name of answer so it will show answer. Okay Well that is basically it for a basic calculator, Here should be your final result:

    [​IMG]
     
  3. Unread #2 - Sep 18, 2010 at 6:21 PM
  4. poopy2177
    Joined:
    Jan 17, 2009
    Posts:
    1,359
    Referrals:
    1
    Sythe Gold:
    0

    poopy2177 Guru
    $5 USD Donor New

    Java Tutorial Pt.2 of many! - Building Basic Calculator

    ty for this I am trying to learn java and I sorta understand know more ty
     
  5. Unread #3 - Sep 18, 2010 at 6:29 PM
  6. diabl0
    Joined:
    Jul 25, 2008
    Posts:
    902
    Referrals:
    0
    Sythe Gold:
    0

    diabl0 Apprentice
    Banned

    Java Tutorial Pt.2 of many! - Building Basic Calculator

    No problem buddy, I am about to add some pictures, so hopefully that will make it easier to learn :)
     
  7. Unread #4 - Mar 2, 2011 at 1:18 AM
  8. youknowwho
    Joined:
    Jun 5, 2005
    Posts:
    484
    Referrals:
    0
    Sythe Gold:
    44

    youknowwho Forum Addict

    Java Tutorial Pt.2 of many! - Building Basic Calculator

    Hey black out your rs name, and thanks for the guide, id rather you just go to the next step of like part 1 like writing more of something than this but this does work also.
     
  9. Unread #5 - Mar 2, 2011 at 9:46 PM
  10. diabl0
    Joined:
    Jul 25, 2008
    Posts:
    902
    Referrals:
    0
    Sythe Gold:
    0

    diabl0 Apprentice
    Banned

    Java Tutorial Pt.2 of many! - Building Basic Calculator

    well that account is banned :p so no need to black it out
     
< -:-Laini-:- vouches :) | Rabs >

Users viewing this thread
1 guest


 
 
Adblock breaks this site