Mine is working finally!
Based upon the requirements, first I thought there must be 1 default constructor plus 4 other which is 5 in total.
That is not the case! The default constructor is included in the 4 required constructors!
So the solution is that you need:
- default constructor
- constructor with 1 argument
- constructor with 2 arguments
- constructor with 3 arguments
YOU ARE ASKING WHY?
I experimented with the instance variables to figure out why this is the case.
It turned out, that the problem was that all instance variables were the same type.
Therefore if you make 2 constructors:
public Circle(double x, double y){ AND public Circle(double y, double x){
this.x = x; this.y = y;
this.y = y; this.x = x;
} }
when you instanciate the Circle (making a Circle object)
Circle first = new Circle(5, 6);
The Compiler has no idea if the 5 stands for x or y and the same with the 6 because you made 2 constructors where they switch each other.
That is why if all the instance variables have the same type, you cannot make constructors with the same
amount of arguments!
BUT
If you have for example an instance variable String x, and double y, you CAN make two constructors with 2 arguments:
public Circle(String x, double y){ AND public Circle(double y, String x){
this.x = x; this.y = y;
this.y = y; this.x = x;
} }
because when you make a Circle object
Circle first = new Circle("Red", 6);
Circle second = new Circle(6, "Red");
It is obvious for the Compiler that a String is not a double, so you can switch them if you made 2 constructors.
AND
this leads to if you have 3 instance variables where 2 share the same type, because of the first example, you can make constructors like:
String x;
double y;
double z;
(String x, double y) but then you cannot have a (String x, double z)
(String x, double y, double z) but then you cannot have a (String x, double z, double y)
(double y, String x, double z) but then you cannot have a (double z, String x, double y)
etc.
I really recommend to use IntelliJ IDEA for all the tasks, because I just realized what was wrong in my code because IntelliJ always shows where the problem is and I tried and tried again
to fix it until finally my code compiled. Then I got curious what is the case with different variable types, so I experimented a little and this is how I got to this result.
THIS IS A HELP TO UNDERSTAND HOW TO SOLVE THIS TASK
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Sara Ferré
19 August 2020, 16:14
Thank you so much for this explanation!!!!
0
Lawson
31 July 2020, 18:25
Thanks very much...it worked
0
Sebastian
25 June 2020, 03:44
Thank you
0
Gellert Varga
24 May 2020, 18:30
Hello! I like what you wrote:)
but if you are a Hungarian (i think, by your name), let's switch to our mother tongue, it's much easier to understand each other:)
But if I'm wrong, I'm sorry!
0