Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a written Java program that compiles, runs and shows the GUI perfectly, b

ID: 3656803 • Letter: I

Question

I have a written Java program that compiles, runs and shows the GUI perfectly, but the out put is incorrect. The program is a financial calculators that lets the user enter in: Investment Amount, Years, Annual Interest Rate. When the "Calculate" button is press, the Future Value of the investment is shown. The following formula is to be used: futureValue = investmentAmount * ( 1 + montlyInterestRate) ^ (years * 12) If Investment amount = 1000, Years = 3 and Annual Interest Rate = 3.25 I should get 11022.66 Instead I get 4.18....E25 Can anyone help? The program code is listed below. Thanks! import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Exercise16_5 extends JFrame { private JTextField jtfInvestmentAmount, jtfYears, jtfAnnualInterestRate, jtfFutureValue; private JButton jbtCalculate = new JButton("Calculate"); public Exercise16_5() { JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(4, 2)); p1.add(new JLabel("Investment Amount")); p1.add(jtfInvestmentAmount = new JTextField(16)); p1.add(new JLabel("Years")); p1.add(jtfYears = new JTextField(16)); p1.add(new JLabel("Annual Interest Rate")); p1.add(jtfAnnualInterestRate = new JTextField(16)); p1.add(new JLabel("Future value")); p1.add(jtfFutureValue = new JTextField(16)); JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); p2.add(jbtCalculate); jtfInvestmentAmount.setHorizontalAlignment(JTextField.RIGHT); jtfYears.setHorizontalAlignment(JTextField.RIGHT); jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT); jtfFutureValue.setHorizontalAlignment(JTextField.RIGHT); jtfFutureValue.setEditable(false); jbtCalculate.setMnemonic('C'); setLayout(new BorderLayout()); add(p1, BorderLayout.CENTER); add(p2, BorderLayout.SOUTH); jbtCalculate.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int investmentAmount = Integer.parseInt(jtfInvestmentAmount.getText()); int years = Integer.parseInt(jtfYears.getText()); double annualInterestRate = Double.parseDouble(jtfAnnualInterestRate.getText()); double futureValue = investmentAmount * (power(annualInterestRate + 1, years * 12)); jtfFutureValue.setText("" + futureValue); } }); } public static double power(double rate, int years) { double power = 1; for (int i = 1; i <= years; i++) { power*= rate; } return power; } public static void main(String[] args) { JFrame frame = new Exercise16_5(); frame.setTitle("Exercise16_5"); frame.pack(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Explanation / Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvestmentCalculator { // Main method public static void main(String[] args) { JFrame fr = new InvestmentCalculator(); fr.setTitle("Investment Value Calculator"); fr.setLocationRelativeTo(null); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.pack(); fr.setVisible(true); }//end of main method } class InvestmentCalculator extends JFrame implements ActionListener { JButton btnCal = new JButton("Calculate"); JLabel lblInv = new JLabel("Investment Amount"); JLabel lblYr = new JLabel("Years"); JLabel lblRt = new JLabel("Annual Interest Rate"); JLabel lblRes = new JLabel("Future Value"); JLabel lblBlnk = new JLabel(""); JTextField txtInv = new JTextField(30); JTextField txtYr = new JTextField(30); JTextField txtRt = new JTextField(30); JTextField txtRes = new JTextField(30); JPanel jp; public InvestmentCalculator() { jp = new JPanel(); // assigning grid layout to the panel jp.setLayout(new GridLayout(5, 2)); // setting the preferred size to panel jp.setPreferredSize(new Dimension(400, 250)); // compnents of the panel jp.add(lblInv); jp.add(txtInv); jp.add(lblYr); jp.add(txtYr); jp.add(lblRt); jp.add(txtRt); jp.add(lblRes); jp.add(txtRes); jp.add(lblBlnk); jp.add(btnCal); add(jp); // adding button listener btnCal.addActionListener(this); } // Action handler @Override public void actionPerformed(ActionEvent e) { double invest = Double.parseDouble(txtInv.getText()); int year = Integer.parseInt(txtYr.getText()); double interestrt = Double.parseDouble(txtRt.getText()); if(e.getSource() == btnCal) { double futureValue = invest * Math.pow(1 + (interestrt/100), year); txtRes.setText(String.format("%,.2f", futureValue)); System.out.println((10000 * Math.pow((3.25 + 1),36))); txtRes.setText(String.valueOf(res)); } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote