• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Help Calculating The Balance Of 3 Variables  RSS feed

 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys i am in need of some desperate help with a program i'm trying to create. Basically it's a program that has 3 options. Selecting "A" will prompt the user for 3 values. An Initial Deposit Amount, Rate of interest per year and Amount to be deposited each month.

Once the user has supplied the initial deposit amount and interest rate for the account as well as the monthly deposit amount, the program should then proceed to calculate the new balance for the account including interest earned over a six month period.

How would i do the calculation for the initial deposit amount, monthly deposit amount and new balance for the account including interest? I'm new to java programming so i'm still a bit unsure. Any help would be greatly appreciated. Here is my program code,

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to not know WHAT you want to do. First figure out WHAT do you want to do. Only them figure out HOW you will do this.
Write on paper (in English or any other language you like) steps that your program needs to take to calculate what you need.
See those links:
StopCoding
WhatNotHow
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:How would i do the calculation for the initial deposit amount, monthly deposit amount and new balance for the account including interest? I'm new to java programming so i'm still a bit unsure.

Do you know the formula for calculating the new balance and just need to know how to implement that in Java ? Or are you asking us to tell you the formula ?

If the former, then you need to tell us what formula you are using, because there are a number of ways it could be done.
If the latter, then you should really ask your teacher because there are a number of ways it could be done so you need to find out which one they are expecting you to use.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what i have to do i'm just unsure on how to code it. I just need some help on how to start coding it are you able to give me the code for the first month? I'm sure once i know that i can apply it to the other months.

cheers.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Johnnoo Williams wrote:How would i do the calculation for the initial deposit amount, monthly deposit amount and new balance for the account including interest? I'm new to java programming so i'm still a bit unsure.

Do you know the formula for calculating the new balance and just need to know how to implement that in Java ? Or are you asking us to tell you the formula ?

If the former, then you need to tell us what formula you are using, because there are a number of ways it could be done.
If the latter, then you should really ask your teacher because there are a number of ways it could be done so you need to find out which one they are expecting you to use.


balance = balance + monthly deposit amount
interest = balance (current) * ((interest rate / 12) / 100)
balance (new) = balance (current) + interest

That's the formula i just need to know how i would implement that in Java.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone able to help me with this? Please?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Anyone able to help me with this? Please?

We are helping.
Please tell us whether:
- you know how you want to calculate balance but don't know how to code it in Java (then ShowSomeEffort, show us what you tried and what you don't understand)
- you dont know how to calculate balance at all (in this case StopCoding and write it on paper first)

EDIT:
Sorry, I didn't notice you know the formula already.
Those are simple arithmetic operations. Read this tutorial: link
If you don't understand something ask about it. You need to be a little more specific about what you don't understand.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link but i just don't understand how i would calculate it all for every month. For the first month would it be something like this?

System.out.println ("Balance after first month: $" + deposit + monthly);

I appreciate the help
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:balance = balance + monthly deposit amount
interest = balance (current) * ((interest rate / 12) / 100)
balance (new) = balance (current) + interest

That's the formula i just need to know how i would implement that in Java.

You've pretty much done it. Just replace the descriptions with the actual variables you've declared and you should be pretty close.
You've already done the first one (see line 89 of your code)
balance = balance + monthly deposit amount

Try the others and if the results aren't exactly what you expect show us the new code.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Johnnoo Williams wrote:Anyone able to help me with this? Please?

We are helping.
Please tell us whether:
- you know how you want to calculate balance but don't know how to code it in Java (then ShowSomeEffort, show us what you tried and what you don't understand)
- you dont know how to calculate balance at all (in this case StopCoding and write it on paper first)

EDIT:
Sorry, I didn't notice you know the formula already.
Those are simple arithmetic operations. Read this tutorial: link
If you don't understand something ask about it. You need to be a little more specific about what you don't understand.


Are you able to help me out with the code for the first month? Maybe give me an example if possible? Once i see it i should then be able to apply it to the other months.

Thanks.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Thanks for the link but i just don't understand how i would calculate it all for every month. For the first month would it be something like this?

System.out.println ("Balance after first month: $" + deposit + monthly);

I appreciate the help

Close, but don't do the calculation in the print statement. You need to retain the result in order to calculate future months, so store the result back into your deposit variable and then print the value of that.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:System.out.println ("Balance after first month: $" + deposit + monthly);

Be careful when writing this kind of stuff.
Try executing this code and see what happened:
Ok, I'll spare you executing this by yourself. The output will be: Result = 56

The reason is the expression with + operator is calculated from left to right.
If any of operands is String the result is also a String.
So "Result = " + a gives Result = 5.
"Result = 5" + b gives Result = 56.

As for your problem. One option is to write a method that calculates the new balance given: old, balance, monthly amount and interest rate.

