3

For my assignment I am to create 5 Classes in a hierarchy that extends Number. I am to create methods of addition subtraction multiplication and devision for each of the classes that has two parameters and a return type of the class.

The method grammar is the same for each class. They all cause crashes in the interactions pane of Dr. Java and return no errors.

How do I resolve this?

public class ComplexN extends Number{
  private double value1;
  private double value2;

  public ComplexN(double real, double imaginary){
    this.value1=real;
    this.value2=imaginary;
  }

  public double getRealPart(){
    return value1;
  }

  public double getImaginaryPart(){
    return value2;
  }


  public static ComplexN add(ComplexN a, ComplexN b){
    ComplexN sum = new ComplexN((a.getRealPart()+b.getRealPart()),(a.getImaginaryPart()+b.getImaginaryPart()));
    return sum;
  }
}
Ocasta Eshu
  • 841
  • 3
  • 11
  • 23
  • If the IDE is crashing, then it's a bug in the IDE, not a problem with your code. – Oliver Charlesworth Nov 08 '11 at 23:48
  • Write a simple test that you can run from the command line. Then you will at least have a stack trace and message. – digitaljoel Nov 08 '11 at 23:48
  • I think the issue is in the way I am calling add(). a friend took a look at the code and was able to call it successfully but hes gone now... I was calling by a.add(a,b) or by c.add(a,b) [a b and c are instantiated ComplexN's] – Ocasta Eshu Nov 09 '11 at 00:43
  • @OliCharlesworth: you were correct, I contacted my professor about the issue and he stated that there is a compatibility bug with Dr. Java and Java 6.0 causing a crash when you try to print the value of an Object without toString() – Ocasta Eshu Nov 09 '11 at 00:47

1 Answers1

2

After consulting with my Professor I have determined the problem.

Dr. Java has a compatibility issue with Java 6.0 that causes a security error when you print the value of an object without explicitly using toString().

so using:

(a.add(a,b)).toString() 

instead of:

a.add(a,b)

or similarly:

a.toString()

instead of:

a

...will solve the problem in the short run. Also having the class extend Object instead of Number will patch this bug.

Ocasta Eshu
  • 841
  • 3
  • 11
  • 23