This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calculation problem with applet  RSS feed

MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody try to compile this code and maybe tell me what is wrong? It compiles and runs, but the future calue calculation comes out wrong. Any help is appreciated!!!
Mary Ellen program will calculate the future value of an investment
at a given interest rate for a specified number of years. The program will
enable the user to enter the interest reat, investment amount, and years to
predefined text fields set by the programmer. The future value will be
displayed when the user clicks on the "calculate" button.
package Chapter9;
import Chapter8.MyFrameWithExitHandling;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import java.util.*;
import java.text.*;
public class FutureValue extends MyFrameWithExitHandling
implements ActionListener
// Declare three text fields
private JTextField jtfld1, jtfld2, jtfld3, jtfldResult;
// Declare "Calculate" Button
private JButton jbtCalculate;
// Declare two menu options
private JMenuItem jmiCalculate, jmiAbout;
Locale locale = Locale.ENGLISH;
// Main method
public static void main(String[] args)
FutureValue frame = new FutureValue();
frame.setSize(225, 210);;
// Constructor
public FutureValue()
setTitle("Future Value");
JMenuBar jmb = new JMenuBar();
// set operation menu
JMenu operationMenu = new JMenu("Operation");
// set help menu
JMenu helpMenu = new JMenu("Help");
// add menu items with pneumonics
operationMenu.add(jmiCalculate= new JMenuItem("Calculate", 'C'));
helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
// Use panel p1 to group label fields
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4,1));
p1.add(new Label("Investment"));
p1.add(new Label("Years"));
p1.add(new Label("Interest Rate"));
p1.add(new Label("Future Value"));
// Use Panel p2 to group text fields
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4,1));
p2.add(jtfld1 = new JTextField(10));
p2.add(jtfld2 = new JTextField(10));
p2.add(jtfld3 = new JTextField(10));
p2.add(jtfldResult = new JTextField(10));
jtfldResult.setEditable(false); // Set jtfResult noneditable
// Use panel p2 for the button
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
p3.add(jbtCalculate = new JButton("Calculate"));
// Set FlowLayout for the frame and add panels to the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.WEST);
getContentPane().add(p2, BorderLayout.CENTER);
getContentPane().add(p3, BorderLayout.SOUTH);
// Register listener
// Handle the add operation
public void actionPerformed(ActionEvent e)
String actionCommand = e.getActionCommand();
// Handle button events
if (e.getSource() instanceof JButton)
// Handle Menuitem events
else if (e.getSource() instanceof JMenuItem)
if ("Calculate".equals(actionCommand))
else if ("About".equals(actionCommand))
JOptionPane.showMessageDialog(this, "Adapted with help",
"For Your Information", JOptionPane.INFORMATION_MESSAGE);
private void calculate(char operator)
float investmentAmount = (Float.parseFloat(jtfld1.getText().trim()));
float yrs = (Float.parseFloat(jtfld2.getText().trim()));
float interestRate = (Float.parseFloat(jtfld3.getText().trim()));
float firstPart = investmentAmount * (1 + interestRate);
double futureValue = Math.pow(firstPart,yrs);
// Set result in TextField jtfResult
maateen ashraf
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would u plz be more specific to the problem which u r facing ....
so that i could come up with some solution...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!