# Calculator Program

• 09-27-2012, 04:03 AM
CODinacup
Calculator Program
Is there any way i could shave this program down? I started learning yesterday so the code probably isnt top notch.

Code:

```// calculator import java.util.Scanner; import java.io.*; public class Calculator{     public static void main(String[] args){      Calculator objVariable1 = new Calculator();  Calculator objVariable2 = new Calculator();  Calculator objVariable3 = new Calculator();  Calculator objVariable4 = new Calculator();         String user2;         int chos;         chos = 0;         System.out.println( "Please choose your operation:"+"\n"+"[1] Multiplication"+"\n"+"[2] Division"+"\n"+"[3] Addition"+"\n"+"[4] Subtraction");         Scanner user_input = new Scanner( System.in );         user2 = user_input.next();         chos = Integer.parseInt( user2 );           switch (chos){    case 1:  System.out.println("You chose multiplication.");     objVariable1.mult();  break;    case 2:  System.out.println("You chose division.");     objVariable2.div();  case 3:  System.out.println("You chose addition.");     objVariable1.add();  break;    case 4:  System.out.println("You chose subtraction.");     objVariable2.sub();   break;  default:  System.out.print("Please choose an available option.");        }           }     public static void mult(){         Scanner user_input = new Scanner( System.in );     System.out.println( "Please input the first number you would like to multiply:" );     double user1 = user_input.nextDouble();     System.out.println( "Please input the second number you would like to multiply:" );     double user2 = user_input.nextDouble();     user1 = user1 * user2;   System.out.println( " The answer is: "+user1);   System.out.println( " Type exit to exit. " );     String end = user_input.next();   if (end.length() == 4) {   System.exit(0);   }   }  public static void div(){         Scanner user_input = new Scanner( System.in );     System.out.println( "Please input the first number you would like to divide:" );     double user1 = user_input.nextDouble();     System.out.println( "Please input the second number you would like to divide:" );     double user2 = user_input.nextDouble();     user1 = user1 / user2;   System.out.println( " The answer is: "+user1);   System.out.println( " Type exit to exit. " );     String end = user_input.next();   if (end.length() == 4) {   System.exit(0);   }   }   public static void add(){         Scanner user_input = new Scanner( System.in );     System.out.println( "Please input the first number you would like to add:" );     double user1 = user_input.nextDouble();     System.out.println( "Please input the second number you would like to add:" );     double user2 = user_input.nextDouble();     user1 = user1 + user2;   System.out.println( " The answer is: "+user1);   System.out.println( " Type exit to exit. " );     String end = user_input.next();   if (end.length() == 4) {   System.exit(0);   }   }  public static void sub(){         Scanner user_input = new Scanner( System.in );     System.out.println( "Please input the first number you would like to subtract:" );     double user1 = user_input.nextDouble();     System.out.println( "Please input the second number you would like to subtract:" );     double user2 = user_input.nextDouble();     user1 = user1 - user2;   System.out.println( " The answer is: "+user1);   System.out.println( " Type exit to exit. " );     String end = user_input.next();   if (end.length() == 4) {   System.exit(0);   }   } }```
• 09-27-2012, 04:19 AM
Fubarable
Re: Calculator Program
You only need one Calculator object, not four.
• 09-27-2012, 07:51 AM
huupderitis
Re: Calculator Program
I agree with what he said. Assuming when people are done with the calculator they usually press Clear or something. You'll want to have like a "clear" option that basically resets the values being calculated back to zero so you can get new input.
• 09-27-2012, 11:09 PM
CODinacup
Re: Calculator Program
Quote:

Originally Posted by huupderitis
I agree with what he said. Assuming when people are done with the calculator they usually press Clear or something. You'll want to have like a "clear" option that basically resets the values being calculated back to zero so you can get new input.

How would I call back to the top of the main method?
• 09-28-2012, 12:35 AM
Fubarable
Re: Calculator Program
Quote:

Originally Posted by CODinacup
How would I call back to the top of the main method?

To repeat sections of code, you'd make a loop such as a while loop.

One thing you should get in the habit of is formatting your code correctly so that you and we can read and debug it more easily. I suggest that you indent each block 3 spaces, that all code lines in the same block be indented the exact same amount. Your code above shows an almost random indentation which will make it difficult if not impossible to catch certain errors.