• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Java] Simple Calculator

Joined
Jun 17, 2009
Messages
2,726
Reaction score
340
Well, I'm new to Java and it's a bank holiday and I was bored so I made a simple calculator.

Code:
/**
 * Personal project
 */
package Mar;

import java.io.*;
import static java.lang.Math.*;
/**
 * @author Re
 *
 */
public class Calculator {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader dataIn = new BufferedReader( new InputStreamReader( System.in));
		//Storage
		int num1 = 0, num2 = 0, result = 0, choice = 0;
		
		double dResult = 0.0;
		// Main Input
		System.out.println("Welcome to Military's calculator");
		System.out.println("Enter 1 for addition, 2 for subtraction");
		System.out.println("Enter 3 for multiplication, 4 for division");
		System.out.println("Enter 5 for square root");
		System.out.println("Enter 0 to exit");
		System.out.println("Enter your choice: ");
		choice = Integer.parseInt(dataIn.readLine());
		
		while(choice != 0){ // Looping calculations
		switch(choice){
		case 1: // Using the addition choice
			System.out.println("Enter 1st number");
			num1 = Integer.parseInt(dataIn.readLine());
			System.out.println("Enter 2nd number");
			num2 = Integer.parseInt(dataIn.readLine());
			result = addNumber(num1, num2);
			System.out.println("The result is "+result+".");
			break;
		case 2: // Using the subtraction choice
			System.out.println("Enter 1st number");
			num1 = Integer.parseInt(dataIn.readLine());
			System.out.println("Enter 2nd number");
			num2 = Integer.parseInt(dataIn.readLine());
			result = subNumber(num1, num2);
			System.out.println("The result is "+result+".");
			break;
		case 3: // Using the multiplication choice
			System.out.println("Enter 1st number");
			num1 = Integer.parseInt(dataIn.readLine());
			System.out.println("Enter 2nd number");
			num2 = Integer.parseInt(dataIn.readLine());
			result = multiNumber(num1, num2);
			System.out.println("The result is "+result+".");
			break;
		case 4: // Using the division choice
			System.out.println("Enter 1st number");
			num1 = Integer.parseInt(dataIn.readLine());
			System.out.println("Enter 2nd number");
			num2 = Integer.parseInt(dataIn.readLine());
			dResult = divNumber(num1, num2);
			System.out.println("The result is "+dResult+".");
			break;
		case 5: // Using the square root choice
			System.out.println("Enter number");
			num1 = Integer.parseInt(dataIn.readLine());
			dResult = squareNumber(num1);
			System.out.println("The result is "+dResult+".");
			break;
		}
		//Loop Input
		System.out.println("Enter your choice: ");
		choice = Integer.parseInt(dataIn.readLine());
	}
		System.out.println("Program was closed!"); // When 0 is pressed
}
	static int addNumber(int num1, int num2){ // Dedicated addition function
		int result = 0;
		result = num1 + num2;
		return result;
		
	}
	
	static int subNumber(int num1,int num2){ // Dedicated subtraction function
		int result = 0;
		result = num1 - num2;
		return result;
		
	}
	
	static int multiNumber(int num1, int num2){ // Dedicated multiplication function
		int result = 0;
		result = num1 * num2;
		return result;
		
	}
	
	static double divNumber(int num1, int num2){ // Dedicated division function
		double result = 0.0;
		result = (num1 / num2);
		return result;
	}
	
	static double squareNumber(int num1){ // Dedicated square function
		double result = 0.0;
		result = sqrt(num1);
		return result;
		
	}
}

I guess I'll add more functions as I get better.
 
Joined
Aug 6, 2005
Messages
552
Reaction score
298
Well, I'm new to Java and it's a bank holiday and I was bored so I made a simple calculator.
...
I guess I'll add more functions as I get better.
You could remove duplicated code, example:
Code:
while(choice != 0) { // Looping calculations
	System.out.println("Enter 1st number");
	num1 = Integer.parseInt(dataIn.readLine());
	System.out.println("Enter 2nd number");
	num2 = Integer.parseInt(dataIn.readLine());
	switch(choice){
		case 1: // Using the addition choice
			result = addNumber(num1, num2);
			break;
		case 2: // Using the subtraction choice
			result = subNumber(num1, num2);
			break;
		case 3: // Using the multiplication choice
			result = multiNumber(num1, num2);
			break;
		case 4: // Using the division choice
			dResult = divNumber(num1, num2);
			break;
		case 5: // Using the square root choice
			dResult = squareNumber(num1);
			break;
		}
	}
I know the last case is working with just one number, so there is room for improvement, but I hope you get the idea.
Other possible improvements:
- remove the static functions(addNumber etc.), as they have no real benefit.
- replace the magic numbers by enums or constants.
Ofc there is more room for improvement, but since you are new to java this should be enough for now.
 
Back
Top