Reputation: 13
So im trying to make a simple calc but having some problems Error message :Multiple markers at this line - No enclosing instance of type Application is accessible. Must qualify the allocation with an enclosing instance of type Application (e.g. x.new A() where x is an instance of Application). - Line breakpoint:Application [line: 64] - main(String[])
What am i doing wrong? Could anyone see something wrong and fix it plz?
public class Application {
interface MathOp {
public double doMath(double a, double b);
}
class Add implements MathOp{
public double doMath(double a, double b) {
return (a + b);
}
}
class Sub implements MathOp{
public double doMath(double a, double b) {
return (a - b);
}
}
class Div implements MathOp{
public double doMath(double a, double b){
return (a / b);
}
}
class Mul implements MathOp{
public double doMath(double a, double b){
return (a * b);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a,b,c;
int choice = 0;
a=b=c=0.0;
while(true) {
System.out.println("Enter two numbers");
a = Double.parseDouble(sc.nextLine());
b = Double.parseDouble(sc.nextLine());
System.out.println("Enter your choice");
System.out.println("1. Add");
System.out.println("2. Sub");
System.out.println("3. Mul");
System.out.println("4. Div");
choice = Integer.parseInt(sc.nextLine());
switch(choice) {
case 1 :
c = new Add().doMath(a,b);
break;
case 2 :
c = new Sub().doMath(a,b);
break;
case 3 :
c = new Div().doMath(a,b);
break;
case 4 :
c = new Mul().doMath(a,b);
break;
default:
break;
}
System.out.println(c + "this is the answere");
System.out.println("would you like to continue? (Y/N)");
if("N".equalsIgnoreCase(sc.nextLine())) { // careful with the paranthesis
break;
}
}
}
Upvotes: 0
Views: 346
Reputation: 1500485
The problem is that your Add
, Sub
etc classes are inner classes, which means you have to have an instance of the containing class... which you don't at this point.
Options:
(Best!) Get rid of your interface, instead creating an enum
(at the top level; not nested) which has one member for each operation. The enum would declare an abstract method, which would then be overridden in each member:
public enum MathOperation {
ADDITION {
@Override public double doMath(double a, double b) {
return a + b;
}
},
SUBTRACTION {
@Override public double doMath(double a, double b) {
return a - b;
}
}
// etc
;
public abstract double doMath(double a, double b);
}
Application
class. Nested types should be pretty rare, IMO.static
modifier to each of the implementation classes. They'd still be nested, but they wouldn't require an implicit reference to an instance of Application
.Application
so that you can create an instance of your operation classes. Don't do this. I'm only including it for completeness.While nested classes can certainly be useful, if you're relative newcomer to Java I suggest you avoid them for now. They come with their own rules and syntax which can be a little odd to start with. Stick with declaring all your types at the top level instead.
Upvotes: 2