Results 1 to 5 of 5
  1. #1
    CODinacup is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Question Calculator Program

    Is there any way i could shave this program down? I started learning yesterday so the code probably isnt top notch.

    Java 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);
      }
      }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    huupderitis is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

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

  4. #4
    CODinacup is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: Calculator Program

    Quote Originally Posted by huupderitis View Post
    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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calculator Program

    Quote Originally Posted by CODinacup View Post
    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.

Similar Threads

  1. Problem with if statement in a calculator program
    By peterhabe in forum New To Java
    Replies: 2
    Last Post: 08-26-2011, 05:07 PM
  2. Getting Backspace on a java calculator program to work
    By marylanddem in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 01:01 AM
  3. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 01:43 PM
  4. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM

Posting Permissions

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