|
-
Dec 12th, 2010, 08:04 PM
#1
Thread Starter
Addicted Member
Resolving some errors
I have the following code for an assignment. Yes, this is for a class, so I don't want the answer given to me but I have searched the code over and I can't come up with anything. A nudge in the right direction would be great.
Code:
public class Loan {
public float P;
public float I;
public float Ia;
public int Nyr;
public int Nprd;
public void setP() {
P = 200000;
}
public void setI() {
I = 5.75f;
Ia = I/(12*100);
}
public void setN() {
Nyr = 30;
Nprd = Nyr*12;
}
public void CalcTbl() {
float Pa;
int Pc; //current period num
float M; //monthly payment
float Mi; //interest paid
float Mp; //principal paid
Pa = P;
Pc = 0;
for(int Npa = Nprd; Npa = 0; Npa--) { //incompatible data types
Pc ++;
M = Pa * (Ia / (1- power(1+Ia,-1*Npa)));
Mi = Pa * Ia;
Mp = M - Mi;
Pa = Pa - Mp;
System.out.println("Current period " + Pc +
" || Payment $" + M +
" || Interest $" + Mi +
" || Principal $" + Mp);
}
}
public float power (float Base, double Pow) {
float Ans = Base;
for(double x = 2; x > Pow; x++) {
Ans = Ans * Base;}
return Ans;
}
}
public class W2IA {
public static void main(String[] args) {
Loan myLoan = new Loan();
myLoan.setP();
myLoan.setI();
myLoan.setN();
System.out.println("For a loan with:");
System.out.println("Principal amount of " + myLoan.P);
System.out.println("Interest of " + myLoan.I);
System.out.println("Number of periods (months) " + myLoan.Nprd);
myLoan.CalcTbl();
}
}
Thanks for the help.
-RT
-
Dec 13th, 2010, 07:07 PM
#2
Re: Resolving some errors
Well first thing I see is that you're using an assignment operator instead of a comparison operator. Your for loop should look like this
Code:
for(int Npa = Nprd; Npa == 0; Npa--) {
Actually, that is most likely the culprit. The program is trying to convert it from a boolean to an integer because you're using the comparison operator.
-
Dec 14th, 2010, 01:53 AM
#3
Lively Member
Re: Resolving some errors
I also recomend using more relivent variable names to make it easier to follow the code.
-
Dec 14th, 2010, 02:35 AM
#4
Re: Resolving some errors
Also, it would be good if you included comments in your code so that people read you code knows how it works.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|