Results 1 to 3 of 3
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default Create a Calculator in Java

    Hi, i just created a very simple calculator using what i learned so far.
    Im not sure if its in the correct section so correct me if it isnt.

    Java Code:
    import java.util.*;
    
    public class Calculator
    {
    	public static void main(String[] args)
    	{
    		/*Scanner input = new Scanner(System.in);
    		System.out.println("What are you doing?");
    		if (answer.equalsIgnorecase("multiplication"))
    		{
    			Multiplication();
    		}*/
    		System.out.println("Hello, If you are doing Multiplication Type 1.");
    		System.out.println("If you are doing Addition, Type 2.");
    		System.out.println("If you are doing Subtraction, Type 3");
    		System.out.println("If you are doing Division, Type 4");
    		Scanner input = new Scanner(System.in);
    		int decision;
    		decision = input.nextInt();
    		if (decision == 1)
    		{
    			Multiplication();
    	    }
    	    if (decision == 2)
    		{
    			Addition();
    		}
    		if (decision == 3)
    		{
    			Subtraction();
    		}
    		if (decision == 4)
    		{
    			Division();
    		}
    		else {
    			System.out.println("Those aren't valid numbers.");
    			wrongInput();
    		}
    	}
    
    	public static void wrongInput()
    	{
    		System.out.println("If you are doing Multiplication Type 1.");
    				System.out.println("If you are doing Addition, Type 2.");
    				System.out.println("If you are doing Subtraction, Type 3");
    				System.out.println("If you are doing Division, Type 4");
    				Scanner input = new Scanner(System.in);
    				int decision;
    				decision = input.nextInt();
    				if (decision == 1)
    				{
    					Multiplication();
    			    }
    			    if (decision == 2)
    				{
    					Addition();
    				}
    				if (decision == 3)
    				{
    					Subtraction();
    				}
    				if (decision == 4)
    				{
    					Division();
    				}
    				else {
    					System.out.println("Those aren't valid numbers.");
    					wrongInput();
    		}
    	}
    	public static void Multiplication()
    	{
    		int mInput1;
    		int mInput2;
    
    		Scanner input = new Scanner(System.in);
    		System.out.print("What is the first number? ");
    		mInput1 = input.nextInt();
    
    		System.out.print("What is the second number? ");
    		mInput2 = input.nextInt();
    
    		System.out.println(mInput1 + " times " + mInput2 + " equals " + mInput1 * mInput2);
    	}
    	public static void Addition()
    	{
    		int aInput1;
    		int aInput2;
    		int aAnswer;
    
    		Scanner input = new Scanner(System.in);
    		System.out.println("What is the first number?");
    		aInput1 = input.nextInt();
    
    		System.out.println("What is the second number?");
    		aInput2 = input.nextInt();
    
    		aAnswer = aInput1 + aInput2;
    		System.out.println(aInput1 + " plus " + aInput2 + " equals " + aAnswer);
    	}
    	public static void Subtraction()
    	{
    		int sInput1;
    		int sInput2;
    		int sAnswer;
    
    		Scanner input = new Scanner(System.in);
    		System.out.println("What is the first number?");
    		sInput1 = input.nextInt();
    
    		System.out.println("What is the second number?");
    		sInput2 = input.nextInt();
    
    		sAnswer = sInput1 - sInput2;
    		System.out.println(sInput1 + " minus " + sInput2 + " equals " + sAnswer);
    	}
    	public static void Division()
    	{
    		int dInput1;
    		int dInput2;
    		int dAnswer;
    
    		Scanner input = new Scanner(System.in);
    		System.out.println("What is the first number?");
    		dInput1 = input.nextInt();
    
    		System.out.println("What is the second number?");
    		dInput2 = input.nextInt();
    
    		dAnswer = dInput2 / dInput1;
    		System.out.println(dInput1 + " divided by " + dInput2 + " equals " + dAnswer);
    	}
    }
    Thanks.
    Albert:rolleyes:

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    To trim down the code a bit, you can eliminate your wrongInput method, by using a loop in the main method of your code, along with another question like "Do you want to do more calculations?"

    The basic idea is: answer = ""

    Java Code:
    While answer does not equal "no"
       Calculations logic(if statements, method calls etc...)
       answer = result of question to continue
    Just an idea to continue on what you've learned thus far.

    You can also look into how to pass parameters, and use only one method called doCalculation() and pass it a value that tells which type of calculation to do. That would take out all of the repetition of code.

    Greetings
    Marcus:cool:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    Congrats on your first Java program!!! :)

    Marcus has made some good suggestions.

    If you're looking to forward your knowledge take them into consideration, they will help teach you good programming style.

    Greetings

    Felissa:p

Similar Threads

  1. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 06:33 AM
  2. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM
  3. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 04:27 PM
  4. How to create ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 0
    Last Post: 02-04-2008, 07:57 AM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •