Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calculation problem with applet

 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic