Solved

Java GUI calculator help! :-(

Posted on 2004-10-13
8
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Expert Comment

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

use some java syntactic line

0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12305766
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
ID: 12305782
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:expertmb
ID: 12307468
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
 

Author Comment

by:Brayn66
ID: 12314689
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
ID: 12315690
/*
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: 12315819
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
ID: 12315993
thanks :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 623
login form jsp example 2 73
maximize the sum of fractions 33 76
netbeans can not run self signed app 8 38
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.
Suggested Courses

752 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