javasubroutineamortization

Amortization Table


This program will calculate the amortization table for a user. The problem is my assignment requires use of subroutines. I totally forgot about that, any ideas on how to modify this to include subroutines?

public class Summ {

public static void main(String args[]){
double loanamount, monthlypay, annualinterest, monthlyinterest, loanlength; //initialize variables

Scanner stdin = new Scanner (System.in);    //create scanner

System.out.println("Please enter your loan amount.");
loanamount = stdin.nextDouble();                                            // Stores the total loan amount to be payed off
System.out.println("Please enter your monthly payments towards the loan.");
monthlypay = stdin.nextDouble();                                            //Stores the amount the user pays towards the loan each month
System.out.println("Please enter your annual interest.");
annualinterest = stdin.nextDouble();                                        //Stores the annual interest
System.out.println("please enter the length of the loan, in months.");
loanlength = stdin.nextDouble();                                            //Stores the length of the loan in months

monthlyinterest = annualinterest/1200;                                      //Calculates the monthly interest

System.out.println("Payment Number\t\tInterest\t\tPrincipal\t\tEnding Balance");    //Creates the header
double interest, principal;                                                 //initialize variables
int i;                                                                      

/* for loop prints out the interest, principal, and ending 
 * balance for each month. Works by calculating each, 
 * printing out that month, then calculating the next month,
 * and so on.
 */

for (i = 1; i <= loanlength; i++) {                                 
    interest = monthlyinterest * loanamount;
    principal = monthlypay - interest;
    loanamount = loanamount - principal;
    System.out.println(i + "\t\t" + interest
    + "\t\t" + "$" + principal + "\t\t" + "$" + loanamount);
    }
        }
    }

Solution

  • any ideas on how to modify this to include subroutines?

    Well, you are better off doing it the other way around; i.e. working out what the methods need to be before you write the code.

    What you are doing is a form or code refactoring. Here's an informal recipe for doing it.

    1. Examine code to find a sections that perform a specific task and produces a single result. If you can think of a simple name that reflects what the task does, that it a good sign. If the task has few dependencies on the local variables where it currently "sits" that is also a good sign.
    2. Write a method declaration with arguments to pass in the variable values, and a result type to return the result.
    3. Copy the existing statements that do the task into the method.
    4. Adjust the new method body so that references to local variables from the old context are replaced with references to the corresponding arguments.
    5. Deal with the returned value.
    6. Rewrite the original statements as a call to your new method.
    7. Repeat.

    An IDE like Eclipse can take care of much of the manual work of refactoring.

    However, the real skill is in deciding the best way to separate a "lump" of code into discrete tasks; i.e. a way that will make sense to someone who has to read / understand your code. That comes with experience. And an IDE can't make those decisions for you.

    (And did I say that it is easier to design / implement the methods from the start?)