Results 1 to 6 of 6

Thread: Arrays

  1. #1
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Arrays

    Hi can somebody please help me or give me some direction on how I am supposed to do this: Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:
    * - 7 year at 5.35%
    * - 15 year at 5.5%
    * - 30 year at 5.75%
    Use an array for the different loans. Display the mortgage payment amount for each loan.
    I dont even know if I am even close with the arrays but I think I need to add formulas to this to. I just am completely lost. Here is what I have. (I know I am not even close) Please help!!

    package mortgagecalculator2;

    /**
    *
    * @author
    */
    public class MortgageCalculator2 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    double loanAmount[]=new double[1];
    loanAmount[0]=200000.00;

    double loanLengthInMonths[]=new double[3];
    loanLengthInMonths[0]=84;
    loanLengthInMonths[1]=180;
    loanLengthInMonths[2]=360;

    double monthlyInterest[]=new double[3];
    monthlyInterest[0]=5.35;
    monthlyInterest[1]=5.5;
    monthlyInterest[2]=5.75;


    System.out.println(loanAmount[0]);
    System.out.println(loanLengthInMonths[0]);
    System.out.println(monthlyInterest[0]);
    System.out.println(loanAmount[0]);
    System.out.println(loanLengthInMonths[1]);
    System.out.println(monthlyInterest[1]);
    System.out.println(loanAmount[0]);
    System.out.println(loanLengthInMonths[2]);
    System.out.println(monthlyInterest[2]);

    }



    }

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Arrays

    Check this:
    Java Code:
    import java.util.Scanner;
    
    public class LoanAmortizationShedule {
        public static void main(String[] args) {
            Scanner stdin = new Scanner(System.in);;                        
            
            System.out.println("Enter loan amount ($):");        
            double loanAmount = stdin.nextDouble();
            
            System.out.println("Enter number of years:");
            int numberOfYears = stdin.nextInt();
            
            System.out.println("Enter interest rate (%):");
            double interestRate = stdin.nextDouble();
            
            double monthlyInterestRate = interestRate / 1200;
            
            // Compute mortgage (monthly payment)
            double monthlyPayment = loanAmount * monthlyInterestRate /
                    (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
            
            //Compute total payment
            double totalPayment = monthlyPayment * numberOfYears * 12;
            
            //printing table               
            System.out.printf("Loan Amount: %1.2f\n", loanAmount);
            System.out.printf("Number of years: %d\n", numberOfYears);
            System.out.printf("Annual Interest Rate: %1.4f%%\n\n", interestRate);
            
            System.out.printf("Monthly payment: %1.2f\n", monthlyPayment);
            System.out.printf("Total Payment: %1.2f\n\n", totalPayment);
            
            double balance = loanAmount; //balance initialization
            System.out.printf("Payment#     Interest     Principal    Balance\n");
            
            for (int i = 1; i <= numberOfYears * 12; i++) {
                double interest = monthlyInterestRate * balance;
                double principal = monthlyPayment - interest;
                balance = balance - principal;
                
                System.out.printf("%-13d%-13.2f%-13.2f%-1.2f", i, interest, principal, balance);
                System.out.println();
            }
        }
    }
    Tips:
    * Total payment = monthly payment * 12
    * balance(initial) = loan amount
    * interest = interest rate / 12 * balance
    * principal = monthly payment - interest.
    * balance = balance - principal

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Arrays

    Quote Originally Posted by diamonddragon View Post
    Check this:....
    diamonddragon, sure it's a lot easier for any of us to solve his problem in its entirety and post it, but in doing this, what does the original poster gain? Learn how to beg for a solution and then copy it? Not much benefit in that and it will only reinforce the wrong behavior. It's a lot harder and takes a lot more time and involvement to give hints and suggestions so that you gently nudge him in the direction of a solution and then go through his code iterations towards the solution, but in doing the latter, the OP would gain much more.

    What I'm getting at is please don't post full solution answers. Again it may be a lot easier to do, but it's just plain wrong.

    Edit: Oh, and the OP will often come back and protest, stating that he learns much more from full solutions, but don't believe it as we've seen time and again that it doesn't help as much as doing it the hard way. I think that we have to sweat a little to truly learn.
    Last edited by Fubarable; 01-22-2012 at 07:02 AM.

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Arrays

    Well that wasn't full post solution, just a hint with same matter, with usefull formulas, and it needs lots of work in order to get final solution.
    Anyway, I respect Your suggestions and will have it in mind in the future.
    Last edited by diamonddragon; 01-22-2012 at 07:11 AM.

  5. #5
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Arrays

    Although he did post this and I thank you for that i cannot turn that in because it is way to good of code in that my professor would obviously know. I do need to be pointed in the right direction though because i have no idea what or how this code was written. Is any of my code usable?

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Arrays

    This:
    double monthlyInterest[]=new double[3];
    monthlyInterest[0]=5.35;
    monthlyInterest[1]=5.5;
    monthlyInterest[2]=5.75;

    can be writen as :
    double monthlyInterest[] = {5.35, 5.5, 5.75};

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  2. Arrays
    By ayiliath in forum New To Java
    Replies: 4
    Last Post: 10-18-2010, 10:01 PM
  3. Using Arrays
    By RevGav in forum New To Java
    Replies: 2
    Last Post: 05-25-2010, 04:57 AM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •