Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, i am new here so i am sorry if i am posting this in the wrong category. I'm a begginer programmer and i am trying to get better. Right now i am writing a small tool that solves mathematical problems. I would very much appreciate it if you could tell me what i'm doing wrong and how i could improve my code (the tool is working). The project is not finished yet and thus far i have only made a simple calculator. However i intend to build the other features the same way as this simple calculator so any feedback would be nice.

What I have tried:

This is the current code, the reason the whole thing is inside a while loop is because i want the user to be able to use the calculator tool multiple times without having to run the app every time.

Java
import java.util.Scanner;
public class NumberHeron
{
	public static void main(String[] args)
	{
		System.out.println("Choose your prefered tool\n");
		System.out.println("1. Simple Calculator\n");
		Scanner scanner = new Scanner(System.in);
		int tool = scanner.nextInt();
		switch(tool)
		{
			case 1:
		    while(tool == 1)
			{
			try {
			System.out.println("Enter two numbers");
			double numOne = scanner.nextDouble();
			double numTwo = scanner.nextDouble();
			System.out.println("Enter an operation, valid ones are: +, -, *, /");
			System.out.println("Type (q) to exit");
			Scanner s = new Scanner(System.in);
			String operation = s.nextLine();
			if (operation.equals("+"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.add(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("-"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.substract(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("*"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.multiply(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("/"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.divide(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("q"))
			{
				break;
			}
		  } catch(Exception ex) { ex.printStackTrace(); break; }
		}
		}
		scanner.close();
	}
}
class SimpleCalculator 
{
	public static double add(double valueOne,double valueTwo)
	{
		double result = valueOne + valueTwo;
		return result;
	}
	public static double substract(double valueOne, double valueTwo)
	{
		double result = valueOne - valueTwo;
		return result;
	}
	public static double multiply(double valueOne, double valueTwo)
	{
		double result = valueOne * valueTwo;
		return result;
	}
	public static double divide(double valueOne, double valueTwo)
	{
		double result = valueOne / valueTwo;
		return result;
	}
}
Posted
Updated 27-Aug-17 22:07pm
v2
Comments
Mohibur Rashid 27-Aug-17 19:03pm    
there are several issue in your source code.
1. The algorithm is incorrect
2. You have declared function in SimpleCalculator as static but, you are trying to access them through object. Which does not make much sense. That is why you are supposed to get some warning.
SimpleCalculator.add(1.0, 2.0)
will do just fine
Skapelse 28-Aug-17 8:38am    
im kinda confused. What do you mean the algorithm is incorrect? I want the user to input the numbers. Thank you!

1 solution

Java
if (operation.equals("+"))
{
	SimpleCalculator simpleCalc = new SimpleCalculator();
	double result = simpleCalc.add(numOne, numTwo);
	System.out.println("The result is " + result);
}
else if (operation.equals("-"))
{
	SimpleCalculator simpleCalc = new SimpleCalculator();
	double result = simpleCalc.substract(numOne, numTwo);
	System.out.println("The result is " + result);
}

You coluld simplify this block by removing the redundant lines of code. And you do not need an instance of SimpleCalculator as all its methods are static.
Java
double result =0.0;
if (operation.equals("+"))
{
    result = SimpleCalculator.add(numOne, numTwo);
}
else if (operation.equals("-"))
{
    result = SimpleCalculator.substract(numOne, numTwo);
}
// etc for other operations
System.out.println("The result is " + result);


For better training in Java I recommend The Java™ Tutorials[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900