-4

I want to make a simple calculator, so one that will calculate square meter, one that will calculate "pi" or anything you can imagine. But i want to call on the beginning of the running program, the calculator i want to use. so if i run the program, that the system will ask me if i want to use the calculator that calculates square meter or the pi. for example output:

  1. which calculator you want to use: SquareMeter
  2. enter first number: 2
  3. enter second number: 4
  4. your answer is: 8

My question is, how do i make this?

Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
kees mees
  • 11
  • 1
  • 1
  • 7

2 Answers2

1

Two ways I can think of, the first is rather complicated but deals with a wide variety of potential user input, the second is simple using basic programming concepts but is limited in what it can do.

Use any of the methods described in this question. (I'm new. If referring to other answers like this is against the rules, just let me know, and please don't hate.)

Example code:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class StackTester {
    public static void main(String[] args) {
        Method method = null;
        // set methodName equal to the name of the method to be called
        String methodName = "hi";
        StackTester cls = new StackTester();
        Class c = cls.getClass();
        try {
            method = c.getMethod(methodName, int.class);
            System.out.println("method = " + method.toString()); 
        } 
        catch (SecurityException e) {
            System.out.println("fail1");
        } 
        catch (NoSuchMethodException e) {
            System.out.println("fail2");
        }
        try {
              System.out.println(method.invoke(cls, 8)); 
            }
        catch (IllegalArgumentException e) {
            } 
        catch (IllegalAccessException e) {
            } 
        catch (InvocationTargetException e) {
    }
}
    public int hi(int x) {
        return x;
    }
}

The alternate method is to use a large series of if/else or switch/case in conjunction with Scanner reading user input to call all the method names you wish to call.

And just a suggestion for future reference: try some research on your own! I learned a lot just trying to answer this question.

Community
  • 1
  • 1
Skynet_0
  • 121
  • 1
  • 7
0

You can use Scanner.

For example:

Scanner in = new Scanner(System.in);

System.out.println("which calculator you want to use?");

String calculatorType = in.nextLine();

System.out.println("enter first number: ");

String firstNumber = in.nextLine();

System.out.println("enter second number: ");

String secondNumber = in.nextLine();

if(calculatorType.equals("SquareMeter")
    //do something
    System.out.println("Your answer is: " + doSomeCalculation(firstNumber, secondNumber));

Inside the if statement you can call a different method depending on what calculation the user asks. In this case that would be the "SquareMeter" so the doSomeCalculation() would look something like that:

int doSomeCalculation(int a, int b){
    return a*b;
}

Hope this helps

qbit
  • 2,528
  • 2
  • 12
  • 27