Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Building a calculator

  1. #1
    Junior Member Goxy's Avatar
    Join Date
    Apr 2014
    Location
    Czech Republic
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Building a calculator

    Hi guys, am pretty new to java, i have few websites with alot of tutorials and help, besides that i have a friend also who started programming java this year, long story short, am getting a lot of help. But i learned some of the very very basics and played with Karel etc.
    So i decided to build my own calculator from scratch just to see what am i actually capable to do with what i have learned without looking at anyone else's code, because if i was looking at other codes, i would basicaly copy it and not let my brain to think by its self, you get the point.
    Every so and then if am looking for a method or some specific command i would be googling and that way i would learn something else, but i found my self stuck right now.

    So this calculator is very simple:
    1. It asks me for my first number (then you enter it)
    2. Then it asks you for your second number (then you enter it)
    3. Then it asks you for your desired operation (then you choose the operation)
    4. And finally you get the result.

    Here's the problem i managed to do everything until the no.3 i have no idea how to make my program to read what i have typed in and then create an actual method that would
    allow me to choose the operation and then execute it, i have googled around and have found literally nothing, maybe am searching for the wrong things.
    I don't really want someone to solve this for me, for it my own assignment. What i need is if someone could shed some light towards which direction i should go, or even possible let me know whats the method/command missing and explain it to me, or even if they know some links that would explain what is needed.

    Heres my code:
    package project1;
    import java.util.Scanner;
     
    public class Learning 
    {
        public static void main(String args[])
        {
            double number1, operations, number2, sum;
            Scanner input = new Scanner(System.in);
            //values valuesObject = new values();
     
            System.out.println("Enter First Number: ");
            String typing = input.nextLine();
            System.out.println("Enter Second Number: ");  
            String typing2 = input.nextLine();
     
            System.out.println("Choose Operation: ");
     
     
        }  
    }

    //values valuesObject = new values();

    This is another class created separately which i thought would actually help me, but then i realised that i dont really need it, so i just shaded it with comment line (//)
    So i basically called the object out of that class, it really has nothing to do with the calculator, but just posting it so you can actually understand why its shaded.

    // i have no idea wtf this does, but ill will figure out soon 
    package project1;
    public class values 
    {
        public String valueValue;
        public void value(String donkeyBalls)
        {
            valueValue = donkeyBalls;
        }    
    }


  2. #2
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Building a calculator

    Ok what you need is to test what type of operator it's inputed and then give it some instructions, so you should go with if statements like this:

    if(operation.equals("+")) {
    //type in what you want to do if true
    }

    ps: Dont forget to test every operator so you can make all operations.

    Also you're making the number input the wrong way insted of String typing = scanner.nextLine() you should be using your num1 and num2 like this num1 = scanner.nextDouble() because you're using numbers not strings.

    --- Update ---

    If you want i can post the code but you said you wanted tips, anyway it's done.
    Last edited by Crash; April 9th, 2014 at 12:48 PM.

  3. The Following User Says Thank You to Crash For This Useful Post:

    Goxy (April 9th, 2014)

  4. #3
    Junior Member Goxy's Avatar
    Join Date
    Apr 2014
    Location
    Czech Republic
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Building a calculator

    I found the mistake after a lot of messing around, basically you cannot type number1 = Scanner.nextDouble(); i realised that my code was actually looking at the input so for the code to work I had to write number1 = input.nextDouble();.

  5. #4
    Junior Member Goxy's Avatar
    Join Date
    Apr 2014
    Location
    Czech Republic
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Building a calculator

    I've solved and written it. Crash thanks for the help with a code mate!

    package project1;
    import java.util.Scanner;
     
    public class Learning 
    {
        public static void main(String args[])
        {
            double number1, operations, number2, sum;
            Scanner input = new Scanner(System.in);
            //values valuesObject = new values();
     
            System.out.println("Enter First Number: ");
            number1 = input.nextDouble();
            System.out.println("Choose Operation: "); 
            String operation = input.next();
            System.out.println("Enter Second Number: ");  
            number2 = input.nextDouble();
     
     
            if(operation.equals("+"))
            {       
                sum = number1 + number2;
                System.out.println("The sum is: " + sum);            
            }
            if(operation.equals("-"))
            {
                sum = number1 - number2;
                System.out.println("The sun is: " + sum);
            }
            if(operation.equals("*"))
            {
                sum = number1 * number2;
                System.out.println("The sum is: " +sum);
            }
            if(operation.equals("/"))
            {
                sum = number1 / number2;
                System.out.println("The sum is: "+ sum);
            }
     
        } 
     
    }

    Pretty much looks like this. Am just going to add a function that once I have executed the calculation it doesn't end the program strait away.
    That is, once the give calculation is calculated, I would be asked if I want to execute another calculation or end the program.

  6. #5
    Junior Member Goxy's Avatar
    Join Date
    Apr 2014
    Location
    Czech Republic
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Building a calculator

    As i said in my last post: Am just going to add a function that once I have executed the calculation it doesn't end the program strait away.
    That is, once the give calculation is calculated, I would be asked if I want to execute another calculation or end the program.
    Now i know it has to do something with loop, and condition, am reading it up but it doesn't seem to add up, at least for now.
    I have do something like:

    do{ //code}
    while(condition)
    But honestly I have no clue what actual command(//code section) will tell java to re-run the program, and setting a supporting condition to it.
    Any tips on what should I do??

  7. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Building a calculator

    Quote Originally Posted by Goxy View Post
    As i said in my last post: Am just going to add a function that once I have executed the calculation it doesn't end the program strait away.
    That is, once the give calculation is calculated, I would be asked if I want to execute another calculation or end the program.
    Now i know it has to do something with loop, and condition, am reading it up but it doesn't seem to add up, at least for now.
    I have do something like:

    do{ //code}
    while(condition)
    But honestly I have no clue what actual command(//code section) will tell java to re-run the program, and setting a supporting condition to it.
    Any tips on what should I do??
    My tip to you is to look at booleans, see how you get on from there and try to figure out how they will help you

  8. The Following User Says Thank You to SauronWatchesYou For This Useful Post:

    Goxy (April 13th, 2014)

  9. #7
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Building a calculator

    I wouldn't go with a loop but again with a if statement and a method for reseting. Here is what you should do for checking whether to terminate or reset:

    //Ask if you want to terminate or make another calculation
    System.out.println("Do you want to reset? Yes or No");
    //Option Input
    option = scanner.nextLine();
    System.out.println();
     
    //Checking whether the program should reset or terminate
    if(option.equals("Yes")) {
    	mainObject.reset();
    }else{
    	return;
    }

    I didn't include the reset method part because i thought you would want to figure it out yourself but if you need help just ask

  10. The Following User Says Thank You to Crash For This Useful Post:

    Goxy (April 13th, 2014)

  11. #8
    Junior Member Goxy's Avatar
    Join Date
    Apr 2014
    Location
    Czech Republic
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Building a calculator

    As a matter of a fact since I literally just started programming, been a week or so, after looking for an actual solution to my given task I have come to conclusion that's it was too early for me to do something like that, I have found that there are a lot of rules and way of how the method functions within the syntax before I could actually execute that into code. I was going to give up on it just for a week or so until I learn more but now this code you posted has gotten me thinking.

    Here's the actual code of the full calculator I have made which self terminates after every calculation:
    package project1;
    import java.util.Scanner;
     
    public class Learning 
    {
        public static void main(String args[])
        {
            double number1, operations, number2, sum;
            Scanner input = new Scanner(System.in);
            //values valuesObject = new values();
     
            System.out.println("Enter First Number: ");
            number1 = input.nextDouble();
            System.out.println("Choose Operation: "); 
            String operation = input.next();
            System.out.println("Enter Second Number: ");  
            number2 = input.nextDouble();
     
     
            if(operation.equals("+"))
            {       
                sum = number1 + number2;
                System.out.println("The sum is: " + sum);            
            }
            if(operation.equals("-"))
            {
                sum = number1 - number2;
                System.out.println("The sun is: " + sum);
            }
            if(operation.equals("*"))
            {
                sum = number1 * number2;
                System.out.println("The sum is: " +sum);
            }
            if(operation.equals("/"))
            {
                sum = number1 / number2;
                System.out.println("The sum is: "+ sum);
            }
        }    
    }
    Crash thanks again for the code! But I cant seem to get around with it, as am constantly getting errors. I guess am just going to have to study more on the basics of how basic elements work, and then in a week or so I will get back to it and finish it.

  12. #9
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Building a calculator

    Always here to help and i'm also quite new, about 2 weeks. Also good luck with your study

  13. #10
    Junior Member Goxy's Avatar
    Join Date
    Apr 2014
    Location
    Czech Republic
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Building a calculator

    Lel, ok am back sooner than I thought, it seems like this was very easy now that I reread everything I have learned, I can't believe this was so simple:
    All I had to do is just created an int, name it and assign it, once its assigned I had to add all of my code inside do{//my code} and then outside the code add while to return different int value than the first assigned one.

    If the first do assigned value is 0 and the while assigned value is 1.
    That means after you are done with your code, you type your system.out.print to choose action (rerun it or end it). And now every time you you choose 1 you rerun the program, but if you choose 0 you exit it.

    package project1;
    import java.util.Scanner;
     
    public class Learning 
    {
         public static void main(String[] args)  
         {
            int returnNum = 0;
            Scanner input = new Scanner(System.in);
     
            do
            {
                double number1, operations, number2, sum;
                System.out.println("Enter First Number: ");
                number1 = input.nextDouble();
                System.out.println("Choose Operation: "); 
                String operation = input.next();
                System.out.println("Enter Second Number: ");  
                number2 = input.nextDouble();
     
     
                if(operation.equals("+"))
                {       
                    sum = number1 + number2;
                    System.out.println("The sum is: " + sum);            
                }
                if(operation.equals("-"))
                {
                    sum = number1 - number2;
                    System.out.println("The sun is: " + sum);
                }
                if(operation.equals("*"))
                {
                    sum = number1 * number2;
                    System.out.println("The sum is: " +sum);
                }
                if(operation.equals("/"))
                {
                    sum = number1 / number2;
                    System.out.println("The sum is: "+ sum);
                }
     
                System.out.println("Type 1 to continue or 0 to exit: ");
                returnNum = input.nextInt();
            }
     
            while(returnNum == 1);
            System.out.println("Finished");
        }
    }

    Crash I don't quite understand why would you want to add mainObject in there, this was pretty simple, could you explain the logic of going into object-oriented coding? I don't really see the logic into doing this and making something more complicated.

  14. #11
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Building a calculator

    I really didn't think to much about using the loop method and i don't think it made it more complicated. Here's my code:
     package com.crash.java;
     
    import java.util.Scanner;
     
    public class Main{
     
    	public static void main(String args[]) {
    		Main mainObject = new Main();
    		execute();
    	}
     
    	public static void execute() {
    		Main mainObject = new Main();
    		Scanner scanner = new Scanner(System.in);
     
    		int num1, num2, total;
    		String opr, option;
     
    		System.out.println("Input 2 numbers: ");
    		num1 = scanner.nextInt();
    		num2 = scanner.nextInt();
    		scanner.nextLine();
     
    		System.out.println("Now the operation: ");
    		opr = scanner.nextLine();
     
    		if(opr.equals("+")) {
    			total = num1 + num2;
    			System.out.println("Total: " + total);
    		}else if(opr.equals("-")) {
    			total = num1 - num2;
    			System.out.println("Total: " + total);
    		}if(opr.equals("/")) {
    			total = num1 / num2;
    			System.out.println("Total: " + total);
    		}if(opr.equals("*")) {
    			total = num1 * num2;
    			System.out.println("Total: " + total);
    		}
     
    		System.out.println();
    		//Ask if you want to terminate or make another calculation
    		System.out.println("Do you want to reset? Yes or No");
    		//Option Input
    		option = scanner.nextLine();
    		System.out.println();
     
    		//Checking whether the program should reset or terminate
    		if(option.equals("Yes") || option.equals("yes")) {
    			mainObject.reset();
    		}else{
    			return;
    		}
    	}
     
    	public static void reset() {
    		Main mainObject = new Main();
    		mainObject.execute();
    	}
    }
    Basicly what i thought when writting this was that i need something to make the whole program run again so i created a method to execute the code and then one that if called would run the execute method again, so i gues in the end is in fact a loop.
    I didn't think it was that difficult to use this method since it took me like 5 minutes but i think the reason for that is that right now i've been working alot with object-oriented coding and with multiple methods. Anyway with all this discussion we've found out 2 different ways of doing this, one with a do while loop and one using an execute and reset method.
    Last edited by Crash; April 14th, 2014 at 08:22 AM.

Similar Threads

  1. Hello everyone, a NewBee in the building------------>Viva
    By Viva in forum Member Introductions
    Replies: 3
    Last Post: April 26th, 2013, 04:34 AM
  2. Building Heaps
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 7
    Last Post: December 2nd, 2012, 03:44 AM
  3. Building your own Date class.
    By ShaunB in forum Java SE API Tutorials
    Replies: 5
    Last Post: October 25th, 2011, 06:37 PM
  4. Need help building a simple calculator
    By zigma in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 14th, 2011, 02:13 AM