• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble creating class objects using an array  RSS feed

 
Shawn Gordian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if I worded my question correctly, but here it is. Here is what I was asked to do.

Create an array of 8 HospitalEmployees – then assign the references to 2 each (using both constructors) of the Doctor, Nurse, Surgeon, and Administrator class objects.
Use a loop to issue each one’s paycheck – observe the use of the overridden method in code


Here is my code so far. The project ask me to create instance of three other classes, but for right now I just added the Nurse class. Once I learn how to do it I can use that for the other three classes. Any help , advice, or nudges are welcome.





 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You say that you need 8 employees, but this is an array of size 2.

Also, I think you misunderstood the part about filling the array. The way you're doing it, you're making a new, anonymous class for every employee added, which rather defeats the purpose of creating those other classes (Nurse, etc). Instead, you can do this:

This is a perfectly valid statement since a Nurse is-an employee. Since the nurse's constructor calls super, it will do all the associated stuff with that Scanner reading input in.
 
Shawn Gordian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:
You say that you need 8 employees, but this is an array of size 2.


Yeah I had it set to 2 while playing with it, just to try to get it to run before I tried 8.

Also, I think you misunderstood the part about filling the array. The way you're doing it, you're making a new, anonymous class for every employee added, which rather defeats the purpose of creating those other classes (Nurse, etc). Instead, you can do this:

This is a perfectly valid statement since a Nurse is-an employee. Since the nurse's constructor calls super, it will do all the associated stuff with that Scanner reading input in.



Ah gotcha. Yeah that was part of the problem. I wasnt sure what the questions was asking me to do. Ill give this a shot for the classes when I get back to my PC thanks.
 
Shawn Gordian
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I decided to go with. Thanks again!

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an easier way t do that, which also obviates any risk of empty places in your array or overrunning the bounds of the array. I have done that by deleting parts of your code:-That is of course called an array initialiser. You will have your eight elements as long as you counted them correctly.
Why did you use two loops; you could have done both printouts in the same loop? Why are you not using both versions of the constructor, as it said in your first post?
 
Shawn Gordian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is an easier way t do that, which also obviates any risk of empty places in your array or overrunning the bounds of the array. I have done that by deleting parts of your code:-That is of course called an array initialiser. You will have your eight elements as long as you counted them correctly.
Why did you use two loops; you could have done both printouts in the same loop? Why are you not using both versions of the constructor, as it said in your first post?



Array initialiser? Dont think we've learned that yet. I put the paystubs in a different loop cause I have to do a little more with them and right now it's just easier if I have them separate. I dont understand what she means by both constructors. I dont understand how I can use the default one then use a conversion one. Wouldnt the conversion overwrite everything anyway since I am passing it information?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have any default constructors, because you wrote constructors in all classes. You may have no‑arguments constructors, but they are different from default constructors.
I think you are supposed to writeAt least that is how I interpreted what you said about both constructors in the first post.
 
Shawn Gordian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You don't have any default constructors, because you wrote constructors in all classes. You may have no‑arguments constructors, but they are different from default constructors.
I think you are supposed to writeAt least that is how I interpreted what you said about both constructors in the first post.


Oh boy thanks. So this whole time I've been reading that as make one doctor object and use both constructors on the one. Not make one doctor using one constructor, then make the other one using the other constructor. Wow. I've been sitting here trying to figure out if its possible to use both the constructors on one doctor.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn Gordian wrote:. . . Wow. I've been sitting here trying to figure out if its possible to use both the constructors on one doctor.
The only way you can call two constructors on the same object is if constructor 1 calls constructor 2 with the this(...); construct.

And, you're welcome
 
Shawn Gordian
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here we go now. Cant believe I was misinterpreting that question for hours. This looks more like it.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use \n unless you have been asked for a specific line end character, even though you see it in some books. Don't write print([line end]); Write println();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!