Results 1 to 5 of 5
  1. #1
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default Method for populating elements of an array

    I'm new on this forum, and it looks like exactly what I'm looking for, so hi to all!
    Ok, I've been looking for an answer to this question and I am perhaps not wording it right in my searches or I'm not understanding what I'm reading correctly, but if I have an array and I want to have the answers from a loop populate the array how would I go about this? I'll post the program and loop I'm looking at specifically below, however this is for a school assignment and I don't want anyone to do it for me, but I would like to understand how I can do what I want to do. If perhaps someone posts an example you can do it for some other program or in some different way so as not to do the program for me that's great, I just want to get clear that I'm not looking for anyone to do my work for me but I'd like to understand what I'm missing! ;)

    Ok specific program is here

    Java Code:
    import java.text.*;
    public class Mortgage2 {
        double intrate, term, loan, monthint;
    
            Mortgage2(double i,double  t,double  l,double  mi)
        {
                intrate = i;
                term = t;
                loan = l;
                monthint = mi;
    
            }
    
            //calculating monthly payments
            double payment() {
                return loan * (monthint * (Math.pow((1+monthint),term)))/(Math.pow((1+monthint),term)-1); //Monthrate = loan * [c(1+c)^n]/[(1 + c)^n -1] Determine monthly rate
            }
            public static class MortgageBalance {
              double interest, principle, payment, loanRemainder;
              MortgageBalance(double intr, double princ, double pay, double loanR)
              {
                interest = intr;
                principle = princ;
                payment = pay;
                loanRemainder = loanR;
              }
              
              
              //Calculate interest payed per payment
            double interest () {
              return loanRemainder * (.0575/12);
            }
            
            //Calculate principle payed per payment
            double principle () {
              return payment - interest;
            }
            //Calculate loan remainder
            double loanRemainder (){
              return ((200000 * (1 + (.0575/12))) - payment);
            }
            //Calculate loan remainder after first run
            double loanBalance (){
            return ((loanRemainder * (1 + (.0575/12))) - payment);
            }
            }
        public static void main(String[] args) {
           double payment1;
           double loan = 200000;
                   Mortgage2 mortgage1 = new Mortgage2 (.0575, 360, loan, .0575/12);
            payment1 = mortgage1.payment();
            System.out.println("Monthly Payment is" +" "+ "$" + payment1);
            System.out.println("Total Loan is $200,000");
            System.out.println("Interest rate is 5.75%");
            System.out.println("Term is 30 years." );
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            System.out.println("Payment #        Payment        Interest        Principle        Balance");
            double loanRemainder1;
            loanRemainder1 = 200000 + (200000 * .0575/12) ;
            double interest1;
            interest1 = loanRemainder1 * (.0575/12);
            double principle1 = payment1 - interest1;
            int paymentCounter = 1;
            while (loanRemainder1 > 0 && paymentCounter < 361)
            {
              Mortgage2 mortgage = new Mortgage2 (.0575, 360, loan, .0575/12);
              MortgageBalance balance = new MortgageBalance (interest1, principle1, payment1, loanRemainder1);
              loanRemainder1 = balance.loanBalance();
              interest1 = balance.interest();
              principle1 = balance.principle();
              DecimalFormat df = new DecimalFormat("#.##");
              System.out.println(paymentCounter + "               " + "$" + df.format(payment1) + "       " + "$" + df.format(interest1) + "         " + "$" + df.format(principle1) + "          " + "$" + df.format(loanRemainder1));
              paymentCounter++;
              
              }
        }
    }
    I know the math is wrong here, and there's something wrong with the loop, I'm rewriting the program anyways, however I initially hoped to have instead of the System.out.println function using each individual answer I wanted to have each answer, payment1, principle1, loanRemainder1, etc populate an array and then I could just print the array but I don't know how to code the answers to populate an array, can anyone point me in the right direction? I guess what I was looking for was to have the outputs of the methods, loanRemainder, principle, interest etc, populate an array. I appreciate the help!
    Last edited by gaborn415; 02-26-2011 at 04:25 AM. Reason: Found a better way to word my question

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Here's an even easier solution. You already have a MortgageBalance class, so just implement its toString() method to output all of its data in one line. Then, in the while loop, instantiate a MortgageBalance with the appropriate values, then call System.out.println(mb.toString()).

  3. #3
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Thanks for your reply, I don't know what the toString() method is, and I'm not sure I understand the rest, what would be the appropriate values in the while loop? Would it be the same values I'm using here?
    Java Code:
    MortgageBalance (interest1, principle1, payment1, loanRemainder1);
    I'm sorry I'm really new to Java, first ever programming class aside from a class that talked about psuedocode about a year ago and I really kept throwing things at this program until something stuck, I kinda get how it works, but to be honest I did a lot of just using the syntax as given in the book and Java video tutorials to put it together, which is why I know there's a problem with the while loop, or the math but I can't pinpoint it. Is (mb.toString()) MortgageBalance.toString? Where can I learn about the proper use of toString() in dummy terms? Or could you perhaps put an example of the toString together in some other simple way to see how it works? The java documentation at Oracle is thorough but a bit difficult for me to understand, I learn better audially so reading is sometimes difficult for my comprehension. Thanks for the reply and I'm not trying to be obtuse, or to get you to write everything out for me, I'm just trying to find resources that work well for me and my way of understanding things.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    class MyClass{
      int x;
      public String toString(){
        return "my classes to string method " + x;
      }
    }
    After overriding toString you can simply call
    Java Code:
    System.out.println(theItem);
    and it will automatically display the items toString method.

    You ask about arrays but I don't see where you intend to use one. Is the goal of this assignment to display every step of the payment until it is paid off?
    Last edited by sunde887; 02-26-2011 at 03:01 PM.

  5. #5
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Sorry I've been so long away from this, between school, work and being very sick I've been sleeping a lot. Feeling better now and coming up on the final assignment. I hate to admit my ignorance here but I'm still confused about the toString method you posted. I'm going to reread it and see if I get a better understanding. Let me update you on what I changed and did for the new assignment. We had to use an array to store the variables of 3 different loans and display the monthly payments for each I did this as shown below...

    Java Code:
    import java.text.*;
    public class MortageCalc
    {
      //Declare variables which make up loan
      double interestRate;
      double term;
      double loanAmount;
      
      
      //Constructor to create Mortgage instance
      public MortageCalc (double iR, double t, double lA)
      {
        interestRate = iR;
        term = t;
        loanAmount = lA;
      }
      
      //Method to determine Monthly payment
            double payment() {
                return loanAmount * ((interestRate/12) * (Math.pow((1+(interestRate/12)),(term * 12))))/(Math.pow((1+(interestRate/12)),(term * 12) )-1); //Monthrate = loan * [c(1+c)^n]/[(1 + c)^n -1] Determine monthly rate
            }
      
      
    public static void main (String[] args) 
    {
      //Declare variables
      double payment1;
      double payment2;
      double payment3;
      
      //Create array with loan variables
      double[][] loans = new double [3][3];
      loans [0][0] = .0535;
      loans [0][1] = 7;
      loans [0][2] = 200000;
      loans [1][0] = .055;
      loans [1][1] = 15;
      loans [1][2] = 200000;
      loans [2][0] = .0575; 
      loans [2][1] = 30;
      loans [2][2] = 200000;
      
      //Create new loan instances and calculate monthly payments
      MortageCalc m1 = new MortageCalc (loans [0][0], loans [0][1], loans [0][2]);
      MortageCalc m2 = new MortageCalc (loans [1][0], loans [1][1], loans [1][2]);
      MortageCalc m3 = new MortageCalc (loans [2][0], loans [2][1], loans [2][2]);
      payment1 = m1.payment();
      payment2 = m2.payment();
      payment3 = m3.payment();
      DecimalFormat df = new DecimalFormat("#,###.##");
      
      //Print payment results
      System.out.println("Monthly payment for loan #1 is $" + df.format(payment1));
      System.out.println("Monthly payment for loan #2 is $" + df.format(payment2));
      System.out.println("Monthly payment for loan #3 is $" + df.format(payment3));
    }
    }
    Now for this week we need to convert the program to use OOP concepts where the main method should contain no data or logic other than that to create the base object. I need to choose one of the three loans to print the amoritization table. I think I'm pretty far along with this one and I figured out where my math was awry in the first program. Is the way the program currently created consistent with the requirements is my first question. and my second is, is it possible to create a constructor to create an array and to populate the array? If so how. Otherwise I think I figured out what to do. I appreciate all your help!

Similar Threads

  1. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 09:49 PM
  2. Replies: 8
    Last Post: 01-15-2011, 10:39 PM
  3. incrementing array elements
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 03:37 PM
  4. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 09:36 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 06:33 PM

Posting Permissions

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