• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Advanced Inheritance.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have an assignment that was due yesterday for my Java class (I know I'm slacking, but Trig seemed more important as I have a test next week and it's kicking the crap out of me at the moment).
The assignment is as follows:


So far I've got:


My main questions are on steps:

1d, 2b, 3d, these are for the constructors which I must have one in each class except the AccountArray class. I believe I've set them up correctly, but I'm not 100% sure. I know I should know this, but I guess I'm just not seeing how it ties into the rest of the program.

also on 1e, 2a, 3c... which is creating the toString method overrides again, I'm not quite sure if I'm doing this correctly (first time I've learned about or utilized them.) Being that the output is supposed to look like this:

Checking
Account Number: 100
Account Balance: 1000.0
Interest Earned: 18.0

I imagine I should have \n in there between each line I want to create?

I'm also having problems with the set methods for some reason. I can't for the life of me think of what I should have in their bodies, being that the program is set up the way it is.


Aaaaaand lastly basically the entire last class I'm not quite sure what I should do. I've got the array set up for ten place holders but other than that I'm not quite sure what I should do next. Should I instantiate a checking and savings for each place holder? So checking would take the first five place holders and savings would take the last five? Then how do I work in the constructors?

Anyway I guess I'm going to keep plugging away at it.

Any help would be appreciated. As I've written this, I think I've come up with an idea for figuring out how to put together the last class. We'll see I suppose. Thanks in advance.

Edit: As I said I think I might have gotten the last class down, although I do still need some help. I just want to make sure that everything is correct.
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags (<---click)
 
Sean Heise
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enkita mody wrote:UseCodeTags (<---click)



Yea, sorry about that, I'm used to being on another forum and accidently used the incorrect tag for it. But I fixed it.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Heise wrote:
1d, 2b, 3d, these are for the constructors which I must have one in each class except the AccountArray class. I believe I've set them up correctly, but I'm not 100% sure. I know I should know this, but I guess I'm just not seeing how it ties into the rest of the program.



IMHO, that is a big issue with these "color by numbers" homework assignments. You know what you are doing, but in many cases, you don't know *why* you are doing it.

For this question, one way to think about it is -- a constructor is used to initialize an instance as it is being instantiated. You need to think about (1) what the parameters are for, in order to enable the caller to pass you configuration details, and (2) what it needs to implement that initialization, including all the components of the class, and its super class. Hopefully, this helps, but if not, then you need to step back, look at the instructions again, and try to see what it is doing -- try to see the "big picture" from the "color by numbers" instructions.

Henry
 
Marshal
Posts: 80493
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another problem about what Henry calls “colour by numbers” is that you don’t know when something would be better done otherwise. Because you are told to use protected fields, for example, you never learn how much better private fields would have been.
 
Sean Heise
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's exactly how I feel. I've never used constructors this way.

But I think I've figured it out.
However, the balance is now what I'm pondering, how it ties into the rest of the program.

Here you see I've set up my array of ten account objects and have instantiated new objects of each class and used their constructor in order to fill in the information as per the instructions.


So I've got my five Checking and five savings as per the assignment instructions. But now I'm supposed to work in the balance of 1000 - 10000, I'm not seeing where this is supposed to be input. The only constructor that asks for a balance is the Account class. Being that the Account class is the super and checking and savings are both subclasses, I thought perhaps I'd be able to input the balance directly into the constructors being that the constructor would have been inherited (so maybe that the constructors for savings/checking might have inherited the spot for balance into their own constructors), but this isn't the case.

I really kind of wish we could get more practice. We're able to do the "you do it" portions of the text book, but being that this is simply a "copy it out of the book" type of practice it doesn't really help me. There are vague descriptions that go along with it that I try to decipher, but don't usually get very far with them.
 
Sean Heise
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Another problem about what Henry calls “colour by numbers” is that you don’t know when something would be better done otherwise. Because you are told to use protected fields, for example, you never learn how much better private fields would have been.



This is also true, however we do go over them in class.
But you're correct, not just for field modifiers. I understand that in inheritance you can't use private variables/methods in any class other than the class that it's called in. And for protected variables/methods you can't use them outside of the super/subclasses... etc.
I atleast understand some principles.

I kind of wish we'd have multiple little programs that would work up to the final weekly program, so as to drill home the principles used in each chapter.
 
Campbell Ritchie
Marshal
Posts: 80493
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ask your instructor what protected means. A lot of people thing it means accessible in the class and its subclasses, but that is not correct.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Heise wrote:Here you see I've set up my array of ten account objects


Just FYI, you can save yourself a bit of typing by declaring and setting your array all in one go, viz:

So I've got my five Checking and five savings as per the assignment instructions. But now I'm supposed to work in the balance of 1000 - 10000, I'm not seeing where this is supposed to be input.


Well, the normal way would be to use your setter method, eg something like:
refAccount[3].setBalance(3500.0);

I really kind of wish we could get more practice. We're able to do the "you do it" portions of the text book, but being that this is simply a "copy it out of the book" type of practice it doesn't really help me.


And this is what Henry was talking about. Programming is NOT simple, and you can only learn so much by rote.

My advice: StopCoding (←click), and think about what a Bank Account actually is. All banks have them; and they probably have the different varieties your exercise is trying to model (eg Checking, Savings etc.).
  • What do they all have in common? (a 'balance' sounds likely; quite possibly a 'customer' or 'owner' as well)
  • What DON'T they have in common? (for example: does a Checking account pay interest?)
  • Writing down this stuff will probably help you to sort out what the exercise is asking you to do.

    Lastly, you might want to look at the tutorials (and specifically this chapter) to see how constructors work.

    The main thing to know is that when you have a hierarchy of classes, ALL of them get called; so your Savings class constructor will need to call the Account constructor, which in turn calls Object's constructor (remember: ALL classes extend Object, even if you don't explicitly state it); and furthermore, the call to the superclass constructor must be the first statement in a constructor.
    If you don't put it in yourself, the compiler will add a call to super() for you, which may NOT be what you want (and in your case, it probably isn't).

    HIH

    Winston
     
    Sean Heise
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, so I think I've gotten it down to the very last step (I think, and hope lol), here's what I've gotten so far.





    from what I'm gathering from the instructions as I re-read them. I should be using the first for loop to instantiate five objects of the savings class and five objects of the checking class. I've probably gone about it in an inefficient manner, but it's what I've got so far.

    My last step is what's blocking me at the moment. I'm not quite sure how to access the toString() methods in each of the other classes to get them to run and display in the for loop. Can anyone assist me, I greatly appreciate it.

    Also, if you read the instructions and notice something amiss, I'd greatly appreciate that too.
     
    Campbell Ritchie
    Marshal
    Posts: 80493
    455
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry for the delay in replying.

    Sean Heise wrote: . . . I understand that in inheritance you can't use private variables/methods in any class other than the class that it's called in.

    If you have a well‑designed class with toString, equals, hashCode, getXXX and maybe setXXX methods, then the fields can be as private and inaccessible as you like.

    Because you don’t need any more access. Those methods provide everything you need.

    If you are not adding any fields, you can often create a subclass which contains a constructor and nothing else.

    And for protected variables/methods you can't use them outside of the super/subclasses...

    That is exactly the same mistake I told you to check about earlier. Actually you can’t use a protected member of a class in its superclasses.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic