How to create Arrays with variables from another class??????

Discussion in 'Java' started by S. Gregory, May 12, 2004.

  1. S. Gregory

    S. Gregory Guest

    I am having difficulty when attempting to create an Array in the
    following source code.


    FIRST CLASS CREATED....................

    import java.text.*;

    public class MortgageCalculation {

    public double loanAmount;
    public double interestRate;
    public int loanTerm;

    //More variables are listed, but they are declared private for the
    calculations.


    public void displayPayment()throws Exception {

    //Math formulas go here

    //End of this class

    SECOND CLASS THAT IS USED FOR DISPLAY OF MORTGAGE AMOUNTS.....

    class MortgageDisplay {

    public static void main(String[] arguments)throws Exception {

    MortgageCalculation loanOne = new MortgageCalculation();
    MortgageCalculation loanTwo = new MortgageCalculation();
    MortgageCalculation loanThree = new MortgageCalculation();

    loanOne.loanAmount = 200000.00; // Here is where I set the
    variables.
    loanTwo.loanAmount = 200000.00; // I would like to type this amount
    once since it is utilized by all three loans.
    loanThree.loanAmount = 200000.00;

    loanOne.loanTerm = 84;
    loanTwo.loanTerm = 180;
    loanThree.loanTerm = 360; //How can I create an array for all
    three?????

    loanOne.interestRate = .0535;
    loanTwo.interestRate = .0550; //I think you get the picture..

    Please help?? After a few days in books I have come up short and need
    the help of someone with more expirence. Thank You.
     
    S. Gregory, May 12, 2004
    #1
    1. Advertising

  2. S. Gregory

    Bjorn Abelli Guest

    "S. Gregory" wrote...

    > I am having difficulty when attempting to
    > create an Array in the following source code.


    To begin with, I think you're starting from the wrong angle.

    If you read up on what OO programming is all about, you'll see that there
    would be several shortcuts for you down the road.

    For starters, you could simplify much work done, just by using the first
    class *as* a class, not just an UDT.

    For example, create a constructor for MortgageCalculation like this:


    public MortgageCalculation
    (double loanAmount,
    double interestRate,
    int loanTerm)
    {
    this.loanAmount = loanAmount;
    this.interestRate = interestRate;
    this.loanTerm = loanTerm;
    }

    You should think about how the actual values for the loans should be put
    into the program. It wouldn't make much of a program if *all* values were
    hardcoded, would it?

    Anyway, with the constructor in place, you could use that instead.

    MortgageCalculation loanOne =
    new MortgageCalculation(200000, .0535, 84);
    MortgageCalculation loanTwo =
    new MortgageCalculation(200000, .0550, 180);
    MortgageCalculation loanThree =
    new MortgageCalculation(200000, .0580, 360);

    You get the picture? :)

    You should also think about what type of "array" you want, where there's
    several possibilities.

    MortgageCalculation[] = new MortgageCalculation[3];

    ....which now have space for three object references, or use an ArrayList,
    which is dynamic and can have references to an arbitrary number of objects.

    Anyway, read up on OO programming, and next time you have any questions,
    post them in comp.lang.java.help instead.

    // Bjorn A
     
    Bjorn Abelli, May 12, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vinuta
    Replies:
    1
    Views:
    386
    =?Utf-8?B?S29zdGFkaW4gS29zdG92?=
    Jan 31, 2005
  2. cyril
    Replies:
    2
    Views:
    3,951
    cyril
    Aug 25, 2004
  3. E11
    Replies:
    1
    Views:
    4,952
    Thomas Weidenfeller
    Oct 12, 2005
  4. Replies:
    9
    Views:
    1,015
  5. Philipp
    Replies:
    21
    Views:
    1,190
    Philipp
    Jan 20, 2009
Loading...

Share This Page