Results 1 to 4 of 4

Thread: Resolving some errors

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Location
    In the midst of corn, cotton, and beans
    Posts
    185

    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

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    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.

  3. #3
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    Re: Resolving some errors

    I also recomend using more relivent variable names to make it easier to follow the code.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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
  •  



Click Here to Expand Forum to Full Width