• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded Constructor  RSS feed

 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know what this means?
Create overloaded constructors for Parts, using the this() call to the full argument constructor for any constructor that has fewer than four arguments. Then create a PartTest program that does basic testing on the Part object.  Call constructors, print out members with accessors.
I think I did the second part correctly but I'm not sure about the first part. Here is my code



*******************************************************************************
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did part of the second part. You can't do the whole thing until you do part 1.

For part 1, can you make this code compile? How about with two params or one param?
Part partObj = new Part("Pen", "0B23F345", "AFT06");
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You did part of the second part. You can't do the whole thing until you do part 1.

For part 1, can you make this code compile? How about with two params or one param?
Part partObj = new Part("Pen", "0B23F345", "AFT06");



It compiles but it doesn't run. I'm not sure where I'm supposed to add .this()
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I do this?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Part2 is not the name of your class. Remember that the constructor name must match the name of the class. Also keep in mind data types so you don't mix up an int and a String.

You want a constructor with less parameters.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:No. Part2 is not the name of your class. Remember that the constructor name must match the name of the class. Also keep in mind data types so you don't mix up an int and a String.

You want a constructor with less parameters.


I'm confused. So you're saying that my default constructor has to have less parameters?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shal Lango wrote:I'm confused. So you're saying that my default constructor has to have less parameters?

No. I'm saying you need to have more than one constructor. That's what overloading is. Having more than one (with different parameters or a different number of parameters)
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Shal Lango wrote:I'm confused. So you're saying that my default constructor has to have less parameters?

No. I'm saying you need to have more than one constructor. That's what overloading is. Having more than one (with different parameters or a different number of parameters)


How about like this?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting closer. Now you have multiple constructors. However, many of them have one parameter. You need a constructor with 4 params (which you have) and one with 3 params and one with 2 params and one with 1 param and one with zero params.

Also, what is the default for a String? It isn't 0.
 
Shal Lango
Ranch Hand
Posts: 31
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Getting closer. Now you have multiple constructors. However, many of them have one parameter. You need a constructor with 4 params (which you have) and one with 3 params and one with 2 params and one with 1 param and one with zero params.

Also, what is the default for a String? It isn't 0.


Okay, I got it
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect! Good job!

While this isn't wrong, note that Java uses lowercase letters to begin variable names. So Name would be name and Number would be number.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Perfect! Good job!

While this isn't wrong, note that Java uses lowercase letters to begin variable names. So Name would be name and Number would be number.


Gotcha. Thank you!
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shal Lango wrote:. . . Okay, I got it
Are you sure about that? I am pretty sure that this.Part(...); won't compile. What you are doing there is calling the Part method, which you don't appear to have. I am sure that you want this(...); which calls another constructor. Note that this(...): or super(...); must always be the first thing in a constructor, so you cannot use both. Well, just about the first thing.
I am also not happy about the many nulls you are passing. What you are saying with a no‑arguments constructor is, “It is quite all right to have a Part with no name or number or anything.” I am pretty sure that isn't what you want. I think you need to give each Part a name if nothing else. \In which case you won't want a no‑arguments constructor. And consider whether you want null for number. Maybe some default value instead. If you start to let nulls into your object, they can be dangerous because they can produce Exceptions if you try to use them. Consider something like this for your constructor:-If you let nulls into your object, you will have to take special precautions about them. Beware: advanced topic follows. Find a copy of Effective Java by Joshua Bloch and find what he says about using a 0‑element array instead of null.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!