Results 1 to 5 of 5
Thread: Calculator Program
- 09-27-2012, 03:03 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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); } } }
-
Re: Calculator Program
You only need one Calculator object, not four.
- 09-27-2012, 06:51 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
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, 10:09 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
-
Re: Calculator Program
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
-
Problem with if statement in a calculator program
By peterhabe in forum New To JavaReplies: 2Last Post: 08-26-2011, 05:07 PM -
Getting Backspace on a java calculator program to work
By marylanddem in forum New To JavaReplies: 1Last Post: 12-09-2010, 01:01 AM -
Calculator program
By kevzspeare in forum New To JavaReplies: 6Last Post: 03-18-2009, 01:43 PM -
unreachable statement - Java calculator program
By V2001Gordon in forum New To JavaReplies: 3Last Post: 12-13-2008, 12:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks