Hack Forums
[Java] [TUT]Your First Calculator in Four Easy Steps[TUT] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] [TUT]Your First Calculator in Four Easy Steps[TUT] (/showthread.php?tid=282939)

Pages: Pages 1 2 3 4 5


* - eXero - May 27, 2011

Thanks andie12serador. I am glad you like it.


* - hego555 - May 28, 2011

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!


* - iDuL - Nov 25, 2011

Pretty good tutorial if I do say so myself.


* - Snoopsy - Dec 29, 2011

This isn't really a tut, it's just copy paste. It's a nice source to learn from though.


* - Custodian of Hell - Dec 29, 2011

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.


* - Mafugsa Bob - Feb 3, 2012

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...




* - eXero - Feb 4, 2012

(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.



* - Mafugsa Bob - Feb 4, 2012

(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.



* - denn1smeister - Feb 4, 2012

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


* - Funn1k - Sep 5, 2012

OMG!!Guess i learnt another way to create a calc :)


* - eXero - Sep 5, 2012

I tried my best. Give me a break. I posted this a long time ago as well.


* - sk0rmpa - Sep 6, 2012

Not really an 'advanced calculator' and also, rename this thread to. [TUT]How to skid in four easy steps[TUT]


* - eXero - Sep 8, 2012

It didn't say it was an advanced calculator. I wish you could read.


* - Dreamcasterâ„¢ - Sep 8, 2012

(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.


* - Undefined - Sep 8, 2012

This is not very helpful... only good for skids