• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass input to other class methods

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to pass input to other class methods. I can hard code a program to work with my classes no problem. I can sort of get things working but when it comes to passing input to other class methods things get hazey. I was hoping for some pointers. I have included my program so far. So on my input from scanner for deposit and withdrawal I would like to pass my input to the methods in credit and debit classes of account or savings or checking. I am including the part of the directions I am having trouble with and yes this is homework and no I am not asking for the answers just help-----

Develop a polymorphic banking application using the Account hierarchy. Create an array of Account references to SavingsAccount and CheckingAccount objects.
For each account in the array, allow the user to specify an amount of money to withdraw from the account using method debit() and an amount to deposit into the account using the method deposit().
As you process each Account, determine its type.
If an account is a SavingsAccount, calculate the amount of interest owed to the Account using method calculateInterest(), then add the interest to the account balance using method credit().
After processing an Account, print the updated balance obtained by using the getBalance() from the base class.
Interactively ask the user for information for each account and then print the result for each account.

-----------------------------Please help...

 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Very first thing. Methods names and variables names suppose to start with the lower case. Only classes, constructors and constants should start with an upper case (constants in fact should be all upper cases). Please fix that part as it is confusing.
Some comments are also redundant as they don't tell anything extra what you couldn't see from code itself. Leave them only in the case you have been told to write such.

jake fowler wrote:I am trying to pass input to other class methods.

Please tell us a bit more, what do you want to pass and to which method?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!!

Well, it looks like there are a number of things about which you're hazy. Before we get to your main question, let's clear up a few things first.

...pass my input to the credit and debit classes of account or savings or checking


debit and credit are not classes, they're methods. Also, you capitalized the names Debit and Credit in your code. By convention, class names, method names, and variables names are written in camel case. Class names start with a capital letter while method and variables names start with a lowercase letter. The names IllegalArgumentException and getBalance are in proper camel case. The letters that are capitalized are the ones that start each word in the name. IllegalArgumentException is a class so it starts with a capital letter. getBalance is a method so it starts with a lowercase letter.

So, the conventional way to write that code for the debit and credit methods is:


Next, this code is very strange:

The only difference between these two is the case of the first letter. This is NOT a good idea. Don't do it. I suspect you were trying to resolve some errors you were getting because of the way you've named your method parameters that are related to these variables. The standard way to code that is this:

Notes:
1) Proper use of camel case convention for interestRate
2) Using this.interestRate to refer to the instance member and differentiate it from the interestRate parameter.
3) Deleted the unnecessary instance member, InterestRate
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
Notes:
1) Proper use of camel case convention for interestRate
2) Using this.interestRate to refer to the instance member and differentiate it from the interestRate parameter.
3) Deleted the unnecessary instance member, InterestRate


I did change all of that thank you
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't quote all post as it makes thread long and difficult to read. Quote only relevant parts which you're refering to. I removed most of the quoted text, I hope that is fine with you
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I'm sorry new to this site
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so now tell us a little bit more about your confusion with passing values to/from different objects that you've created in your program. And by that I mean the objects that you create with the new keyword, as in this code:

Here, you have created one array that can hold Account objects on line 19, and six Account objects, one object on each of lines 21 to 26. That makes a total of nine objects in your program at that point because you also created a Scanner object on line 10 and a DecimalFormat object on line 12.

You can think of your main() method as the "stage" where the "story" of your program is played out. You are the director and your job is to tell these different objects how to work together to do something meaningful. So, you tell the Scanner object named input, "Hey, input, get the next thing that the user enters on the command line and treat is as a double." Only instead of saying that in so many words, you say it like this in "Java-speak": input.nextDouble(). When input does what you asked it to do and gives back the result you asked for, you store that value in a variable, like how you assigned the value of input.nextDouble() on line 32 to the variable you've called depositAmount. If you think of the variable as a box with a label "depositAmount" on it, then the value that the user entered is now inside that box.

Now, you have a few options for what you want to do next with that value in the depositAmount box/variable. You look around and you see that there are a number of cast members in this little play (the objects that you created earlier) standing in the wings, just waiting for you to tell them to do something so they can come on stage and get in on this little performance that you're orchestrating.

What do you want to do next? How does this "story" play out on the stage? You're the director, you decide.

Does it make sense to think about your program like this?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes I'm sorry new to this site


That's okay. On some sites (especially mail lists) it is de rigeur to quote all of the previous post. But on most forums it is unnecessary.
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu Lacar

I have tried everything I can think of. I tried casting and no dice. I want to take the box you speak of and add it to my balance and figure it into the interest rate. I would really like to get a deposit amount and a withdrawal amount add into the balance and then figure interest. I am supposed to pull the getBalance from the base class also?? Not really sure how to accomplish that.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jake, when you say "add it to my balance" think about who exactly "my" is there. Think objects. You don't actually have anything. The objects in the program has the information. So to which object do you want to give the value that needs to be added to its balance? That object is the one you need to tell, "Hey, you, here's a value, add it to your balance." Except again, you don't say it in those words but in "Java-speak", which translates to calling some method that the object has that will make it do what you want it to do. Just like the way the Scanner object named input has a nextDouble() method, what method do you want to call?

