views:

81

answers:

3

Hi. There are 2 files named:

  1. AnnuityDueGUI.java
  2. AnnuityDueResultGUI.java

Under AnnuityDueGUI.java, there is this method as below:

=============

public double calculateFADGUI(){
        //FVA = A{[(1+i)^n – 1] / i} (1+i)
        String amountStr = amount.getText() ;  //convert string to double
        dAmount = Double.parseDouble(amountStr) ;
        String iStr = iText.getText() ;
        dInterest = Double.parseDouble(iStr) ;
        String periodStr = period.getText() ;
        dPeriod = Double.parseDouble(periodStr) ;
        iPeriod = (int)dPeriod ;
        due = new Annuity(dAmount, dInterest, iPeriod) ;
        System.out.println(due.calculateFAD()) ;
        return due.calculateFAD() ;   //calculateFAD() is under Annuity.java
    }

===============

Under AnnuityDueResultGUI.java, how to grab the result from the method that I've stated above?both classes are under the same package "GUI". I also did import AnnuityDueGUI.* ;

But still have no idea on how to grab the result from AnnuityDueGUI.java and display it under AnnuityDueResultGUI.java.

Please assist and thanks in advance.

+5  A: 

You will need a reference to the AnnuityDueGUI object in AnnuityDueResultGUI. So for instance

AnnuityDueGUI adg = new AnnuityDueGUI()
double result = adg.calculateFADGUI()

UPDATE:

Also if you happen to already be constructing the AnnuityDueGUI somewhere else you can just pass the reference to AnnuityDueResultGUI

public AnnuityDueResultGui(AnnuityDueGUI adg) {
    this.adg = adg;
}

private void otherFunc () {
    double results = this.adg.calculateFADGUI()
}
Matti
exactly what I'm answering...I need to be faster ;)
Sylvain M
I'm not sure where the amount variable is stored, but if it is not part of AnnuityDueGUI, then consider making calculateFADGUI static and passing in amount.
Quenton Jones
A: 
class AnnuityDueGUI {

public double calculateFADGUI(){
        //FVA = A{[(1+i)^n – 1] / i} (1+i)
        String amountStr = amount.getText() ;  //convert string to double
        dAmount = Double.parseDouble(amountStr) ;
        String iStr = iText.getText() ;
        dInterest = Double.parseDouble(iStr) ;
        String periodStr = period.getText() ;
        dPeriod = Double.parseDouble(periodStr) ;
        iPeriod = (int)dPeriod ;
        due = new Annuity(dAmount, dInterest, iPeriod) ;
        System.out.println(due.calculateFAD()) ;
        return due.calculateFAD() ;   //calculateFAD() is under Annuity.java
    }

//...other code
}

class AnnuityDueResultGUI {

AnnuityDueGUI var = new AnnuityDueGUI();
double result = var.calculateFADGUI();
//other code
}
Zaki
A: 

Create AnnuityDueResultGUI with a reference to the actual AnnuityDueGUI instance, then you can access the method:

public class AnnuityDueResultGUI {
  private AnnuityDueGUI parent;

  public AnnuityDueResultGUI(AnnuityDueGUI parent) {
    this.parent = parent;
  }

  public void somemethod() {
    parent.calculateFADGUI();  // now you can access a method from a AnnuityDueGUI  instance
  }
}

and somewhere inside a method of AnnuityDueGUI:

// ...
AnnuityDueResultGUI resultGUI = new AnnuityDueResultGUI(this);
// ...
Andreas_D