Java acm ComplexNumber Class cannot be instantiated?

103 views Asked by At

Can someone help me find out why my code doesn't work? I litterally have no clue why it won't work. It says java.lang.InstantiationException and every post i search in the internet tells me that abstract classes cannot be instantiated, but this is not an abstract class or am i wrong?

import acm.program.ConsoleProgram;

public class ComplexNumber extends ConsoleProgram{

    double re;
    double im;
    double a1;
    double a2;
    double b1;
    double b2;

    public ComplexNumber(double real, double imaginary) {
        double re = real;
        double im = imaginary;
    }

public ComplexNumber(ComplexNumber cn) {
    double re = getReal();
    double im = getImaginary();
}

    private double getReal() {
        return re;
    }

    private double getImaginary() {
        return im;
    }

    public String toString() {
        return "" + re + " " + im + "*i";
    }

    private ComplexNumber add(ComplexNumber cn2) {
        a1 = re;
        a2 = cn2.getReal();
        b1 = im;
        b2 = cn2.getImaginary();
        return new ComplexNumber(a1+a2,b1+b2);
    }

    @Override
    public void run() {
        ComplexNumber cn1 = new ComplexNumber(1.0, 2.0);
        ComplexNumber cn2 = new ComplexNumber(3.0, 4.0);
        cn1.add(cn2).toString();
    }
}

I would really appreciate it, if somebody could help me out.

java.lang.InstantiationException: ComplexNumber Laden: ComplexNumber.class kann nicht instanziiert werden. 
  at java.lang.Class.newInstance(Unknown Source) 
  at sun.applet.AppletPanel.createApplet(Unknown Source) 
  at sun.applet.AppletPanel.runLoader(Unknown Source) 
  at sun.applet.AppletPanel.run(Unknown Source) 
  at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NoSuchMethodException: ComplexNumber.<init>() 
  at java.lang.Class.getConstructor0(Unknown Source) ... 5 mor
2

There are 2 answers

2
George Z. On

Inside your constructor (both of them) you declare new variables which are local. This means, that the field called "re" does not get any value, so, the getter will try to return a non instantiated field.

Check this class, with the proper field/variable declaration (Some extra comments inside the code :) )

public class ComplexNumber extends ConsoleProgram {
    double re;
    double im;

    public ComplexNumber(double real, double imaginary) {
        re = real;
        im = imaginary;
        // double re = real will declare new local variable. So the field
        // this.re will never get a value.
    }

    public ComplexNumber(ComplexNumber cn) {
        // Alternative constructor will use the original constructor.
        this(cn.getReal(), cn.getImaginary());
    }

    private double getReal() {
        return re;
    }

    private double getImaginary() {
        return im;
    }

    public String toString() {
        return "" + re + " " + im + "*i";
    }

    private ComplexNumber add(ComplexNumber cn2) {
        // a1,a2,b1,b2 variables can be local, no need to be fields.
        double a1 = re;
        double a2 = cn2.getReal();
        double b1 = im;
        double b2 = cn2.getImaginary();
        return new ComplexNumber(a1 + a2, b1 + b2);
    }

    @Override
    public void run() {
        ComplexNumber cn1 = new ComplexNumber(1.0, 2.0);
        ComplexNumber cn2 = new ComplexNumber(3.0, 4.0);
        cn1.add(cn2).toString();
    }
}
0
Dawood ibn Kareem On

The issue seems to be that you are using this class as an applet. Your browser (or whatever other applet framework your using) is trying to instantiate a ComplexNumber without passing any arguments to the constructor. But your ComplexNumber class does not have a suitable constructor - you've got one version with two double parameters and another with a ComplexNumber parameter. But you'll need one with no parameters at all. Maybe something like

public ComplexNumber() {
    re = 0.0;
    im = 0.0;
}

You should also make the fixes suggested in George Zougianos' answer - without them, your class is still broken.