I'm a beginner Java programmer trying to write a while statement in a loan table program. There are 2 separate classes: LoanTableTester (contains the main method) and LoanTable (doesn't contain the main method). I will post the code and the output in a sub-post of this question.
Output that I'm trying to get: Mortgage problem Principal = $100000.00 Time = 30 years Low rate = 11% High rate = 12% Annual Interest Rate Monthly Payment 11.00 952.32 11.25 971.26 11.50 990.29 11.75 1009.41 12.00 1028.61
LoanTableTester.java import apcslib.*; import chn.util.*; public class LoanTableTester { /** * Method Main * * @param args A parameter */ public static void main(String[] args) //Main method { //instance variables double loanAmt; //This is the loan amount in dollars int loanLgth; //This is the length of the loan in years double lowIntRt; //This is the lowIntRt expressed as a decimal double highIntRt; //This is the highIntRt expressed as a decimal ConsoleIO console = new ConsoleIO(); //ConsoleIO method System.out.println("Enter the dollar amount of the loan ---> "); //User enters the dollar amount of the loan loanAmt = console.readDouble(); //Takes user inout for loanAmt as a double System.out.println("Enter the length of the loan in years ---> "); //User enters the length of the loan in years loanLgth = console.readInt(); //Takes user input for loanLgth as an int System.out.println("Enter the low interest rate of the loan as a decimal ---> "); //User enters the low interest rate of the loan as a decimal lowIntRt = console.readDouble(); //Takes user input for lowIntRt as a double System.out.println("Enter the high interest rate of the loan as a decimal ---> "); //User enters the high interest rate of the loan as a decimal highIntRt = console.readDouble(); //Takes user input for highIntRt as a double LoanTable mortgage = new LoanTable(loanAmt, loanLgth, lowIntRt, highIntRt); //loanAmt, loanLgth, lowIntRt and highIntRt are being passed as parameters System.out.println("Mortgage problem"); //Prints out 'mortgage problem' System.out.println("Principal: " + loanAmt); //Prints out loanAmt System.out.println("Time: " + loanLgth + " years"); //Prints out the length of the loan in years System.out.println("Low rate: " + lowIntRt); //Prints out the lowIntRt as a decimal System.out.println("High rate: " + highIntRt); //Prints out the highIntRt as a decimal } }
LoanTable.java import apcslib.*; public class LoanTable { //instance variables double loanAmt; //This is the variable 'p' for the loan amount in dollars int loanLgth; //The length of the loan in years double lowIntRt; //The low interest rate expressed as a decimal double highIntRt; //The high interest rate expresed as a decimal double mthlyIntRt; //The monthly interest rate ('k') int nmbrOfMthlyPymts; //This is the number of monthly payments ('n') double intRt; //The interest rate incremented by 0.25 double c; //The c value is to be calculated before determining the monthly payments double annRt; //The annual rate double mthlyPymt; //This is the variable 'a' for calculating the monthly payment /** * Constructor for objects of class LoanTable */ public LoanTable(double myloanAmt, int myloanLgth, double mylowIntRt, double myhighIntRt) //The identifiers in the paratheses of public LoanTable are 3 doubles and 1 int for the parameters { loanAmt = myloanAmt; //loanAmt is set equal to myloanAmt becuase myloanAmt is being passed into public LoanTable loanLgth = myloanLgth; //loanLgth is set equal to myloanLgth becuase myloanLgth is being passed into public LoanTable lowIntRt = mylowIntRt; //lowIntRt is set equal to mylowIntRt because mylowIntRt is being passed into public LoanTable highIntRt = myhighIntRt; //highIntRt is set equal to myhighIntRt because myhighIntRt is being passed into public LoanTable } //Modifier for calculating the c value /** * Method CalcC * * @return The return value */ double CalcC() { c = Math.pow(1 + mthlyIntRt, nmbrOfMthlyPymts); return c; } //Modifier for calculating the monthly payment /** * Method CalcMthlyPymt * * @return The return value */ double CalcMthlyPymt() { mthlyPymt = (loanAmt * mthlyIntRt * c) / (c - 1); return mthlyPymt; } //Modifier for calculating the number of monthly payments /** * Method CalcNmbrOfMthlyPymts * * @return The return value */ double CalcNmbrOfMthlyPymts() { nmbrOfMthlyPymts = (loanLgth * 12); return nmbrOfMthlyPymts; } //Modifier for calculating the monthly interest rate /** * Method CalcMthlyIntRt * * @return The return value */ double CalcMthlyIntRt() { mthlyIntRt = (annRt/12.0); return mthlyIntRt; } /** * Method MthlyLoop * * @return The return value */ public double MthlyLoop() { double intRt = lowIntRt; while(intRt<highIntRt) { CalcC(); CalcMthlyPymt(); CalcNmbrOfMthlyPymts(); CalcMthlyIntRt(); intRt += 0.25; System.out.print("Help"); } return mthlyPymt; } }
@BlazeRyder
Let me look at it
While statement that I'm working on: public double MthlyLoop() { double intRt = lowIntRt; while(intRt<highIntRt) { CalcC(); CalcMthlyPymt(); CalcNmbrOfMthlyPymts(); CalcMthlyIntRt(); intRt += 0.25; System.out.print("Help"); } return mthlyPymt; }public double MthlyLoop() { double intRt = lowIntRt; while(intRt<highIntRt) { CalcC(); CalcMthlyPymt(); CalcNmbrOfMthlyPymts(); CalcMthlyIntRt(); intRt += 0.25; System.out.print("Help"); } return mthlyPymt; }
The output that I currently get: Enter the dollar amount of the loan ---> 100000.00 Enter the length of the loan in years ---> 30 Enter the low interest rate of the loan as a decimal ---> .11 Enter the high interest rate of the loan as a decimal ---> .12 Mortgage problem Principal: 100000.0 Time: 30 years Low rate: 0.11 High rate: 0.12
Yeah, I do not know that much java. But one thing that I do see is that you are trying to call public class LoanTable to your main method. If you want to do this you need to add void to your public class LoanTable. That is the only thing I can see. Also it looks like you found different ways to get user input. I learned that differently but it works the same way I am guessing. =) Let me tag some people who might be able to help you out! @SapphireMoon @NvidiaIntely
I did the calculation methods for 'c', MthlyPymt, NmbrOfMthlyPymts, and MthlyIntRt outside of the while statement.
@SapphireMoon @NvidiaIntely I know you guys helped me out the other day so do you guys know this?
If this was HTML and CSS I would be able to help you out since I know it by heart. But not a lot of people want to learn it. IDK why though! @Comm.Dan
Such as 'public class void LoanTable'? @BlazeRyder
Yeah. I made that mistake the other day when I was trying to call a method into my main method.
I'm not sure if you still need help on this, but could you post the code to pastebucket or another website similar? It looks very messy on my computer for some reason. Once I can read it, I'll do my best to help! Haven't done Java in a bit but after looking at it, I should be able to figure it out.
I might b able 2 help as well. not that this problem is getting any more attention
Join our real-time social learning platform and learn together with your friends!