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

S

S. Gregory

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.
 
B

Bjorn Abelli

...
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top