The moose likes Beginning Java and the fly likes SubClass Blues! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SubClass Blues!" Watch "SubClass Blues!" New topic

SubClass Blues!

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Hi Everyone,
I'm still working on my subclass assigment and only getting more confused from my last posted question. So I'm going to try again cause I need help badly. In my first assigment I had to write a class named "Account" to model accounts, then a client program to test the account class. I did this all in one file as you will see below. Now I'm being asked to use that program and write two subclasses "Savings Account" and "Chequing Account" and test programs to go with to test them. The difference of the two accounts is that the chequing account has an overdraft allowance and the savings account does not so, if the user withdraws too much "Insufficient" funds will display.
So here is my problem, I'm working on my savings account subclass and I'm lost. In my original account class the user didn't input the amounts to withdraw or deposit, so now I would like to change that and have the user input it. Is this possible without changing the Account class but modifying things in the subclasses and testprograms? Can someone please let me know cause I don't want to have to change everything in my account class. I would like to work with what I have already written so I can learn from it. So if you can help my account class code and the beginning of my Savings Account subclass code are below. Thanks in advance.

Below is my SavingsAccount subclass:

As you can see I'm desperate .
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
I would like to change that and have the user input it. Is this possible without changing the Account class but modifying things in the subclasses and testprograms?
Yes. You can make some small changes to your TestAccountClass class to change it to query the user for the values that it is currently using.
You'll likely want to get input using the system console or a simple pop-up input dialog, such as appears when using JOptionPane.showInputDialog(). I'd recommend the JOptionPane as it's a little bit easier to implement.
Have you used a JOptionPane to get input before?
[ February 25, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
I have used JOptionPane before. So what your saying is I need to change my TestAccountClass for the user to input then move one to my savings and chequing account subclasses or in the test programs for the subclasses can I have the user input?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
That is what I would suggest, too. The Account class (and it's subclasses) don't "know" where the data comes from for the amount to deposit or withdraw. They are passed as parameters to the appropriate functions. This is a clean design since you can easily change the source of the data by changing your test classes. You can probably imagine different ways to obtain the data such as user input, a file, or a database. In each case you could just write a different test class that gets the data from the appropriate source.

Java API Documentation
The Java Tutorial
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Okay so I've established that I can change to user input in my test programs. I don't want to change my TestAccountClass, it's fine the way it is, I have to write a TestSavingsAccount program but my first steps are to get the subclass right. That is what I need help with right now. Can anyone help me out I'm lost as my text doesn't eplain anything when it comes to doing user input. I need some direction. Please help!!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
You have a class (Account), which is a base class, and you want to create
another class (SavingsAccount), derived from the base class.
It seems to me you're looking at this from the wrong angle. A bit like
having a cake recipe, and a chocolate cake recipe - you seem to be using
the cake recipe to bake the cake, then trying to turn it into a chocolate
cake, instead of using the chocolate cake recipe (does this make sense?)
Here's a modified version of your code, hopefully this might make more sense.
(needs error handling)
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Stacey Johnson:
I'm lost as my text doesn't eplain anything when it comes to doing user input.

As a side-note, I often find it necessary when learning something new that it helps to have multiple resources to leverage. It just helps me to learn to see the same concept presented in slightly different ways. Each resource helps reinforce what the other is saying, and fills in any gaps, like the one you are mentioning your textbook has. However since you are a student, I know that money may not be in great abundance (been there, done that ). Fortunately there are some good free online resources you can leverage (like JavaRanch). Three of my other favorites (which are more reference based) are:
  • www.JavaAlmanac.com � this site has tons of little sample code snippets/examples that are well indexed by package and searchable.
  • Sun�s The Java Tutorial � it has some nice introductory lessons (with downloadable examples) on most topics � you can either view it online or download a copy and unzip it on your PC (great for a laptop when you don�t have internet access).
  • Thinking in Java � Available in print as well in a free downloadable electronic version.

  • Some other ones I have bookmarked, but have only used on occasion are:
  • Interactive Programming In Java
  • Introduction to Computer Science using Java
  • Introduction to Programming Using Java
  • McGill University School of Computer Science CS203A

  • The first two of those four are pretty robust and detailed; the last one presents some short treaties on some core concepts, so it can work well as a quick "second point of view".
    Lastly, I've found JDK 1.4 Tutorial by Gregory M. Travis to be a good book, and you can purchase an electronic copy (pdf) at the publisher�s website for only $17.50.
    I hope that some of these can help you as you progress in your studies (plus they�re great reading for those boring Sunday Afternoons when you�re doing laundry )
    It is sorta covered in the JavaRanch Style Guide.
    subject: SubClass Blues!
    Similar Threads
    Help on testprogram and subclass please
    Help code problem please
    Having trouble understanding an error code i keep getting. Please help!
    Very Confused!!
    Ready to give up!