import java.io.*; import java.util.Scanner; /********************************************************************* * * @author Duncan Buell */ public class Loan { private double payoff,principal,rate; /********************************************************************* * This is the standard constructor, which ought for good form do the * explicit initialization of the instance variables for the class. */ public Loan() { this.payoff = 0.0; this.principal = 0.0; this.rate = 0.0; } // public Loan() /********************************************************************* * This method simply returns the double precision value of the * monthly payoff. * * @return The double value that is the monthly loan payoff. */ public double getPayoff() { return(this.payoff); } /********************************************************************* * This method simply returns the double precision value of the current * loan principal. * * @return The double value that is the current loan principal. */ public double getPrincipal() { return(this.principal); } /********************************************************************* * This method simply returns the double precision value of the loan * interest rate. * * @return The double value that is the loan interest rate. */ public double getRate() { return(this.rate); } /********************************************************************* * This method takes an input string, converts to a double, and sets * that double value as the monthly payoff amount of the loan. * It SHOULD be the case that extensive error testing is done in this * method to determine that in fact the input string represents a legal * value for a double. However, we don't do that here. * * @param The (String) value of what is supposed to be a double. * @return A boolean false of the input was illegal, else true. */ public boolean setPayoff(String inString) { boolean returnValue; double convertedValue; convertedValue = Double.valueOf(inString); returnValue = false; if(convertedValue > 0.0) { this.payoff = convertedValue; returnValue = true; } return(returnValue); } /********************************************************************* * This method takes an input string, converts to a double, and sets * that double value as the prinicpal amount of the loan. * Same comments about error checking as for the payoff. * * @param The (String) value of what is supposed to be a double. * @return A boolean false of the input was illegal, else true. */ public boolean setPrincipal(String inString) { boolean returnValue; double convertedValue; convertedValue = Double.valueOf(inString); returnValue = false; if(convertedValue > 0.0) { this.principal = convertedValue; returnValue = true; } return(returnValue); } /********************************************************************* * This method takes an input string, converts to a double, and sets * that double value as the interest rate of the loan. * Same comments about error checking as for the payoff. * * @param The (String) value of what is supposed to be a double. * @return A boolean false of the input was illegal, else true. */ public boolean setRate(String inString) { boolean returnValue; double convertedValue; convertedValue = Double.valueOf(inString); returnValue = false; if(convertedValue > 0.0) { this.rate = convertedValue; returnValue = true; } return(returnValue); } /********************************************************************* * This method amortizes the loan until the principal balance is zero. * NOTE: This assumes monthly calculation of interest, without regard * to the number of days in a month, etc. */ public void amortize() { int month; double interestThisMonth,newPrincipal; System.out.printf("Mon OldPrinc Int NewPrinc%n"); month = 0; while(this.principal > 0.0) { ++month; interestThisMonth = this.principal * (this.rate/100.0) / 12.0; newPrincipal = this.principal + interestThisMonth - this.payoff; System.out.printf("%3d %8.2f %8.2f %8.2f %n",month,principal,interestThisMonth,newPrincipal); this.principal = newPrincipal; if(month > 180) System.exit(0); } } // public void amortize() } // public class Loan