Max constructors

  • 2
Your mentors at the secret CodeGym center have tried to prepare a Circle class for you. But this circle needs to be improved, and that is your task. Study the Circle class and, without changing anything else about the class, write the maximum number of possible constructors with different parameters. And don't forget about the default constructor.
You can't complete this task, because you're not signed in.
Comments(4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
David HainesLevel 7 , La Crosse
18 June, 21:33
Struggled with this one for quite a while. Finally got it to work after reading all of the help questions and answers and comparing to mine. THANK YOU to everybody that contributed.
Peter KaranyiLevel 9 , Cowplain
24 May, 14:32
PART 1: 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!
Peter KaranyiLevel 9 , Cowplain
24 May, 14:27
PART 2: 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.
17 July 2019, 00:42
Not sure what I'm doing wrong here, but my code won't even compile. What's up? Thanks ahead of time!