The function will have following signature:

Then in your code you might write for every month:
BTW, why are you keeping deposit, monthly as integers?
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the help guys i got the everything working except for the interest. I'm not sure what i'm doing wrong i was trying to do it similar to what i did with the other 2 but i get errors. Here is the code,

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 23 you have:That's wrong. You can't name a variable like deposit (current).
For the compiler it looks like a method deposit with an argument current. Also, you can't have a whitespace in variable's name.
Use other name (for example currentDeposit ) instead.

Also, when you got errors at compile time, it is helpful if you post them so we can see what goes wrong.
A link: ItDoesntWorkIsUseless
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:but i get errors.

It would help if you told us what the errors were.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the errors,

MenuDrivenProgram.java:91: ')' expected
interest = deposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:91: ';' expected
interest = deposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:92: <identifier> expected
deposit (new) = deposit (current) + interest;
^
MenuDrivenProgram.java:92: ')' expected
deposit (new) = deposit (current) + interest;
^
4 errors
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Thanks so much for the help guys i got the everything working except for the interest. I'm not sure what i'm doing wrong i was trying to do it similar to what i did with the other 2 but i get errors. Here is the code:

uggh...surely there must be a better way. What would you do if you had to calculate the results for 12 months? or 60? or for 30 years?

You should notice that you are basically doing the same hunk of code over and over again...that is very common, and so just about all languages have looping structures. You should probably look into writing a for-loop, since you will know in advance exactly how many times you want this hunk of code to run.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Here are the errors,

MenuDrivenProgram.java:91: ')' expected
interest = deposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:91: ';' expected
interest = deposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:92: <identifier> expected
deposit (new) = deposit (current) + interest;
^
MenuDrivenProgram.java:92: ')' expected
deposit (new) = deposit (current) + interest;
^
4 errors


So, this is what I thought. See my previous post .

BTW, for this you don't need to introduce a new variable. Just use the once you've already got. A deposit.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Johnnoo Williams wrote:Here are the errors,

MenuDrivenProgram.java:91: ')' expected
interest = deposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:91: ';' expected
interest = deposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:92: <identifier> expected
deposit (new) = deposit (current) + interest;
^
MenuDrivenProgram.java:92: ')' expected
deposit (new) = deposit (current) + interest;
^
4 errors


So, this is what I thought. See my previous post .

BTW, for this you don't need to introduce a new variable. Just use the once you've already got. A deposit.


Think i'm still doing it wrong. Is this close?

interest = currentDeposit (current) * ((interest / 12) / 100);
currentDeposit (new) = currentDeposit (current) + interest;
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:interest = currentDeposit (current) * ((interest / 12) / 100);
currentDeposit (new) = currentDeposit (current) + interest;

No. Don't guess. Don't introduce any new variable. Don't use braces like there is a method (like currentDeposit (current) ) because you are not calling any method. Don't update interest variable. You are mixing interest rate with the value of interest gained during a month.
Also new is a java keyword. You can never use it as a variable name.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not working i keep getting the same 4 errors.

Also do you mean i should change that value that says (new)?

EDIT: Okay i removed "new" and changed it to something else but i still get these 2 errors,

MenuDrivenProgram.java:91: ')' expected
interest = currentDeposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:91: ';' expected
interest = currentDeposit (current) * ((interest rate / 12) / 100);
^
2 errors
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Still not working i keep getting the same 4 errors.

Also do you mean i should change that value that says (new)?

EDIT: Okay i removed "new" and changed it to something else but i still get these 2 errors,

MenuDrivenProgram.java:91: ')' expected
interest = currentDeposit (current) * ((interest rate / 12) / 100);
^
MenuDrivenProgram.java:91: ';' expected
interest = currentDeposit (current) * ((interest rate / 12) / 100);
^
2 errors

I already told you that you can't have a variable named currentDeposit (current) or interest rate... :/
You are trying random things with your code without understanding what are you doing. Don't do that.
StopCoding and think what you want to achieve. In low level. Like you were explaining it to 3-years-old.
Then learn some basics of Java like legal names for variables. Only then go back to your code.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i've changed all the variables and made sure they aren't key java variable names but when i change interest rate i get more error messages.

I really don't know what to do here i've tried not coding and thinking about all this but it isn't working :/
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Yeah i've changed all the variables and made sure they aren't key java variable names but when i change interest rate i get more error messages.

I really don't know what to do here i've tried not coding and thinking about all this but it isn't working :/
Show us your code and the error messages, so we can tell you what else might be wrong.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you still haven't fixed those variable names, have you.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me what i should name them? Please?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Can you tell me what i should name them? Please?

Any way you like. But don't put braces ( ) or spaces into variable name.
Instead of balance (current) try balanceCurrent or currentBalance.
Instead of interest rate try interestRate.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got these 4 errors now. :/

