View Full Version : New Java help
troublemaker
07-15-2008, 04:03 AM
Hi, i am extremely new to java and am learning loops. I am stuck on a question though. here it is:
ask the user to enter # years invested, the $ invested and the % rate invested at. Find the compounded amount. like this
$100.00 was invested for 2 years at 12%
Year Interest Investment's worth now
1 12.00 112.00
2 13.44 125.44
i have gotten this far:
import java.awt.*;
import hsa.Console;
import java.text.*;
public class Repeat
{
static Console c;
public static void main (String[] args)
{
c = new Console ();
int numberOfYearsInput, runTime;
double amountInvestedInput, interestRateInput, amountInvestedCalc, interestAmountCalc, investmentWorthCalc;
runTime = 0;
DecimalFormat x = new DecimalFormat("$##.##");
c.setCursor(1,40);
c.println("Investment Program");
c.setCursor(2,1);
c.println("How many years are invested:");
c.setCursor(2,30);
numberOfYearsInput = c.readInt();
c.setCursor(3,1);
c.println("How much was invested:");
c.setCursor(3,30);
amountInvestedInput = c.readDouble();
c.setCursor(4,1);
c.println("Interest Rate Invented at:" + " %");
c.setCursor(4,30);
interestRateInput = c.readDouble();
c.setCursor(8,1);
c.println("Year");
c.setCursor(8,10);
c.println("Interest");
c.setCursor(8,20);
c.println("Investment Worth");
interestAmountCalc = ((amountInvestedInput)* (interestRateInput/100));
investmentWorthCalc = (amountInvestedInput + interestAmountCalc);
for(runTime = 1; runTime <=numberOfYearsInput; runTime ++)
{
c.println(runTime);
c.println(interestAmountCalc, 10);
c.println(investmentWorthCalc,20);
}
}
}
brad211987
07-15-2008, 04:23 AM
so.....whats the problem?
troublemaker
07-15-2008, 04:28 AM
umm.. basically how do i make it so that the year 1 interest value has the 12% interest applied for year 2 and it displays that amount in the worth now for year 2. as well as the interest earned on the 112$ according to 12%
Right now all it is doing is if i enter two years it will do
year interest worth now
1 12.5 112
2 12.5 112
please help
brad211987
07-15-2008, 04:38 AM
You will need to put logic inside your loop to keep a running total of your compounding interest. Right now, your loop only prints values, nothing is changed. Here is an example of keeping a running total:
public class RunningTotal
{
public static void main(String[] args)
{
private total = 100;
for (int x = 0 ; x < 100 ; ++x)
{
total = total + x;
System.out.println(total);
}
}
}
The variable "total" would be similar to your original principal balance. In my example, I simply add the current value of "x" to the total on each iteration of the loop. The output of this program should look like this:
100 (add 0 for x)
101 (add 1 for x)
103 (add 2 for x)
106 (add 3 for x)
This is essentially what you will need to do within your loop, only instead of simply adding the value of x, you will need to add the value (currentPrincipal * interestRate).
Hope that gets you in the right direction, feel free to post more of your code as you work through it.
troublemaker
07-15-2008, 04:38 AM
Can anyone please help me with this?
brad211987
07-15-2008, 04:38 AM
Gotta give me more than a few minutes to write a reply :thumbsup:
troublemaker
07-15-2008, 04:41 AM
ywp and reply you did thanks for the advice i will need some time to understand it! dont go anywhere! you are the best!
troublemaker
07-15-2008, 04:48 AM
wow. i think the problem i am having is that my variables are horrible defined and i am getting confused. will it be possible for you to give me the names of the variables and like a set up windows where i can try out the loop things. This is what i am getting so far:
import java.awt.*;
import hsa.Console;
import java.text.*;
public class RepetitiohpipiongPractice
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
int numberOfYearsInput, runTime;
double amountInvestedInput, interestRateInput, amountInvestedCalc, interestAmountCalc, investmentWorthCalc;
runTime = 0;
DecimalFormat x = new DecimalFormat("$##.##");
c.setCursor(1,40);
c.println("Investment Program");
c.setCursor(2,1);
c.println("How many years are invested:");
c.setCursor(2,30);
numberOfYearsInput = c.readInt();
c.setCursor(3,1);
c.println("How much was invested:");
c.setCursor(3,30);
amountInvestedInput = c.readDouble();
c.setCursor(4,1);
c.println("Interest Rate Invented at:" + " %");
c.setCursor(4,30);
interestRateInput = c.readDouble();
c.setCursor(8,1);
c.println("Year");
c.setCursor(8,10);
c.println("Interest");
c.setCursor(8,20);
c.println("Investment Worth");
for(runTime = 1; runTime <=numberOfYearsInput; runTime ++)
{
interestAmountCalc = ((amountInvestedInput)* (interestRateInput/100));
investmentWorthCalc = (amountInvestedInput + interestAmountCalc);
amountInvestedInput = amountInvestedInput * interestRateInput;
c.println(runTime);
c.println(interestAmountCalc, 10);
c.println(investmentWorthCalc,20);
}
}
}
brad211987
07-15-2008, 05:06 AM
You are on the right track, but when you add up your total, you are cutting it down by the interest rate instead of adding the interest.
Try this instead:
amountInvestedInput = amountInvestedInput + (amountInvestedInput * interestRateInput);These 2 lines:
interestAmountCalc = ((amountInvestedInput)* (interestRateInput/100));
investmentWorthCalc = (amountInvestedInput + interestAmountCalc);
probably won't need to be in the loop.
troublemaker
07-15-2008, 05:13 AM
thanks but i still cannot get it to work for the life of me. i have been staring at it for 2 hours straight now and just cannot think. i cannot afford to stop now though. but i really am getting frutrated now. You are great though. Very patient and understanding about my slowness.... anyways here is my updated code:
import java.awt.*;
import hsa.Console;
import java.text.*;
public class RepetitiohpipiongPractice
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
int numberOfYearsInput, runTime;
double amountInvestedInput, interestRateInput, amountInvestedCalc, interestAmountCalc, investmentWorthCalc;
runTime = 0;
DecimalFormat x = new DecimalFormat("$##.##");
c.setCursor(1,40);
c.println("Investment Program");
c.setCursor(2,1);
c.println("How many years are invested:");
c.setCursor(2,30);
numberOfYearsInput = c.readInt();
c.setCursor(3,1);
c.println("How much was invested:");
c.setCursor(3,30);
amountInvestedInput = c.readDouble();
c.setCursor(4,1);
c.println("Interest Rate Invented at:" + " %");
c.setCursor(4,30);
interestRateInput = c.readDouble();
c.setCursor(8,1);
c.println("Year");
c.setCursor(8,10);
c.println("Interest");
c.setCursor(8,20);
c.println("Investment Worth");
interestAmountCalc = ((amountInvestedInput)* (interestRateInput/100));
investmentWorthCalc = (amountInvestedInput + interestAmountCalc);
for(runTime = 1; runTime <=numberOfYearsInput; runTime ++)
{
amountInvestedInput = amountInvestedInput + (amountInvestedInput * interestRateInput);
c.println(runTime);
c.println(interestAmountCalc, 10);
c.println(investmentWorthCalc,20);
}
}
}
i get the same thing for year 1 as year 2. the calculations for year 1 are right though
can you just post the right code!! i know it will not benifit me in the long run, but after staring at this for several hours i just want it to work!!!!!
troublemaker
07-15-2008, 05:41 AM
are you still there? I am pulling my hairs !!!
troublemaker
07-15-2008, 06:04 AM
Anyone There!!!!
brad211987
07-15-2008, 01:25 PM
Don't you sleep?
Take a closer look at what you are now doing in your loop.
amountInvestedInput = amountInvestedInput + (amountInvestedInput * interestRateInput);
c.println(runTime);
c.println(interestAmountCalc, 10);
c.println(investmentWorthCalc,20);
You are adding the interest amounts to "amountInvestedInput" but you are printing other values. Take a step back and think through this entire process before writing any more. Here is a general breakdown of the problem.
variables:
principalBalance
interestRate
termInYears
logic flow:
ask user for input to fill 3 variables
for each year in termInYears
principalBalance = principalBalance + (principalBalance * interestRate)
print principalBalance
end loop
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.