JH6 Homework Assignment Application Output: After prompting for the required inp
ID: 3577142 • Letter: J
Question
JH6 Homework Assignment
Application Output: After prompting for the required input items (loan amount or principle, number of months, and annual percentage rate) the program will provide the following information in order:
The total monthly payment amount.
The amortization schedule which will include the following columns (appropriately labeled):
payment number or period (from 1 to n number of months), the monthly payment amount, amount that is applied to principle during that period, the amount that is applied to interest, and the new balance of the loan after the principle amount is applied.
The amortization schedule will be displayed in sections of 36 months (3 years) at a time (unless the loan term is less than three years, then the entire schedule is displayed). At the end of a given section the user will be prompted to either go forward (f), go backward (b), or quit (q). When the user selects to go forward the next 36 months of the schedule will be displayed. Alternately, when the user wishes to go backward the application will display the previous 36 months.
All money amounts (principle, interest, balance) must be rounded and displayed to two decimal positions.
Programming Requirements: Because we are working with classes, class objects, and object oriented code we want to take as much code out of the main application as possible. Therefore, we will use classes to handle the majority of the work that needs to be done.
For this program you will have a main application plus two separate classes. These two classes will have no relationship to one another—that is one is not a parent and the other is not a child. Both of these classes are base or top-level classes, independent one from another. The first class will be called LoanCalculation, and the other LoanAmortization.
LoanCalculation: this class will perform the calculation of the monthly payment amount and adhere to the following guidelines:
This class will have one constructor that takes as its parameters the loan amount, the number of months of the loan, and annual interest rate. These values will be stored in private properties. Also, this constructor will automatically calculate the monthly interest rate and store this value also as a private property.
There will be a method called calculatePayment() that will calculate and return to the application the monthly payment amount. This method will use the private properties of the class for what it needs for the calculation, therefore you will not pass any parameters into this method.
You should create get methods for the private properties that will need to be retrieved by the main application: getMonths(), getLoanAmount(), etc. Do not use the variables in your main program after you have instantiated an instance of this class—use the get methods of this class to return the data you need.
LoanAmortization: this class will calculate the principle and interest portions for each monthly payment amount and will also calculate the remaining loan balance for each period, and will adhere to the following guidelines:
CPS-161: Introduction to Java Programming
JH6 Homework Assignment
This class will have one constructor that takes as its parameters the period number, the monthly payment amount, and the loan balance as of the start of that period.
There will be a method called calculateNewPeriod() that will calculate the interest, principle, and new loan balance for the period—all of which will be saved to private properties. The results of this calculation will be stored in private properties created for this purpose.
There will be a method called formatPeriod() that will format and return to the application a single print line representing all of the data needed for a single period’s amortization.
Main Application: The main application will broker the exchange of information between itself and the classes and will control all aspects of interaction with the user. In summary, the main application will need to perform the following functions:
Prompt and accept from the user the three pieces of data required for this application: Loan amount, duration of the loan in months, and annual interest rate.
Instantiate a new instance of LoanCalcuation() passing the user’s data into the constructor.
Invoice the LoanCalculation() class’s calculatePayment method to obtain the monthly payment
amount.
Create an array typed as LoanAmortization that is sized to the number of months of the loan.
Construct a loop for the number of months of the loan. Within each iteration of this loop
perform the following tasks:
Instantiate a new instance of LoanAmortization() passing the necessary data into the
class’s constructor.
Invoke the LoanAmortization() class’s calculateNewPeriod() method.
Store the LoanAmortization instance variable in the array.
Construct a loop that will prompt the user to move forward, backward, or quit, and then display the first page of the loan’s amortization invoking the LoanAmortization() class’s formatPeriod() method for each element of the array. Within this loop the code must respond to the user’s request to display subsequent pages of the schedule or to quit the application.
Tips:
I would not attempt to perform the entire amortization calculation in a single line of code. Rather, you should break the calculation up into multiple variables that are then put together for the final result.
Because the user wants to be able to move forward or backward through the amortization schedule in 3 year increments, you will need to calculate the entire amortization schedule and store the results in an array. When you are displaying the schedule, you are simply looping through the specific periods (elements) within the array. What you are storing in the array are instances of the LoanAmortization() class.
Your class objects must be created as separate class/code files (as in my in-class example).
Final Note: The monthly amount of the loan will remain the same throughout the life of the loan with one possible exception: the final month. Why, and how do you calculate this? Note: leave this to end of your programming. If you figure this out then I will count it as extra credit, but I will give no further information on what this requirement is or how to program for it.
Explanation / Answer
public class Main {
public static void main(String[] args) {
Main eCalc = new Main();
eCalc.calcEmiAllMonths(1000, 10, 12);
}
public Double calcEmi(double p, double r, double n) {
double R = (r / 12) / 100;
double e = (p * R * (Math.pow((1 + R), n)) / ((Math.pow((1 + R), n)) - 1));
return e;
}
public void calcEmiAllMonths(double p, double r, double n) {
double R = (r / 12) / 100;
double P = p;
double e = calcEmi(P, r, n);
double totalInt = Math.round((e * n) - p);
double totalAmt = Math.round((e * n));
System.out.println("***************************");
System.out.println(" Principal-> " + P);
System.out.println(" Rate of Interest-> " + r);
System.out.println(" Number of Months-> " + n);
System.out.println(" EMI -> " + Math.round(e));
System.out.println(" Total Interest -> " + totalInt);
System.out.println(" Total Amount -> " + totalAmt);
System.out.println("***************************");
double intPerMonth = Math.round(totalInt / n);
for (double i = 1; i <= n; i++) {
intPerMonth = (P * R);
P = ((P) - ((e) - (intPerMonth)));
System.out.println( (int) i+" " + Math.round(intPerMonth)+" "+ Math.round((e) - intPerMonth)+" "+Math.round(P));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.