MenuDrivenProgram.java:91: cannot find symbol
symbol : variable balanceCurrent
location: class MenuDrivenProgram
interest = balanceCurrent * interestRate / 12 / 100;
^
MenuDrivenProgram.java:91: cannot find symbol
symbol : variable interestRate
location: class MenuDrivenProgram
interest = balanceCurrent * interestRate / 12 / 100;
^
MenuDrivenProgram.java:92: cannot find symbol
symbol : method deposit(int)
location: class MenuDrivenProgram
deposit (deposit) = balanceCurrent + interest;
^
MenuDrivenProgram.java:92: cannot find symbol
symbol : variable balanceCurrent
location: class MenuDrivenProgram
deposit (deposit) = balanceCurrent + interest;
^
4 errors
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Got these 4 errors now. :/

MenuDrivenProgram.java:91: cannot find symbol
symbol : variable balanceCurrent
location: class MenuDrivenProgram
interest = balanceCurrent * interestRate / 12 / 100;
^
MenuDrivenProgram.java:91: cannot find symbol
symbol : variable interestRate
location: class MenuDrivenProgram
interest = balanceCurrent * interestRate / 12 / 100;
^
MenuDrivenProgram.java:92: cannot find symbol
symbol : method deposit(int)
location: class MenuDrivenProgram
deposit (deposit) = balanceCurrent + interest;
^
MenuDrivenProgram.java:92: cannot find symbol
symbol : variable balanceCurrent
location: class MenuDrivenProgram
deposit (deposit) = balanceCurrent + interest;
^
4 errors

That's because you use variable that you haven't declared. Declare the variable first.
Is deposit a method?
You are using deposit (deposit). Please, show us your full code.

Again, you are trying random things without understanding what you are doing.
Learn the basics first, do some tutorials (for example http://docs.oracle.com/javase/tutorial/ ).
When you learn basics of Java, go back to your program.

Also, you need to read and understand what java compiler complains about. Sometimes it not that easy or straightforward but in this case javac tells you:
MenuDrivenProgram.java:92: cannot find symbol
symbol : variable balanceCurrent
It is self-explanatory.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void InvestmentReport()
{
System.out.printf("*** Investment Report stub ***\n");
// This is where your Part A solution goes
System.out.printf("*************** InvestmentReport Menu ***************\n\n");
Scanner console=new Scanner(System.in);
int deposit, monthly, interestRate, balanceCurrent;
double interest;
System.out.println ("Enter your initial deposit amount in dollars\n");
deposit = console.nextInt();

System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
interest = console.nextDouble();

System.out.println ("Enter your monthly deposit amount in dollars\n");
monthly = console.nextInt();

System.out.println ("Savings growth over the next 6 months:\n");

System.out.println ("Balance after first month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month: $" + interest);
interest = balanceCurrent * ((interestRate / 12) / 100);
deposit (deposit) = balanceCurrent + interest;

System.out.println ("Balance after second month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month:\n");

System.out.println ("Balance after third month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month:\n");

System.out.println ("Balance after fourth month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month:\n");

System.out.println ("Balance after fifth month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month:\n");

System.out.println ("Balance after sixth month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month:\n");
}
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is the error message,

MenuDrivenProgram.java:92: cannot find symbol
symbol : method deposit(int)
location: class MenuDrivenProgram
deposit (deposit) = balanceCurrent + interest;
^
1 error
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. You used them before so you know how to do this. And please, post full code (whole file). Not just one method.
Also. Don't declare balanceCurrent as integer. How will you put 100.43 dollars into an integer?
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Theres the full code
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in line 92. You wrote deposit (depo) (and that in Java is a method call). You don't have method deposit hence the error.
I told you 1712 times not to use braces or spaces in variable name. And you are still doing that.

Please, learn the basics...
It's like you were trying to build a house without even knowing what a brick is.
I already posted a link to Oracle Getting Started Tutorial. It's really useful.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is that better?
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MenuDrivenProgram.java:92: possible loss of precision
found : double
required: int
deposit = balanceCurrent + interest;
^
1 error

Now i get that error. How do i fix that?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to assign a double to an int. It is not possible in Java (by simple assign without cast). Just change all your int variables to double and you should be fine. They should be double anyway.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did that but i get these 2 errors now

MenuDrivenProgram.java:90: variable balanceCurrent might not have been initialized
interest = balanceCurrent * interestRate / 12 / 100;
^
MenuDrivenProgram.java:90: variable interestRate might not have been initialized
interest = balanceCurrent * interestRate / 12 / 100;
^
2 errors

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, read those messages. What does the message say? That a variable might not be initialized? Yes, it does! And that means that you try to use a variable without giving it any value first!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!