It's not a secret anymore!*
The moose likes Applets and the fly likes calculation problem with applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "calculation problem with applet" Watch "calculation problem with applet" New topic
Author

calculation problem with applet

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
HI!
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

FutureValue.java--This 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.pack();
frame.setSize(225, 210);
frame.center();
frame.setVisible(true);
}
// Constructor
public FutureValue()
{
setTitle("Future Value");
setBackground(Color.yellow);
setForeground(Color.black);
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
// set operation menu
JMenu operationMenu = new JMenu("Operation");
operationMenu.setMnemonic('O');
jmb.add(operationMenu);
// set help menu
JMenu helpMenu = new JMenu("Help");
operationMenu.setMnemonic('H');
jmb.add(helpMenu);
// 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
jbtCalculate.addActionListener(this);
jmiCalculate.addActionListener(this);
jmiAbout.addActionListener(this);
}
// Handle the add operation
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
// Handle button events
if (e.getSource() instanceof JButton)
{
calculate('n');
}
// Handle Menuitem events
else if (e.getSource() instanceof JMenuItem)
{
if ("Calculate".equals(actionCommand))
calculate('n');
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
jtfldResult.setText(String.valueOf(futureValue));
}
}
maateen ashraf
Ranch Hand

Joined: Jan 08, 2001
Posts: 122
would u plz be more specific to the problem which u r facing ....
so that i could come up with some solution...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calculation problem with applet
 
Similar Threads
Multiple Windows in Applets
Adding Record Problem in JDBC ?
calculation problem in my applet
JMenuBar in an Applet : Good vs Bad Code Practice?
Java GUI development question.