I wrote a simple calculation program. I want users to enter their request as 12+12 and return the answer. I used StringTokenizer, but I got an error and it doesn't show me any result. There was a mention that Calc is a superclass and MinusCalc and PlusCalc are subclasses. Does anyone have any idea?
void inputLineData() { // This is just the function that use for this case
System.out.println(" Plz enter your all numbers");
String strAll = key.next();
StringTokenizer st = new StringTokenizer(strAll);
int n1 = Integer.parseInt(st.nextToken());
String str = st.nextToken();
int n2 = Integer.parseInt(st.nextToken());
switch (str.charAt(0)) {
case '+':
PlusCalc P = new PlusCalc(n1, n2);
listCalc[indexCalc] = P;
indexCalc++;
break;
case '-':
MinusCalc M = new MinusCalc(n1, n2);
listCalc[indexCalc] = M;
indexCalc++;
break;
default:
System.out.println("Error!");
}
}
And this is MinusCalc class:
public class MinusCalc extends Calc {
@Override
public int func(){
return n1 - n2 ;
}
public MinusCalc(int n1, int n2) {
super(n1, n2);
}
}
And this is PlusCalc class:
public class PlusCalc extends Calc {
@Override
public int func(){
return n1 + n2;
}
public PlusCalc(int n1, int n2) {
super(n1, n2);
}
}
And this is Calc class:
public abstract class Calc {
public Calc(int n1, int n2) { // constructor with parameters!!
this.n1 = n1;
this.n2 = n2;
}
int n1,n2;
public abstract int func();
}