Solved

Java GUI calculator help! :-(

Posted on 2004-10-13
8
560 Views
Last Modified: 2007-12-19

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
Comment
Question by:Brayn66
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
instead of this english labguage line
if (the calculate button is pressed)

use some java syntactic line

0
 
LVL 3

Expert Comment

by:RuadRauFlessa
Comment Utility
something like

  public void buttonClicked (JButton buttonObj){

          if (buttonObj.isPressed )  {
                    processInputs()

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

//you also forgot this bracket
  }
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
Comment Utility
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
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
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
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

 

Author Comment

by:Brayn66
Comment Utility
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
 
LVL 6

Accepted Solution

by:
expertmb earned 500 total points
Comment Utility
/*
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
Comment Utility
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
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
thanks :-)
0

Featured Post

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

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

772 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

13 Experts available now in Live!

Get 1:1 Help Now