abstract:
/** this method is for displaying result
* @param month int - month to display
* @param ipaid double - interest paid
* @param computePayment double - Compute payment
* @version 3.00
* @author MM
*/
private void printAmortisation(int month, double ipaid, double computePayment){
DecimalFormat form = new DecimalFormat("###.00");
form.setGroupingUsed(true);
System.out.println(" " + month + "\t$" + form.format(ipaid) + "\t\t$" + form.format(computePayment));
}
/** this method is for displaying header
* @param month int - month to calculate year
* @version 3.00
* @author MM
*/
private void headerPrinter(int month){
System.out.println("-------------------------------------------------");
System.out.println("Month: Interest: Balance: YEAR : "+month/12);
System.out.println("-------------------------------------------------");
}
/** this method is for displaying message to prompt user
* @version 3.00
* @author MM
*/
private void continueMessage(){
System.out.println("************Press Enter to continue**************");
}
/** this method is for displaying message for end of program
* @version 3.00
* @author MM
*/
private void endProgramMessage(){
System.out.println("************END OF PROGRAM**************");
}
/** main method
* @param args String[]
* @version 3.00
* @author MM
*/
public static void main(String[] args) {
MortgageCalculatorWeek4 mortgageCalculatorWeek4 = new MortgageCalculatorWeek4(); // initialize class
System.out.println("-------------------Mortgage Calculator 4------------------------------"); // print header
mortgageCalculatorWeek4.setCalc(); // start calculations
}
}
I have wrote my code for my assignment and it seems to be working ok. However, I would like to know if someone can look at my assignment details and compare them to my code to see if i am ok before I submit. The reason is because the past two weeks I have had silly simple mistakes and lost several points. Thanks in advance!
here is the assignment details for this program:
Modify the mortgage program to calculate and display the mortgage payment amount for each of three mortgage loans: 7 years at 5.35\%, 15 years at 5.5\%, and 30 years at 5.75\%. Use an array for the different loans. Hard code the loan amount to $200,000. Continue using a command-line interface.
Here is my Java code:
Code:- import java.text.DecimalFormat;
- public class MortgageCalculatorWeek4 {
- // Initial values set for the fields
- public static double amountFix = 200000; //Initial mortgage amount
- public static int[] term = {7, 15, 30}; // hard coded array of terms
- public static double[] interest = {.0535, .0550, .0575}; // hardcoded array of loans
- /**
- * Computes the monthly payment based on the loan amount, rate, and length of the loan
- * @param loanAmt double - Loan amount
- * @param rate double - Loan rate
- * @param term int - length of the loan
- * @version 3.0
- * @author MM
- */
- public double computePayment(double loanAmt, double rate, int term)
- {
- double mrate; // mortgage rate
- double months; // months
- double answer; // result
- months = term * 12; //Gets number of months
- mrate = rate / 100.0 / 12.0; //Gets monthly rate from annual
- answer = loanAmt * mrate / (1 - Math.pow ((1 + mrate),(-months)));
- return answer; // return result
- }
- /** This is the section where all the calculations are performed and printed
- * @version 3.00
- * @author MM
- */
- public void setCalc()
- {
- boolean displayHeader = true; // boolean which decides what header should be displayed
- int array = 0; // array index
- double computePayment;
- double ipaid;
- for(array = 0 ;array<3;array++){
- computePayment = amountFix; // assign initial value
- double monthlyPayment = new Double(computePayment(amountFix, interest[array]*100,term[array])); // calculate monthly payment
- printMonthlyPayment(monthlyPayment,interest[array],term[array]); // display monthly payment
- for(int month = 1; month <= (term[array] * 12) ; month++)
- {
- // calculate interest and balance
- ipaid = computePayment * (interest[array] / 12);
- computePayment -= (monthlyPayment - ipaid);
- if(month == term[array] * 12 && computePayment < 0) {
- computePayment *= -1;
- }
- if(displayHeader){ // if it's needed displayHeader
- headerPrinter(month);
- }
- displayHeader = false; // set displayHeader to false again
- printAmortisation(month,ipaid,computePayment); // display results
- if((month\%12) == 0){
- if(!(month == (term[term.length-1]*12) && (term.length-1 == array))){ // if month is different of value in last term
- continueMessage(); // print continue message
- displayHeader = true; // set display header to true
- }else{
- endProgramMessage(); // else display end program message
- System.exit(1); // and finish program
- }
- try{
- System.in.read();
- }catch (Exception ex){
- ex.printStackTrace(); // if any exception occurs print stack trace
- }
- }
- }
- }
- }
- // methods for displaying values and friendly user interface
- /** this method is for displaying result
- * @param month int - month to display
- * @param ipaid double - interest paid
- * @param computePayment double - Compute payment
- * @version 3.00
- * @author MM
- */
- private void printMonthlyPayment(double monthlyPayment, double interest, int term){
- DecimalFormat form = new DecimalFormat("###.00");
- form.setGroupingUsed(true);
- System.out.println("Monthly payment : $"+ form.format(monthlyPayment)+" for Loan: " +interest*100+"\% & Term: "+term+" years");
- }
- /** this method is for displaying result
- * @param month int - month to display
- * @param ipaid double - interest paid
- * @param computePayment double - Compute payment
- * @version 3.00
- * @author MM
- */
- private void printAmortisation(int month, double ipaid, double computePayment){
- DecimalFormat form = new DecimalFormat("###.00");
- form.setGroupingUsed(true);
- System.out.println(" " + month + "\t$" + form.format(ipaid) + "\t\t$" + form.format(computePayment));
- }
- /** this method is for displaying header
- * @param month int - month to calculate year
- * @version 3.00
- * @author MM
- */
- private void headerPrinter(int month){
- System.out.println("-------------------------------------------------");
- System.out.println("Month: Interest: Balance: YEAR : "+month/12);
- System.out.println("-------------------------------------------------");
- }
- /** this method is for displaying message to prompt user
- * @version 3.00
- * @author MM
- */
- private void continueMessage(){
- System.out.println("************Press Enter to continue**************");
- }
- /** this method is for displaying message for end of program
- * @version 3.00
- * @author MM
- */
- private void endProgramMessage(){
- System.out.println("************END OF PROGRAM**************");
- }
- /** main method
- * @param args String[]
- * @version 3.00
- * @author MM
- */
- public static void main(String[] args) {
- MortgageCalculatorWeek4 mortgageCalculatorWeek4 = new MortgageCalculatorWeek4(); // initialize class
- System.out.println("-------------------Mortgage Calculator 4------------------------------"); // print header
- mortgageCalculatorWeek4.setCalc(); // start calculations
- }
- }
Copy Code |