And when you say you "tried everything" you have to show us the code that you tried that didn't work so we know exactly what it was that you tried to do and failed. We can't help you much if you're not more specific in describing your problem.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And remember, Java is a case-sensitive language. If you name something calculateInterest and try to refer to it as CalculateInterest with a capital 'C', that would be wrong. You have to use the exact name that you declared.
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed it. I'm going nuts sorry
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So right now I'm just trying to work on getting the interest to show up correctly. I have a set balance and I am asking for a deposit I would like to add the balance and deposit together and figure interest. Why is my answer coming up empty???

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the variable sa referring to? Where's your code that sets that to something?
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




If at all possible is there an example you can point me to?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're losing track of who's who in your program. As I explained earlier, the objects that you create are like the cast members in a play.

Look at line 9. That's one object you're creating there. It's a SavingsAccount object. Fine. In you're program, you refer to this object as acts[0]

Now, look at line 16. That's another savings account object. It's a different object from the one you created earlier on line 9. You are referring to this new SavingsAccount object as sa.

Now on line 21, which object are you calling the credit() method on?

And on line 23, which object are you calling the calculateInterest() method on?

What's happened there is that you've gotten yourself confused and thought you were talking to one object when in reality you're talking to another object. When you clear up that confusion and refer to the objects by their correct names, you'll fix your problem.
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


GOT IT!!! I think... Please proof read and make sure I'm not seeing things. I would like to move on!!
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks fine.

One more thing:

This code could be simplified as

Or you could just use println:

And it would be even cleaner if you used printf instead:

Here's a nice little cheat sheet for printf: https://www.cs.colostate.edu/~cs160/.Spring16/resources/Java_printf_method_quick_reference.pdf
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all of the Help!!

I am getting an error in thread "main" java.util.MissingFormatArgumentException: Format specifier '% o'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Test2.main(Test2.java:33)

When using your last example??
Also I am able to per directions ----After processing an Account, print the updated balance obtained by using the getBalance() from the base class.

with the way I have written it?? Again Thank you!!!
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jake fowler wrote:
I am getting an error in thread "main" java.util.MissingFormatArgumentException: Format specifier '% o'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Test2.main(Test2.java:33)


It should be:

The % sign after ".2f" was what was causing the exception. I had the %n there initially but subsequently edited the format string to include the the "of Interest ..." stuff. Just missed deleting that wayward % sign. My bad.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to find out is to try it yourself and see what happens. Now that you understand that you need to correctly address objects using the references that you've assigned to them, then it's just a matter of figuring out which one to call and when to call it.
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WORKS GREAT!!! Can you point out why my balance is 5 dollars off??

 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My math was off it's balanced correctly
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you going to do with the other accounts which are stored in an array at indices 1, 2, 3, 4, and 5?
I hope you're not going to copy/paste the same routine which starts at line 26 and ends at line 39 five times by changing account numbers accordingly?
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't want to but I don't know any other way?? What do you suggest??? Your help would be greatly appreciated!!
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have tried to create a loop to go through my array so I can clean up my program. I have run into a few problems. 1---the current code uses a loop but has the same amount of lines to do 6 account and that's a lot of lines (12*6)..2---- I can remove the print lines but then that turns the loop into a method and I had trouble printing that. Could someone please give me a few suggestions on how to successfully print this array without 100 lines of code?? I would really appreciate some help. Thank you!!!



 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should be replaced with
You were missing opening brace.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 33:

Should have given a compiler error, you already have a variable 'n' in the for() loop. In any event, you don't want to change the value of the for() loop's n.

Edit: guess you didn't get an error because of the missing '{'.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
needs to display an account number based on 'n', like:
ditto for your other prints.

P.S. When you add the missing '{' you'll need to add a '}' after the block (line 40).
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much!! So I would have to create another loop for the checking account correct?? Once the loop hits the Checking account part of the array it errors out which makes sense.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Obviously, this doesn't work for CheckingAccount's. The cast will fail.
With today's banking incentives it is possible that even CheckingAccount's could potentially have interest. If you define a method calculateInterest() in the Account class and have the method do nothing, then you don't need the cast here because SavingsAccount will have its own calculateInterest() method, and CheckingAccount (not having one of those) will default to using the one in Account.
 
jake fowler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it to work!!! Thank you for helping me with the last piece of my puzzle. I used 2 loops 1 for savings another for checking!! Thank you Thank you Thank you!!!
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to be of help.

I do think you should consider my last post. Your loop shouldn't have to know which flavor of Account you are operating on. By defining calculateInterest() in the Account class to just return 0, you are implementing polymorphism, one of the key feature of object oriented programming. In this way one loop should do it. Anyway, good luck.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic