aspose file tools*
The moose likes Beginning Java and the fly likes Advanced Inheritance. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Advanced Inheritance." Watch "Advanced Inheritance." New topic
Author

Advanced Inheritance.

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22
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.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

UseCodeTags (<---click)


OCA7
Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Oct 30, 2012
Posts: 22
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

Joined: Oct 30, 2012
Posts: 22
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
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

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


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Sean Heise
    Greenhorn

    Joined: Oct 30, 2012
    Posts: 22
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38340
        
      23
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Advanced Inheritance.