I'm having trouble getting a mathmatical formula to work in my code.
I am supposed to write a program in java that calculates the ending balance each month. The user is supposed to input the monthly payment, the annual interest rate, and the total months required to pay off the loan.
I am to use the formula:
Balance(pn) = payment * (1 - (1 + mir) ^ (pn - tp)) / mir
payment = monthly payment
mir = monthly interest rate
pn = payment number
tp = total number of payments
^ = raised to the power of (the exponential operator - code will use the pow method for this task)
Here's my code so far:
[HTML]import java.util.Scanner;
public class CarPayment
{
private double balance; //balance for payoff
private double payment; //monthly payment amount
private double mir; //monthly interest rate
private double pn; //payment number
private double tp; //total number of payments required for payoff
public void inputdata()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the monthly payment:");
payment = input.nextDouble();
System.out.println("Enter the annual interest rate:");
mir = input.nextDouble();
System.out.println("Enter the number of months required for pay off:");
tp = input.nextDouble();
}
public void schedule()
{
System.out.printf("%s%20s\n", "After payment number", "Payoff");
for (int i = 1; i <= tp; i++)
{
// balance = payment * (1 - (1 + mir) ^ (pn - tp)) / mir;
balance = payment * (1 - Math.pow((1 + mir),(pn - tp))) / mir;
System.out.println(i + " $" + balance);
} //end for
} //end schedule
} //end CarPayment class
public class CarPaymentTester
{
public void run()
{
CarPayment pay = new CarPayment();
pay.inputdata();
pay.schedule();
} //end run
public static void main(String[] args)
{
CarPaymentTester t = new CarPaymentTester();
t.run();
} //end main
} //end CarPaymentTester class[/HTML]
the problem code is in the schedule method of the CarPayment class.
Any help will be appreciated.
Thanks.
8 2072
You did not initialize pn so IMO pn is always 0 everytime.
I think pn should be int not double and should be used as - for (int i = 1; i <= tp; i++) {
-
-
balance = payment * (1 - Math.pow((1 + mir),(i - tp))) / mir;
-
System.out.println(i + \" $\" + balance);
-
}
-
-
So that pn starts from 1 to tp
Final=Start(1+(interest in a decimal(10%=.10)/YearDenoms(Per year is 1, per month is 12))^Years/YearDenoms
Thats an effective formula I learned last year in Math for finding interest.
Scratch that power part, its the rate*year denoms, sorry...not Years/year denoms, if you have questions about it I will clarify any of them
r035198x,
Thanks for the help, but the problem is still there. Your formula with pn initialized gets one of the same results I was getting.
I'm still searching for a solution.
JavaStudent07,
I'm sure your formulas will work, but my instructor specified the basic formula and the fact that it must utilize the Math.pow() method.
Thanks for the input none the less.
horace1 1,510
Recognized Expert Top Contributor
r035198x,
Thanks for the help, but the problem is still there. Your formula with pn initialized gets one of the same results I was getting.
I'm still searching for a solution.
JavaStudent07,
I'm sure your formulas will work, but my instructor specified the basic formula and the fact that it must utilize the Math.pow() method.
Thanks for the input none the less.
if schedule() is -
public void schedule()
-
{
-
System.out.printf("%s%20s\n", "After payment number", "Payoff");
-
-
for (int pn = 1; pn <= tp; pn++)
-
{
-
// balance = payment * (1 - (1 + mir) ^ (pn - tp)) / mir;
-
balance = payment * (1 - Math.pow((1 + mir),(pn - tp))) / mir;
-
System.out.println(pn + " $" + balance);
-
} //end for
-
} //end schedule
-
} //end CarPayment class
-
when run I get
Enter the monthly payment:
100
Enter the annual interest rate:
.1
Enter the number of months required for pay off:
10
After payment number Payoff
1 $575.9023816275154
2 $533.4926197902669
3 $486.84188176929354
4 $435.52606994622283
5 $379.07867694084507
6 $316.98654463492954
7 $248.68519909842243
8 $173.55371900826455
9 $90.90909090909093
10 $0.0
which looks reasonable - any idea what the answer should be?
Yes, that looks reasonable, but I was given test criteria to ensure the formula is correct. If the monthly payment is 165.25, the total number of payments is 36, and the interest rate is 9%, the balance after the first payment is 5070.31. When I run these numbers I get a first payment of 1746.16.
Any ideas? Thanks for helping me dig.
horace1 1,510
Recognized Expert Top Contributor
Yes, that looks reasonable, but I was given test criteria to ensure the formula is correct. If the monthly payment is 165.25, the total number of payments is 36, and the interest rate is 9%, the balance after the first payment is 5070.31. When I run these numbers I get a first payment of 1746.16.
Any ideas? Thanks for helping me dig.
I think your problem is you are asking for the annual interest rate which is 9% and the calculation uses the monthly interest rate which is .0075
using your data with this value a run gives
Enter the monthly payment:
165.25
Enter the annual interest rate:
.0075
Enter the number of months required for pay off:
36
After payment number Payoff
1 $5070.308952032767
2 $4943.086269173012
3 $4814.909416191807
4 $4685.771236813245
5 $4555.664521089341
6 $4424.582004997514
7 $4292.516370034991
8 $4159.460242810253
9 $4025.4061946313295
10 $3890.3467410910625
11 $3754.2743416492444
12 $3617.1813992116136
13 $3479.0602597057
14 $3339.9032116534904
15 $3199.70248574089
16 $3058.450254383945
17 $2916.138631291825
18 $2772.7596710265116
19 $2628.3053685592095
20 $2482.7676588234012
21 $2336.1384162645772
22 $2188.40945438656
23 $2039.572525294457
24 $1889.619319234164
25 $1738.5414641284187
26 $1586.3305251093814
27 $1432.978004047701
28 $1278.4753390780572
29 $1122.8139041211405
30 $965.985008402048
31 $807.979895965062
32 $648.7897451847983
33 $488.40566827368485
34 $326.8187107857351
35 $164.01985111662685
36 $0.0
OOOOOO. Good catch. I can work with that.
Thanks. I know where to go from here.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Shannan Casteel via AccessMonster.com |
last post by:
I have a subform for listing parts. It has fields including: ClaimID, ITEM,
NET PRICE, LIST PRICE, Quantity, Supplier, and a calculated field called Part
Total. The subform is based on a...
|
by: Roy |
last post by:
Ok, here's my problem and I'm a first year webbie, so you'll have to
bear with me. I'm converting some Access reports over to a .NET
website. It's your typical Access cluster$#@$ (pardon my...
|
by: Daniel Walzenbach |
last post by:
Hi,
I have a web application which sometimes throws an “out of memory”
exception. To get an idea what happens I traced some values using performance
monitor and got the following values (for...
|
by: Olivier BESSON |
last post by:
Hello,
I have a web service of my own on a server (vb.net). I must declare it with
SoapRpcMethod to be used with JAVA.
This is a simple exemple method of my vb source :
...
|
by: damon.reynolds |
last post by:
Hello.
I am trying to write an expression to help me pull some data out of my
database. We have a group of people travelling between two dates -
and
Unfortunatley for me, the rooms are...
| |
by: Santosh Krisnan |
last post by:
hello all,
I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the...
|
by: XVI |
last post by:
Hi, I am working with 3 acces 2000 database. I have make 20 query to extract informations on these database. The 3 database have the same structure and datatype bot not the same records. When I...
|
by: barnzee |
last post by:
Hi all, newbie here, but having a go
I am trying to build a stock watchlist in excel 2007 with a dynamic link to a DDE server (paid for from a broker).There is no add-in or plug-in, I just CTL ALT...
|
by: Barry L. Bond |
last post by:
Greetings!
I just got a new Peet Brothers Ultimeter 2100 Weather Station. This
new one has a way to display the heat index, if you press the "dew point"
key twice.
Being aware of all the...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |