Question: Suppose you want to add a constructor to Comp1ex that takes a double value as its argument and creates a Comp1ex number with that value

Suppose you want to add a constructor to Comp1ex that takes a double value as its argument and creates a Comp1ex number with that value as the real part (and no imaginary part). You write the following code:

public void Complex (double real) { } re = real; im =

But then the statement Comp1ex c = new Comp1ex(1.0); does not compile. Why? Solution: Constructors do not have return types, not even void. This code defines a method named Comp1ex, not a constructor. Remove the keyword void.

public void Complex (double real) { } re = real; im = 0.0;

Step by Step Solution

3.40 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The provided code snippet is attempting to define a constructor for a class called Comp1ex that init... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithm Design Questions!