Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

[TUT]Your First Calculator in Four Easy Steps[TUT]

Thanks andie12serador. I am glad you like it.
Even though its not advanced, what is a advanced calculator?
A calculator that allows you to divide by 0 is more advanced then the ones you buy at the 99 cent store!
But seriously it is a simple java program, not a scientific calculator!
Pretty good tutorial if I do say so myself.
This isn't really a tut, it's just copy paste. It's a nice source to learn from though.
June: $68.00
May: $142.41
You can try-catch the divide by zero exception to make a smoother experience for the user and put it inside the do-while loop.
[Image: vBCph.jpg]
I'd do it like this:

Code
import java.util.Scanner;

    public static void main (String args[]){

    double fn,sn,answer=0;
    int way;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter fnum");
    fn = input.nextDouble();

    System.out.println("Enter 1 for +, 2 for -, 3 for *, 4 for /");
    way = input.nextDouble();

    Sytem.out.println("Enter snum);
    sn = input.nextDouble();

    }if (way == 1){
        answer = fn + sn;

    }else if (way == 2){
        answer = fn - sn;

    }else if (way == 3){
        answer = fn * sn;

    }else if (way == 4){
        answer = fn / sn;

    //Here comes the anti-bug part

    }else if (way > 4){
        System.out.println("Enter a valid way to handle fnum and snum (under 4)");
    }else if (way < 1){
        System.out.println("Enter a valid way to handle fnum and snum (above 1)");

    //And here if someone tries to devide by zero (pfft, idiots)

    }else if (fnum == 0 && way == 4){
        System.out.println("Don't try to devide by zero...");
    }else if (snum == 0 && way == 4){
        System.out.println("Don't try to devide by zero...");
    }

    System.out.println("Your answer is: " + answer);

}

CBF testing it though, too lazy.

Basicly what it does is ask the user for a first number, then a way to handle the fnum and snum... and if you try to devide by zero you get an error...

(Feb 3, 2012 03:54 PM)Kiipa Wrote:  I'd do it like this:

Code
import java.util.Scanner;

    public static void main (String args[]){

    double fn,sn,answer=0;
    int way;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter fnum");
    fn = input.nextDouble();

    System.out.println("Enter 1 for +, 2 for -, 3 for *, 4 for /");
    way = input.nextDouble();

    Sytem.out.println("Enter snum);
    sn = input.nextDouble();

    }if (way == 1){
        answer = fn + sn;

    }else if (way == 2){
        answer = fn - sn;

    }else if (way == 3){
        answer = fn * sn;

    }else if (way == 4){
        answer = fn / sn;

    //Here comes the anti-bug part

    }else if (way > 4){
        System.out.println("Enter a valid way to handle fnum and snum (under 4)");
    }else if (way < 1){
        System.out.println("Enter a valid way to handle fnum and snum (above 1)");

    //And here if someone tries to devide by zero (pfft, idiots)

    }else if (fnum == 0 && way == 4){
        System.out.println("Don't try to devide by zero...");
    }else if (snum == 0 && way == 4){
        System.out.println("Don't try to devide by zero...");
    }

    System.out.println("Your answer is: " + answer);

}

CBF testing it though, too lazy.

Basicly what it does is ask the user for a first number, then a way to handle the fnum and snum... and if you try to devide by zero you get an error...

And your point is what? This is meant for new people wanting to learn to program in Java.
(Feb 4, 2012 05:33 AM)eXero Wrote:  And your point is what? This is meant for new people wanting to learn to program in Java.

Your calculator didn't give you an error when dividing by zero, and I do believe the one I posted had a while loop :) Just a example.
I liked it,
I have some advice

Maybe you should explain the code a bit so people can try and learn.
Thanks for upload help people like me that want to learn java.

Jhawsh
OMG!!Guess i learnt another way to create a calc :)
I tried my best. Give me a break. I posted this a long time ago as well.
Not really an 'advanced calculator' and also, rename this thread to. [TUT]How to skid in four easy steps[TUT]
It didn't say it was an advanced calculator. I wish you could read.
(Feb 21, 2010 05:12 PM)eXero Wrote:  
Java Calculator

Step 1.
1.1 Open Netbeans, and create new project.
1.2 Title it whatever you want.
1.3 Click "Finish" to start the project.
Spoiler (Click to View)

Step 2.
Put this code in the program:Spoiler (Click to View)

PIC:
Spoiler (Click to View)

You should now have the coding done.

Step 4.
Click "Run" and you should now have a working calculator.
Spoiler (Click to View)

I hope you liked my tutorial. Please give me credits for this code, and thread. Please post a reply.

Its an okey guide you wrote, but why not take a moment and try to explain the code? Since this is aimed to beginners at java, i think an important step is to talk about the code so they can understand.
Anyone who can understand the code on its own can also write his own without your guide, so make it more beginner friendly.

Anyone, good guide overall, Btw, your pics arent working now.
This is not very helpful... only good for skids
[Image: j2RMNCH.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by ScrewbaccaDec 2, 2016 10:50 AM 19 1,366 Dec 2, 2016 10:50 AM
Last Post: Goodies
[C] by NielyMar 22, 2014 08:21 AM 8 211 Mar 22, 2014 08:21 AM
Last Post: Niely
[.NET] by KeeloFeb 9, 2013 10:39 PM 5 402 Feb 9, 2013 10:39 PM
Last Post: 0xDEAD10CC