Solved

Help! having problem calculating compound annual investment.

Posted on 2004-10-14
23
478 Views
Last Modified: 2012-08-13
I am having trouble implementing the annual compound portion of the calculation that should be based on the user input for years invested.
Am I right in thinking that this should be placed between:

 the calculations and the displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);     ???

when I place it I am getting one compile error:       line 110: ';' expected double compoundInterest(){

Thank you for your time, lauren

Here is the latest code:

import javax.swing.*;
import BreezySwing.*;

  public class Mutualfund8 extends GBFrame {

             // variables for window objects

     private JLabel        initialPrincipleLabel;
     private DoubleField   initialPrincipleField;
     private JLabel            interestRateLabel;
     private DoubleField   interestRateField;
     private JLabel        yearsFundLabel;
     private IntegerField  yearsFundField;
     private JButton            calculateButton;
     private JLabel        yearEndPrincipleLabel;
    private JTextArea     output;


              // instance variables

     private int yearsFund;
     private double initialPrinciple;
     private double earnedInterest;          // initial principle * interest rate
     private double yearEndPrinciple;    // initial principle + earned interests



                // constructor

  public Mutualfund8 (){

               // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                        Format.justify('r', "Principle", 18) +
                       Format.justify('r', "Interest rate", 20) +
                       Format.justify('r', "Year end Principle", 21) +  "\n";


               // instantiate window objects


           initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
           initialPrincipleField = addDoubleField   (0,1,2,1,1);
           interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
           interestRateField     = addDoubleField (0,2,2,1,1);
           yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
           yearsFundField        = addIntegerField  (0,3,2,1,1);
           calculateButton       = addButton        ("       Calculate",4,1,1,1);
           output                = addTextArea      (header,5,1,4,4 );


     output.setEnabled(false);
     initialPrincipleField.requestFocus();
     interestRateField.requestFocus();
     yearsFundField.requestFocus();

               // initialize double fields to zero

     //intialPrinciple = 0;
     //interestRate = 0;
     //yearsFund = 0;
}
  public void buttonClicked (JButton buttonObj){
          if (buttonObj == calculateButton){
                    processInputs();
          } else {
               calculateButton.setEnabled(false);

               displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);
          }
}

     // read the inputs, compute the investment informatiom, display and format the year, principle, interest earned and the year end total


   private void processInputs(){

     //declare local variables

          double initialPrinciple;
          double interestRate;
          int yearsFund;
          double earnedInterest;
          double yearEndPrinciple;
          double compoundInterest;
          double futureValue;


          initialPrinciple = initialPrincipleField.getNumber();
          interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();

        //calculations
        x = initialPrinciple(1 + interestRate);
          n = yearsFund;

          interestRate = (interestRate / 100);
        earnedInterest = (initialPrinciple * interestRate);
         yearEndPrinciple = (earnedInterest + initialPrinciple);

   double compoundInterest(){
   double initialPrinciple = initialprincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double futureValue = initialPrinciple * Math.pow((1+ interestRate/100.0), yearsFund);
   return futureValue;
}





                          //static double pow(double x, int n){

                         //yearEndPrinciple = Math.pow(double yearEndPrinciple, int yearsFund)
                           //yearsFund

                            // displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);

                        //yearEndPrinciple = initialPrinciple;

                          // for(yearsFund = 1; yearsFund<=10; yearsFund = yearsFund+1)

                          // {
                         //     yearEndPrinciple += yearEndPrinciple * interestRate;

                         //     }

 }
   private void displayNumbers(int num1, double num2, double num3, double num4){
                    String numberLine = Format.justify('l', num1, 15) +
                                                Format.justify('r', num2, 15, 2) +
                                                Format.justify('r', num3, 15, 2) +
                                               Format.justify('r', num4, 18, 2);
                              output.append (numberLine + "\n");
}


   public static void main(String[] args){

               Mutualfund8 theGUI = new Mutualfund8 ();
               theGUI.setLookAndFeel("Motif");
               theGUI.setSize (550,500);          //width = 500, height = 500 in pixels
               theGUI.setVisible (true);          //window is visible
     }
}
0
Comment
Question by:Brayn66
  • 9
  • 9
  • 4
  • +1
23 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 60 total points
ID: 12316026
you braces are a bit screwed up

>    double compoundInterest(){

whats the intention here?

> }          // <<<<***--- doesn't look like this is needed
>    private void displayNumbers(int num1, double num2, double num3, double num4){
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 140 total points
ID: 12316030
working code
compile and run


import java.awt.*;
import javax.swing.*;

import BreezyGUI.*;

  public class Mutualfund6 extends GBFrame{

             // variables for window objects

     private Label         initialprincipleLabel;
     private DoubleField   initialprincipleField;
     private Label            interestRateLabel;
     private IntegerField  interestRateField;
     private Label         yearsFundLabel;
     private DoubleField   yearsFundField;
     private Button            calculateButton;
     private Button            myButton;
    private TextArea      output;
    private Label yearEndPrincipleLabel;


              // instance variables
     private double earnedInterest;          // initial principle * interest rate
     private double yearEndPrinciple;    // initial principle + earned interests

              // constructor

     private double intialPrinciple = 0;
     private double interestRate = 0;
     private double yearsFund = 0;


  public Mutualfund6(){

               // table header's line

  String  header = Format.justify('l', "Year", 12) +
                        Format.justify('r', "Principle", 15) +
                       Format.justify('r', "Interest rate", 18) + "\n";


               // instantiate window objects


           initialprincipleLabel = addLabel         ("Enter principle",1,1,1,1);
           initialprincipleField = addDoubleField   (0,1,2,1,1);
           interestRateLabel     = addLabel         ("Interest Rate",2,1,1,1);
           interestRateField     = addIntegerField (0,2,2,1,1);
           yearsFundLabel        = addLabel         ("Years Invested",3,1,1,1);
           yearsFundField        = addDoubleField  (0,3,2,1,1);
           calculateButton       = addButton        ("Calculate",4,1,1,1);
           yearEndPrincipleLabel = addLabel         ("Year end principle",5,1,1,1);
           output           = addTextArea      (header,4,1,4,4 );
           myButton       = addButton        ("My Button",6,1,1,1);

     output.setEnabled(false);
     initialprincipleField.requestFocus();
     interestRateField.requestFocus();
     yearsFundField.requestFocus();

     // initialize double fields to zero

     intialPrinciple = 0;
     interestRate = 0;
     yearsFund = 0;


}



  public void buttonClicked (Button buttonObj){

          if (buttonObj.equals(calculateButton)){
                    System.out.println("calculate button clicked :" );
            }else if(buttonObj.equals(myButton)){
                  System.out.println("my button clicked :");
            }
          /*if (buttonObj == calculateButton)  {
                    processInputs();

          } else {
               //displayDashes();
               //displayNumbers (initialPrinciple, interestRate, yearsFund);
          }*/
          processInputs();
      System.out.println("button clicked :" + buttonObj);
}


     // read the inputs, compute the invest informatiom, display and format the year, principle, interest earned and the year end total


   private void processInputs(){

     //declare local variables

          double initialPrinciple;
          double interestRate;
          double yearsFund;
          double earnedInterest;
          double yearEndPrinciple;

          initialPrinciple = initialprincipleField.getNumber();
          interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();


        //calculate

        earnedInterest = (initialPrinciple * interestRate);
          yearEndPrinciple = (earnedInterest + initialPrinciple);

                  yearEndPrinciple = compoundInterest();

          displayNumbers (initialPrinciple, interestRate, yearsFund, yearEndPrinciple);


 }
   private void displayNumbers (double num1, double num2, double num3, double num4){
                    String dashLine = Format.justify('l', num1, 9, 2) +
                                              Format.justify('r', num2, 12, 2) +
                                             Format.justify('r', num3, 15, 2) +
                                             Format.justify('r', num4, 18, 2) +
                                             "\n";
                              output.append (dashLine + "\n");
}




     public static void main(String[] args){

               Mutualfund6 theGUI = new Mutualfund6 ();
               theGUI.setSize (200,300);          //width = 300, height = 300 in pixels
               theGUI.setVisible (true);          //window is visible

     }


      /*
                        S=P(1+I)^n
       S = future value (yearEndPrinciple)
       P = principle amount (initialPrinciple)
       I = interest rate (interestRate)
        n = number of years to compound (yearsFund)
      */
      double compoundInterest(){
              double initialPrinciple = initialprincipleField.getNumber();
        double interestRate = interestRateField.getNumber();
        double yearsFund = yearsFundField.getNumber();
            double futureValue = initialPrinciple * Math.pow((1+ interestRate/100.0), yearsFund);
            return futureValue;
      }
}
0
 

Author Comment

by:Brayn66
ID: 12317922
objects,
I have had a time with putting the finishing touches on this assignment. if you see my spec fom above, I am only missing the compounding annually portion of this equation.

The     double compoundInterest(){  }    this was suggested by expertmb but I am not sure if it works, or where it belongs.

below is my code that does what the program should do, (except compound the interest annually when the user enters  a certain number of years to invest).

thank you for helping lauren



import javax.swing.*;
import BreezySwing.*;

  public class Mutualfund6 extends GBFrame{

              // variables for window objects

      private JLabel        initialPrincipleLabel;
      private DoubleField   initialPrincipleField;
      private JLabel              interestRateLabel;
      private DoubleField   interestRateField;
      private JLabel        yearsFundLabel;
      private IntegerField  yearsFundField;
      private JButton              calculateButton;
      private JLabel        yearEndPrincipleLabel;
    private JTextArea     output;


               // instance variables

      private int yearsFund;
      private double initialPrinciple;
      private double earnedInterest;            // initial principle * interest rate
      private double yearEndPrinciple;    // initial principle + earned interests



                   // constructor

  public Mutualfund6(){

                 // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                           Format.justify('r', "Principle", 18) +
                           Format.justify('r', "Interest rate", 20) +
                           Format.justify('r', "Year end Principle", 21) +  "\n";


                 // instantiate window objects


             initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
             initialPrincipleField = addDoubleField   (0,1,2,1,1);
             interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
             interestRateField     = addDoubleField (0,2,2,1,1);
             yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
             yearsFundField        = addIntegerField  (0,3,2,1,1);
             calculateButton       = addButton        ("       Calculate",4,1,1,1);
             output                = addTextArea      (header,5,1,4,4 );


      output.setEnabled(false);
      initialPrincipleField.requestFocus();
      interestRateField.requestFocus();
      yearsFundField.requestFocus();

                  // initialize double fields to zero

      //intialPrinciple = 0;
      //interestRate = 0;
      //yearsFund = 0;
}
  public void buttonClicked (JButton buttonObj){
            if (buttonObj == calculateButton){
                        processInputs();
            } else {
                  calculateButton.setEnabled(false);

                  displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);
            }
}

      // read the inputs, compute the investment informatiom, display and format the year, principle, interest earned and the year end total


   private void processInputs(){

      //declare local variables

            double initialPrinciple;
            double interestRate;
            int yearsFund;
            double earnedInterest;
            double yearEndPrinciple;

            initialPrinciple = initialPrincipleField.getNumber();
            interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();

        //calculate
            interestRate = (interestRate / 100);
        earnedInterest = (initialPrinciple * interestRate);
          yearEndPrinciple = (earnedInterest + initialPrinciple);

            displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);


 }
   private void displayNumbers(int num1, double num2, double num3, double num4){
                        String numberLine = Format.justify('l', num1, 12) +
                                                       Format.justify('r', num2, 15, 2) +
                                                       Format.justify('r', num3, 15, 2) +
                                                       Format.justify('r', num4, 18, 2);
                                    output.append (numberLine + "\n");
}


   public static void main(String[] args){

                  Mutualfund6 theGUI = new Mutualfund6 ();
                  theGUI.setLookAndFeel("Motif");
                  theGUI.setSize (550,500);            //width = 550, height = 500 in pixels
                  theGUI.setVisible (true);            //window is visible
      }
}


--------------------------------------------------------------------------------------------
expertmb,

I can only import javax.swing and BreezySwing for use in my GUI.
I tried to compile and run your suggested (comment above) code but get the following compile errors.

C:\ex\Mutualfund9.java:5: package BreezyGUI does not exist
import BreezyGUI.*;
^
C:\ex\Mutualfund9.java:7: cannot resolve symbol
symbol  : class GBFrame
location: class Mutualfund9
  public class Mutualfund9 extends GBFrame{
                                   ^
C:\ex\Mutualfund9.java:12: cannot resolve symbol
symbol  : class DoubleField
location: class Mutualfund9
     private DoubleField   initialprincipleField;
             ^
C:\ex\Mutualfund9.java:14: cannot resolve symbol
symbol  : class IntegerField
location: class Mutualfund9
     private IntegerField  interestRateField;
             ^
C:\ex\Mutualfund9.java:16: cannot resolve symbol
symbol  : class DoubleField
location: class Mutualfund9
     private DoubleField   yearsFundField;
             ^
C:\ex\Mutualfund9.java:38: cannot resolve symbol
symbol  : variable Format
location: class Mutualfund9
  String  header = Format.justify('l', "Year", 12) +
                   ^
C:\ex\Mutualfund9.java:39: cannot resolve symbol
symbol  : variable Format
location: class Mutualfund9
                        Format.justify('r', "Principle", 15) +
                        ^


The system is out of resources.                                      //i have not gotten this one before
Consult the following stack trace for details.
java.lang.OutOfMemoryError

Tool completed with exit code 3


thanks!!  lauren
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12317943
it is recognising the BreezyGUI.jar. put that jar in classpath.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12317950
typo,
it is not  recognising the BreezyGUI.jar
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12318902
I dont think your processInput() actually calculate the compound interest for a specified number of years. It looked to me that your method only workout the amount for one year only!

Here is the formula for working out the compound interest of a lump-sum for n years:

amountAfter_N_Years = initialLumpSum x (1+interestRate/100) ^ n

e.g. £1000 at an annual rate of 20% for 10 years:

amountAfter_N_Years = 1000 x 1.2^10 = 6191.74

If you want to display a table of how the money grows over the year, your processInput() should be like this:

double processInput(double initialSum, double annualInterestRate, double numYears){
         return initialSum x Math.pow(1+100/annualInterestRate, numYears);
}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12319024
funnyveryfunny

>>return initialSum x Math.pow(1+100/annualInterestRate, numYears);
should be
return initialSum x Math.pow(1+annualInterestRate/100.0, numYears);

i had already put that method for your reference i am giving that method below

   /*
                       S=P(1+I)^n
      S = future value (yearEndPrinciple)
      P = principle amount (initialPrinciple)
      I = interest rate (interestRate)
       n = number of years to compound (yearsFund)
     */
     double compoundInterest(){
            double initialPrinciple = initialprincipleField.getNumber();
        double interestRate = interestRateField.getNumber();
        double yearsFund = yearsFundField.getNumber();
          double futureValue = initialPrinciple * Math.pow((1+ interestRate/100.0), yearsFund);
          return futureValue;
     }


0
 

Author Comment

by:Brayn66
ID: 12321676
Thank you both!

Expertmb:

I added the suggested:  BreezyGUI.jar. Here is the corrected code- I am receiving the listed error. Could this be a problem with my braces as Objects had suggested?

I also reviewed the code from funnyveryfunny, and see that as you stated I did already have that code from you, but wondered if I should be including his initial calculation to initiate a base calculation?

amountAfter_N_Years = initialLumpSum x (1+interestRate/100) ^ n

I don't believe I have this in my code, unless we have it coded an alternate way.

Thanks again, lauren


import javax.swing.*;
import BreezySwing.*;

import BreezyGUI.*;

  public class Mutualfund8 extends GBFrame {

              // variables for window objects

      private JLabel        initialPrincipleLabel;
      private DoubleField   initialPrincipleField;
      private JLabel              interestRateLabel;
      private DoubleField   interestRateField;
      private JLabel        yearsFundLabel;
      private IntegerField  yearsFundField;
      private JButton              calculateButton;
      private JLabel        yearEndPrincipleLabel;
                private JTextArea     output;


               // instance variables

      private int yearsFund;
      private double initialPrinciple;
      private double earnedInterest;            // initial principle * interest rate
      private double yearEndPrinciple;                 // initial principle + earned interests



                   // constructor

  public Mutualfund8 (){

                 // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                           Format.justify('r', "Principle", 18) +
                           Format.justify('r', "Interest earned", 20) +
                           Format.justify('r', "Year end Principle", 21) +  "\n";


                 // instantiate window objects


             initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
             initialPrincipleField = addDoubleField   (0,1,2,1,1);
             interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
             interestRateField     = addDoubleField (0,2,2,1,1);
             yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
             yearsFundField        = addIntegerField  (0,3,2,1,1);
             calculateButton       = addButton        ("       Calculate",4,1,1,1);
             output                = addTextArea      (header,5,1,4,4 );


      output.setEnabled(false);
      initialPrincipleField.requestFocus();
      interestRateField.requestFocus();
      yearsFundField.requestFocus();

                  // initialize double fields to zero

      //intialPrinciple = 0;
      //interestRate = 0;
      //yearsFund = 0;
}
  public void buttonClicked (JButton buttonObj){
            if (buttonObj == calculateButton){
                        processInputs();
            } else {
                  calculateButton.setEnabled(false);

                  displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);
            }
}

      // read the inputs, compute the investment informatiom, display and format the year, principle, interest earned and the year end total


   private void processInputs(){

      //declare local variables

            double initialPrinciple;
            double interestRate;
            int yearsFund;
            double earnedInterest;
            double yearEndPrinciple;
            double compoundInterest;
            double futureValue;

            initialPrinciple = initialPrincipleField.getNumber();
            interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();

        //calculations
        x = initialPrinciple(1 + interestRate);
            n = yearsFund;

            interestRate = (interestRate / 100);
        earnedInterest = (initialPrinciple * interestRate);
          yearEndPrinciple = (earnedInterest + initialPrinciple);

   double compoundInterest (){
   double initialPrinciple = initialprincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double futureValue = initialPrinciple * Math.pow((1+ interestRate/100.0), yearsFund);
   return futureValue;
}






                              //static double pow(double x, int n){

                              //yearEndPrinciple = Math.pow(double yearEndPrinciple, int yearsFund)
                                //yearsFund

                                 // displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);

                            //yearEndPrinciple = initialPrinciple;

                               // for(yearsFund = 1; yearsFund<=10; yearsFund = yearsFund+1)

                               // {
                              //      yearEndPrinciple += yearEndPrinciple * interestRate;

                              //      }

 }
   private void displayNumbers(int num1, double num2, double num3, double num4){
                        String numberLine = Format.justify('l', num1, 15) +
                                                       Format.justify('r', num2, 15, 2) +
                                                       Format.justify('r', num3, 15, 2) +
                                                       Format.justify('r', num4, 18, 2);
                                    output.append (numberLine + "\n");
}


   public static void main(String[] args){

                  Mutualfund8 theGUI = new Mutualfund8 ();
                  theGUI.setLookAndFeel("Motif");
                  theGUI.setSize (550,500);            //width = 500, height = 500 in pixels
                  theGUI.setVisible (true);            //window is visible
      }
}
0
 

Author Comment

by:Brayn66
ID: 12321702
Sorry - the error:

';' expected
   double compoundInterest (){
                           ^
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12324601
I can see one mistake that is your compoundInterest() is defined within processInputs() body!

So your code should be:

 private void processInputs(){

     //declare local variables

          double initialPrinciple;
          double interestRate;
          int yearsFund;
          double earnedInterest;
          double yearEndPrinciple;
          double compoundInterest;
          double futureValue;

          initialPrinciple = initialPrincipleField.getNumber();
          interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();

        //calculations
        x = initialPrinciple(1 + interestRate);
          n = yearsFund;

          interestRate = (interestRate / 100);
        earnedInterest = (initialPrinciple * interestRate);
         yearEndPrinciple = (earnedInterest + initialPrinciple);

} // <== corrected

   double compoundInterest (){
   double initialPrinciple = initialprincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double futureValue = initialPrinciple * Math.pow((1+ interestRate/100.0), yearsFund);
   return futureValue;
}


>>>return initialSum x Math.pow(1+100/annualInterestRate, numYears);
should be
return initialSum x Math.pow(1+annualInterestRate/100.0, numYears);

thanx expertmb.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12324789
since you have defined global variables:

 // instance variables

     private int yearsFund;
     private double initialPrinciple;
     private double earnedInterest;          // initial principle * interest rate
     private double yearEndPrinciple;                 // initial principle + earned interests

you don't need to redefine them within your processInputs() body:

void processInputs(){
     initialPrinciple = initialPrincipleField.getNumber();
     interestRate = interestRateField.getNumber();
     yearsFund = yearsFundField.getNumber();

     // then call your compoundInterest() here
     double yearEndPrinciple = compoundInterest();
     double interestEarned = yearEndPrinciple - initialPrinciple;

     //then call your display() which displays: a X amount gives an earnedInterest sum and total sum
     //at the end of N years
     // e.g: £ 1000 with interest rate of 20% over 10 years gives:
     // 10yrs £1000 5191.74 6191.74
     displayNumbers (yearsFund, initialPrinciple, interestEarned, yearEndPrinciple);

}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12324801
correction:
 
 double yearEndPrinciple = compoundInterest();
 double interestEarned = yearEndPrinciple - initialPrinciple;

should be:

 yearEndPrinciple = compoundInterest();
 earnedInterest = yearEndPrinciple - initialPrinciple;

and add another instance variable:

private double interestRate;
0
 

Author Comment

by:Brayn66
ID: 12324869
I have implemented the changes but cannot resolve the interface expected error that I get .  Otherwise I think it will compile! Thanks for your help!! lauren

error:
 'class' or 'interface' expected
   private void displayNumbers(int num1, double num2, double num3, double num4){
           ^
1 error
Tool completed with exit code 1



source code:

import javax.swing.*;
import BreezySwing.*;
import BreezyGUI.*;

  public class Mutualfund8 extends GBFrame {

              // variables for window objects

      private JLabel        initialPrincipleLabel;
      private DoubleField   initialPrincipleField;
      private JLabel              interestRateLabel;
      private DoubleField   interestRateField;
      private JLabel        yearsFundLabel;
      private IntegerField  yearsFundField;
      private JButton              calculateButton;
      private JLabel        yearEndPrincipleLabel;
    private JTextArea     output;


               // instance variables

      private int yearsFund;
      private double initialPrinciple;
      private double earnedInterest;            // initial principle * interest rate
      private double yearEndPrinciple;    // initial principle + earned interests



                   // constructor

     private double intialPrinciple = 0;
     private double interestRate = 0;
     private double yearsFund = 0;


  public Mutualfund8 (){

                 // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                           Format.justify('r', "Principle", 18) +
                           Format.justify('r', "Interest earned", 20) +
                           Format.justify('r', "Year end Principle", 21) +  "\n";


                 // instantiate window objects

             initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
             initialPrincipleField = addDoubleField   (0,1,2,1,1);
             interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
             interestRateField     = addDoubleField (0,2,2,1,1);
             yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
             yearsFundField        = addIntegerField  (0,3,2,1,1);
             calculateButton       = addButton        ("       Calculate",4,1,1,1);
             output                = addTextArea      (header,5,1,4,4 );


      output.setEnabled(false);
      initialPrincipleField.requestFocus();
      interestRateField.requestFocus();
      yearsFundField.requestFocus();


}
  public void buttonClicked (JButton buttonObj){
            if (buttonObj == calculateButton){
                        processInputs();
            } else {
                  calculateButton.setEnabled(false);

                  displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);
            }
}

   private void processInputs(){

      //declare local variables

            double initialPrinciple;
            double interestRate;
            int yearsFund;
            double earnedInterest;
            double yearEndPrinciple;
            double compoundInterest;
            double futureValue;

            initialPrinciple = initialPrincipleField.getNumber();
            interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();


        //calculations
        x = initialPrinciple(1 + interestRate);
            n = yearsFund;

            interestRate = (interestRate / 100);
        earnedInterest = (initialPrinciple * interestRate);
          yearEndPrinciple = (earnedInterest + initialPrinciple);


}                  // Compound

   double compoundInterest (){
   double initialPrinciple = initialprincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
   return futureValue;
}



                  //display
 }
   private void displayNumbers(int num1, double num2, double num3, double num4){
                        String numberLine = Format.justify('l', num1, 15) +
                                                       Format.justify('r', num2, 15, 2) +
                                                       Format.justify('r', num3, 15, 2) +
                                                       Format.justify('r', num4, 18, 2);
                                    output.append (numberLine + "\n");
}
                  //GUI

   public static void main(String[] args){

                  Mutualfund8 theGUI = new Mutualfund8 ();
                  theGUI.setLookAndFeel("Motif");
                  theGUI.setSize (550,500);            //width = 550, height = 500 in pixels
                  theGUI.setVisible (true);            //window is visible
                        }


thanks,
lauren
0
 

Author Comment

by:Brayn66
ID: 12324910
funnyveryfunny,
sorry I just got yor last postings after I sent mine. I will try the advice-thanks. lauren
0
 

Author Comment

by:Brayn66
ID: 12325120
I have tried to apply and now have a compile error related to futureValue:

 illegal start of type
   return futureValue;
   ^
 <identifier> expected
   return futureValue;
                     ^
2 errors

thanks, lauren

Source code;

import javax.swing.*;
import BreezySwing.*;

import BreezyGUI.*;

  public class Mutualfund11 extends GBFrame {

              // variables for window objects

      private JLabel        initialPrincipleLabel;
      private DoubleField   initialPrincipleField;
      private JLabel              interestRateLabel;
      private DoubleField   interestRateField;
      private JLabel        yearsFundLabel;
      private IntegerField  yearsFundField;
      private JButton              calculateButton;
      private JLabel        yearEndPrincipleLabel;
    private JTextArea     output;


               // instance variables

      private int yearsFund;
      private double initialPrinciple;
      private double interestRate;
      private double earnedInterest;            // initial principle * interest rate
      private double yearEndPrinciple;    // initial principle + earned interests



                   // constructor

     private double intialPrinciple = 0;
     private double interestRate = 0;
     private double yearsFund = 0;


  public Mutualfund11 (){

                 // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                           Format.justify('r', "Principle", 18) +
                           Format.justify('r', "Interest earned", 20) +
                           Format.justify('r', "Year end Principle", 21) +  "\n";


                 // instantiate window objects

             initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
             initialPrincipleField = addDoubleField   (0,1,2,1,1);
             interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
             interestRateField     = addDoubleField (0,2,2,1,1);
             yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
             yearsFundField        = addIntegerField  (0,3,2,1,1);
             calculateButton       = addButton        ("       Calculate",4,1,1,1);
             output                = addTextArea      (header,5,1,4,4 );

      output.setEnabled(false);
      initialPrincipleField.requestFocus();
      interestRateField.requestFocus();
      yearsFundField.requestFocus();

}
  public void buttonClicked (JButton buttonObj){
            if (buttonObj == calculateButton){
                        processInputs();
            } else {
                  calculateButton.setEnabled(false);

                  displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
            }
}

   private void processInputs(){

      //declare local variables

            double initialPrinciple;
            double interestRate;
            int yearsFund;
            double earnedInterest;
            double yearEndPrinciple;
            double compoundInterest;
            double futureValue;

            initialPrinciple = initialPrincipleField.getNumber();
            interestRate = interestRateField.getNumber();
        yearsFund = yearsFundField.getNumber();


        //calculations

            interestRate = (interestRate / 100);
        earnedInterest = (initialPrinciple * interestRate);
          yearEndPrinciple = (earnedInterest + initialPrinciple);


}                  // Compound

   double initialPrinciple = initialprincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double yearEndPrinciple = compoundInterest();
   double interestEarned = yearEndPrinciple - initialPrinciple;
   double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
   return futureValue;

                  //display

   private void displayNumbers(int num1, double num2, double num3, double num4, double num5){
                        String numberLine = Format.justify('l', num1, 15) +
                                                       Format.justify('r', num2, 15, 2) +
                                                       Format.justify('r', num3, 15, 2) +
                                                       Format.justify('r', num4, 18, 2) +
                                                       Format.justify('r', num5, 15, 2);
                                    output.append (numberLine + "\n");
}
                  //GUI

   public static void main(String[] args){

                  Mutualfund11 theGUI = new Mutualfund11 ();
                  theGUI.setLookAndFeel("Motif");
                  theGUI.setSize (550,500);            //width = 550, height = 500 in pixels
                  theGUI.setVisible (true);            //window is visible
                        }
}
0
 

Author Comment

by:Brayn66
ID: 12325245
PS - I am not clear on how this:

yearEndPrinciple = compoundInterest();
 earnedInterest = yearEndPrinciple - initialPrinciple;


interacts with:

  double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
   return futureValue;


In the big picture, I am not sure if I am implementing these correctly or in the right area. thanks again!
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12325331
>  double initialPrinciple = initialprincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double yearEndPrinciple = compoundInterest();
   double interestEarned = yearEndPrinciple - initialPrinciple;
   double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
   return futureValue;

this chunk here doesn't belongs to any method, therefore this statement:

return futureValue;

is illegal.

correct this by put it in a method.

////////////////Code///////////////////////
double compoundInterest(){
   double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
   return futureValue;
}
////////////////////////////////////////////

then call compoundInterest() in your processInputs() like this:


////////////////////////////Code/////////////////////////
 private void processInputs(){
          initialPrinciple = initialPrincipleField.getNumber();
          interestRate = interestRateField.getNumber();
          yearsFund = yearsFundField.getNumber();

           //calculations
           yearEndPrinciple = compoundInterest();
           earnedInterest = yearEndPrinciple - initialPrinciple;

           double futureValue = yearEndPrinciple; // unnecessary, I thought futureValue is yearEndPriciple

           // display calculation
          displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);

}
////////////////////////////////////////////////      

add these two code sections to your class, and delete old code.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12325380
> yearEndPrinciple = compoundInterest();
if I understood correctly, yearEndPrinciple represents the total amount of money: i.e yearEndPrinciple= initialPrinciple+earnedInterest

> earnedInterest = yearEndPrinciple - initialPrinciple;

hence this statement.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12325445
>interacts with:
>double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
>return futureValue;

I adviced on putting this in a seperate method called compoundInterest() is because:

1) Although you can just have the above statement within your processInputs() method, so the line

   yearsEndPrinciple = compoundInterest();

can be:

    yearsEndPrinciple = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);

this is the matter of choice.

2) By using compoundInterest() seperately has its advantage such that you can reuse it again in the case where you might need to produce a table to show the gradual interest-growth over the specified number of years.

Lets get this your code working first then we can discuss further. A short answer is your implementation is it's correct.
0
 

Author Comment

by:Brayn66
ID: 12325616
Does it look like I have implmented this right?
My compile error says:

package BreezyGUI does not exist
import BreezyGUI.*;
^
cannot resolve symbol
symbol  : variable futureValue
location: class Mutualfund11
displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
                                                                                                       ^
 cannot resolve symbol
symbol  : variable futureValue
location: class Mutualfund11
          displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
                                                                                         ^
3 errors

My current source:

thanks funnyveryfunny! lauren

import javax.swing.*;
import BreezySwing.*;
import BreezyGUI.*;

  public class Mutualfund11 extends GBFrame {

              // variables for window objects

      private JLabel        initialPrincipleLabel;
      private DoubleField   initialPrincipleField;
      private JLabel              interestRateLabel;
      private DoubleField   interestRateField;
      private JLabel        yearsFundLabel;
      private IntegerField  yearsFundField;
      private JButton              calculateButton;
      private JLabel        yearEndPrincipleLabel;
    private JTextArea     output;


               // instance variables

      private int yearsFund;
      private double initialPrinciple;
      private double interestRate;
      private double earnedInterest;            // initial principle * interest rate
      private double yearEndPrinciple;    // initial principle + earned interests



                   // constructor

     //private double intialPrinciple = 0;
     //private double interestRate = 0;
     //private double yearsFund = 0;


  public Mutualfund11 (){

                 // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                           Format.justify('r', "Principle", 18) +
                           Format.justify('r', "Interest earned", 20) +
                           Format.justify('r', "Year end Principle", 21) +  "\n";


                 // instantiate window objects

             initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
             initialPrincipleField = addDoubleField   (0,1,2,1,1);
             interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
             interestRateField     = addDoubleField (0,2,2,1,1);
             yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
             yearsFundField        = addIntegerField  (0,3,2,1,1);
             calculateButton       = addButton        ("       Calculate",4,1,1,1);
             output                = addTextArea      (header,5,1,4,4 );

      output.setEnabled(false);
      initialPrincipleField.requestFocus();
      interestRateField.requestFocus();
      yearsFundField.requestFocus();

}
  public void buttonClicked (JButton buttonObj){
            if (buttonObj == calculateButton){
                        processInputs();
            } else {
                  calculateButton.setEnabled(false);

                  displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
            }


      //private void processInputs(){

            //declare local variables
            double initialPrinciple;
            double interestRate;
            int yearsFund;
            double earnedInterest;
            double yearEndPrinciple;
            double compoundInterest;
            double futureValue;
      }

      // Compound
   double compoundInterest(){
            double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
                 return futureValue;
   double initialPrinciple = initialPrincipleField.getNumber();
   double interestRate = interestRateField.getNumber();
   double yearsFund = yearsFundField.getNumber();
   double yearEndPrinciple = compoundInterest();
   double interestEarned = yearEndPrinciple - initialPrinciple;
  // double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
  // return futureValue;
      }
 private void processInputs(){
          initialPrinciple = initialPrincipleField.getNumber();
          interestRate = interestRateField.getNumber();
          yearsFund = yearsFundField.getNumber();

           //calculations
           yearEndPrinciple = compoundInterest();
           earnedInterest = yearEndPrinciple - initialPrinciple;

          // display calculation
          displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
      }



                  //display
   private void displayNumbers(int num1, double num2, double num3, double num4, double num5){
                        String numberLine = Format.justify('l', num1, 15) +
                                                       Format.justify('r', num2, 15, 2) +
                                                       Format.justify('r', num3, 15, 2) +
                                                       Format.justify('r', num4, 18, 2) +
                                                       Format.justify('r', num5, 15, 2);
                                    output.append (numberLine + "\n");
}
                  //GUI

   public static void main(String[] args){

                  Mutualfund11 theGUI = new Mutualfund11 ();
                  theGUI.setLookAndFeel("Motif");
                  theGUI.setSize (550,500);            //width = 550, height = 500 in pixels
                  theGUI.setVisible (true);            //window is visible
                        }
                  }
0
 

Author Comment

by:Brayn66
ID: 12325699
I have corrected the futureValue problem and now have the following errors:

';' expected
   double compoundInterest(){
                          ^
 '}' expected
                  }
                         ^
2 errors

I feel I'm close, but am struggling with final errors. thanks, lauren

source code:

import javax.swing.*;
import BreezySwing.*;

import BreezyGUI.*;

  public class Mutualfund11 extends GBFrame {

              // variables for window objects

      private JLabel        initialPrincipleLabel;
      private DoubleField   initialPrincipleField;
      private JLabel              interestRateLabel;
      private DoubleField   interestRateField;
      private JLabel        yearsFundLabel;
      private IntegerField  yearsFundField;
      private JButton              calculateButton;
      private JLabel        yearEndPrincipleLabel;
    private JTextArea     output;


               // instance variables

      private int yearsFund;
      private double initialPrinciple;
      private double interestRate;
      private double earnedInterest;            // initial principle * interest rate
      private double yearEndPrinciple;    // initial principle + earned interests
      private double compoundInterest;



                   // constructor

     //private double intialPrinciple = 0;
     //private double interestRate = 0;
     //private double yearsFund = 0;


  public Mutualfund11 (){

                 // table header's line

  String  header = Format.justify('l', "Year#", 10) +
                           Format.justify('r', "Principle", 18) +
                           Format.justify('r', "Interest earned", 20) +
                           Format.justify('r', "Year end Principle", 21) +  "\n";


                 // instantiate window objects

             initialPrincipleLabel = addLabel         ("       Enter principle amount in whole number format",1,1,1,1);
             initialPrincipleField = addDoubleField   (0,1,2,1,1);
             interestRateLabel     = addLabel         ("       Interest Rate",2,1,1,1);
             interestRateField     = addDoubleField (0,2,2,1,1);
             yearsFundLabel        = addLabel         ("       Years Invested",3,1,1,1);
             yearsFundField        = addIntegerField  (0,3,2,1,1);
             calculateButton       = addButton        ("       Calculate",4,1,1,1);
             output                = addTextArea      (header,5,1,4,4 );

      output.setEnabled(false);
      initialPrincipleField.requestFocus();
      interestRateField.requestFocus();
      yearsFundField.requestFocus();

}
  public void buttonClicked (JButton buttonObj){
            if (buttonObj == calculateButton){
                        processInputs();
            } else {
                  calculateButton.setEnabled(false);

                  displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
            }


// Compound
   double compoundInterest(){
            double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
                 return futureValue;
        }


  private void processInputs(){
          initialPrinciple = initialPrincipleField.getNumber();
          interestRate = interestRateField.getNumber();
          yearsFund = yearsFundField.getNumber();

           //calculations
           yearEndPrinciple = compoundInterest();
           earnedInterest = yearEndPrinciple - initialPrinciple;

          // display calculation
          displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple, futureValue);
      }
                  //display
   private void displayNumbers(int num1, double num2, double num3, double num4, double num5){
                        String numberLine = Format.justify('l', num1, 15) +
                                                       Format.justify('r', num2, 15, 2) +
                                                       Format.justify('r', num3, 15, 2) +
                                                       Format.justify('r', num4, 18, 2) +
                                                       Format.justify('r', num5, 15, 2);
                                    output.append (numberLine + "\n");
      }
                  //GUI
   public static void main(String[] args){

                  Mutualfund11 theGUI = new Mutualfund11 ();
                  theGUI.setLookAndFeel("Motif");
                  theGUI.setSize (550,500);            //width = 550, height = 500 in pixels
                  theGUI.setVisible (true);            //window is visible
                        }
                  }
0
 
LVL 4

Accepted Solution

by:
funnyveryfunny earned 300 total points
ID: 12328931
try this now:

/////////////////source code////////////////////////////////////////////
import javax.swing.*;
import BreezySwing.*;

import BreezyGUI.*;

public class Mutualfund11 extends GBFrame {

     // variables for window objects
     private JLabel        initialPrincipleLabel;
     private DoubleField   initialPrincipleField;
     private JLabel            interestRateLabel;
     private DoubleField   interestRateField;
     private JLabel        yearsFundLabel;
     private IntegerField  yearsFundField;
     private JButton            calculateButton;
     private JLabel        yearEndPrincipleLabel;
     private JTextArea     output;


     // instance variables
     private int yearsFund;
     private double initialPrinciple;
     private double interestRate;
     private double earnedInterest;          // initial principle * interest rate
     private double yearEndPrinciple;    // initial principle + earned interests



     // constructor
     //private double intialPrinciple = 0;
     //private double interestRate = 0;
     //private double yearsFund = 0;

     public Mutualfund11 (){

        // table header's line
        String  header = Format.justify('l', "Year#", 10) +
                        Format.justify('r', "Principle", 18) +
                       Format.justify('r', "Interest earned", 20) +
                       Format.justify('r', "Year end Principle", 21) +  "\n";


         // instantiate window objects
        initialPrincipleLabel = addLabel("       Enter principle amount in whole number format",1,1,1,1);
        initialPrincipleField = addDoubleField(0,1,2,1,1);
        interestRateLabel     = addLabel("       Interest Rate",2,1,1,1);
        interestRateField     = addDoubleField(0,2,2,1,1);
        yearsFundLabel        = addLabel("       Years Invested",3,1,1,1);
        yearsFundField        = addIntegerField(0,3,2,1,1);
        calculateButton       = addButton("       Calculate",4,1,1,1);
        output                = addTextArea(header,5,1,4,4 );

        output.setEnabled(false);
        initialPrincipleField.requestFocus();
        interestRateField.requestFocus();
        yearsFundField.requestFocus();
    }
      
    public void buttonClicked (JButton buttonObj){
       if (buttonObj == calculateButton){
           processInputs();
       }else{
           calculateButton.setEnabled(false);
           displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);
       }
    }

    // Compound
    double compoundInterest(){
       double futureValue = initialPrinciple * Math.pow(1+ interestRate/100.0, yearsFund);
       return futureValue;
    }


    private void processInputs(){
       initialPrinciple = initialPrincipleField.getNumber();
       interestRate = interestRateField.getNumber();
       yearsFund = yearsFundField.getNumber();
        
         //calculations
       yearEndPrinciple = compoundInterest();
       earnedInterest = yearEndPrinciple - initialPrinciple;
        
       // display calculation
       displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);
    }
     
      //display
    private void displayNumbers(int num1, double num2, double num3, double num4){
          String numberLine = Format.justify('l', num1, 15) +
                                  Format.justify('r', num2, 15, 2) +
                                  Format.justify('r', num3, 15, 2) +
                                  Format.justify('r', num4, 18, 2);
          output.append (numberLine + "\n");
     }
      
      
    //GUI
    public static void main(String[] args){
        Mutualfund11 theGUI = new Mutualfund11 ();
        theGUI.setLookAndFeel("Motif");
        theGUI.setSize (550,500);          //width = 550, height = 500 in pixels
        theGUI.setVisible (true);          //window is visible
    }
}
///////////////////////////end of source code/////////////////////////////////////////


I suggest that you get a free Java editor like Borland's JBuilder Personal, it would save you a lot of hassles.

I also deleted your futureValue as I see it as a duplicated value of yearsEndPrinciple.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12334014
Thanks Lauren :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now