• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

Java GUI calculator help! :-(


I have worked with a couple of others and wanted to see if there was anyone else that would review my code.
I have made improvements along the way and believe it is very close to completion.
I have two compile errors(which are listed after the source code).

the specs on this program are an investment calculator in GUI format:

      * Inputs:
            1. interests rates
            2. initial principle
            3. number of years to stay in fund

      * Outputs (in table format):
            1. table whose columns contain year number
            2. principle at the beginning of the year
            3. interests earned
             4. principle at the end of the year


       * it should compound compound annually for the number of years in the fund.
      

source code:

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

  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 TextArea      output;


            // instance variables
     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", 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     = addInterestField (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);
           yearEndPrincipleLabel = addLabel         ("Year end principle",5,1,1,1);
           outputField           = addTextArea      (header,4,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 (the calculate button is pressed)  {
                    processInputs()

          } else {
               displayDashes()
               displayNumbers (initialPrinciple, interestRate, yearsFund)
          }

     // read the inputs, compute the informatiom, display & format: 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

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

          displayNumbers (initialPrinciple, interestRate, yearsFund);

 }
   private void displayNumbers (double num1, double num2, int num3){
                    String dashLine = Format.justify('l', num1, 12) +
                                              Format.justify('r', num2, 15, 2) +
                                              Format.justify('r', num3, 18, 2) + "\n";
                              output.append (numberLine + "\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

     }
}






My compile errors are:    ..............................

C:\ex\Mutualfund6.java:64: ')' expected
          if (the calculate button is pressed)  {
                        ^
C:\ex\Mutualfund6.java:121: illegal start of expression                        // (this is a bracket issue)
}



thanks for any and all help! lauren
0
Brayn66
Asked:
Brayn66
  • 3
  • 2
  • 2
  • +1
1 Solution
 
armoghanCommented:
instead of this english labguage line
if (the calculate button is pressed)

use some java syntactic line

0
 
RuadRauFlessaCommented:
something like

  public void buttonClicked (JButton buttonObj){

          if (buttonObj.isPressed )  {
                    processInputs()

          } else {
               displayDashes()
               displayNumbers (initialPrinciple, interestRate, yearsFund)
          }

//you also forgot this bracket
  }
0
 
RuadRauFlessaCommented:
Sorry that should be isSelected.

However there is a better way of doing this and that would be to use action listeners which I can see none of here. It might be that you didn't post the complete code. Otherwise a better picture would be available.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
expertmbCommented:
this code compiles, you need to have BreezyGUI.jar

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;

//     calculateButton.addActionListener(new GBFrameButtonListener(this));

}



  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);



          displayNumbers (initialPrinciple, interestRate, yearsFund);


 }
   private void displayNumbers (double num1, double num2, double num3){
                    String dashLine = Format.justify('l', num1, 12, 2) +
                                              Format.justify('r', num2, 15, 2) +
                                             Format.justify('r', num3, 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

     }
}


0
 
Brayn66Author Commented:
Per the requirements , everything seems to work (compile and execute) although the investment calculator will not compound annually based on the number of years to invest (yearsFund). It only return a result for 1 year regardless of the number of years entered.  I am not sure if I need a loop or possible a method.

I also tried to add a loop, but it didn't seem to have any funtion in the program.  
I have tried to add an exponential method to account for the compound annually function, but I get the error listed below the corresponding source code.  A crude example of what I am trying to accomplish (an inverstment formula to calculate a compounded annually investment)  would be:

                  S=P(1+I)^n
 S = future value (yearEndPrinciple)                                    
 P = principle amount (initialPrinciple)
 I = interest rate (interestRate)
 n = number of years to compound (yearsFund)

This code is complete minus this issue. Thank you for any help! lauren



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;

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

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

    //  static double pow(double yearEndPrinciple, int yearsFund){
        //}          --- I was hoping this would help the formula compound, but it gives an illegal start  //of expression error when I compile

    displayNumbers (yearsFund, initialPrinciple, earnedInterest, yearEndPrinciple);

          yearEndPrinciple = initialPrinciple;


  /* for(yearsFund = 1; yearsFund<=10; yearsFund = yearsFund+1)
               
   {
      yearEndPrinciple += yearEndPrinciple * interestRate;
                               }
             ---this for loop didn't seem to function?
*/        

 }
   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
 
expertmbCommented:
/*
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
 
Brayn66Author Commented:
I understand and this looks like what I have been struggling with.

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(){


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
 
expertmbCommented:
thanks :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now