简体   繁体   中英

Error cannot find symbol for simple investment class

Hello im having trouble as to why i am getting the error cannot find symbol for my simple investments class this is a compiler error.

public class Investments
{
   //instance variables 
   private double moneyInvested;
   private double investRate;
   private int numOfYears;

   //construscts a investment with the parameters moneyInvested, double investRate, int numOfYears
    public Investments(double moneyInvested, double investRate, int numOfYears)
   {  
      double amount = moneyInvested;
      double rate = investRate;
      int time = numOfYears;
   }

   public double ruleOf72()
   {  
      return (72/rate);
   }

   public int simpleAnnual()
   {
      return  Math.round(amount * Math.pow(1 + rate, time));
   }

   public int compoundAnnual()
   {  
      return  Math.round(amount * Math.pow((1 +rate)^time));
   }

}

The variables declared here

double amount = moneyInvested;
double rate = investRate;
int time = numOfYears;

are all local variables . They cannot be accessed outside the block they are defined in, ie. outside the constructor body.

You probably meant to use your instance fields.

this.moneyInvested = moneyInvested;
// ... etc.

You should refactor the rest of your code to also use those instance variables.

try this way

public class Investments {
    // instance variables
    private double moneyInvested;
    private double investRate;
    private int numOfYears;

    double amount;
    double rate;
    int time;

    public Investments(double moneyInvested, double investRate, int numOfYears) {
        this.amount = moneyInvested;
        this.rate = investRate;
        this.time = numOfYears;
    }

    public double ruleOf72() {
        return (72 / this.rate);
    }

    public int simpleAnnual() {
        return Math.round(this.amount * Math.pow(1 + this.rate, this.time));
    }

    public int compoundAnnual() {
        return Math.round(this.amount * Math.pow((1 + this.rate) ^ this.time));
    }

}

The names of your constructor parameters do not have to exist as local variables in your class. I could have made the names the same, but for clarity I have used your internal names.

public class Investments
{
    //instance variables 
    private double amount;
    private double rate;
    private int time;

    //constructs an investment with the parameters moneyInvested, investRate, numOfYears
    public Investments(double moneyInvested, double investRate, int numOfYears)
    {  
        amount = moneyInvested;
        rate = investRate;
        time = numOfYears;
    }

    public double ruleOf72()
    {  
        return (72/rate);
    }

    public int simpleAnnual()
    {
        return  Math.round(amount * Math.pow(1 + rate, time));
    }

    public int compoundAnnual()
    {  
        return  Math.round(amount * Math.pow((1 +rate)^time));
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM