(Creating an investment value calculator) Write a program that calculates the fu
ID: 3635436 • Letter: #
Question
(Creating an investment value calculator) Write a program that calculates the fu- ture value of an investment at a given interest rate for a specified number of years. The formula for the calculation is as follows:futureValue = investmentAmount × (1 + monthlyInterestRate)years×12
Use text fields for interest rate, investment amount, and years. Display the future amount in a text field when the user clicks the Calculate button or chooses Calcu- late from the Operation menu. Show a message dialog box when the user clicks the About menu item from the Help menu.
Explanation / Answer
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Q165 extends JFrame { JLabel investment = new JLabel("Investment Amount:"); JLabel years = new JLabel("Years:"); JLabel interest = new JLabel("Annual Interest Rate: "); JLabel fValue = new JLabel("Future Value: "); JTextField investmentInput = new JTextField(); JTextField yearsInput = new JTextField(); JTextField interestInput = new JTextField(); JTextField fValueOutput = new JTextField(); Q165() { setLayout(new BorderLayout()); addTextFields(); addButtonFields(); } protected void addTextFields() { JPanel upper = new JPanel(); upper.setLayout(new GridLayout(5,2)); upper.add(investment); upper.add(investmentInput); upper.add(years); upper.add(yearsInput); upper.add(interest); upper.add(interestInput); upper.add(fValue); upper.add(fValueOutput); add(upper, BorderLayout.NORTH); } protected void addButtonFields() { JPanel lower = new JPanel(); lower.setLayout(new BorderLayout()); JButton calculate = new JButton("calculate"); lower.add(calculate, BorderLayout.EAST); //lower.setSize(width, height) add(lower, BorderLayout.SOUTH); //register the listener calculate.addActionListener(new CalcListener()); } private class CalcListener implements ActionListener { double investmentAmt = 0; double yearsAmt = 0; double interestAmt = 0; public void actionPerformed(ActionEvent e) throws InvalidInputException { //check if the inputs are numeric (should I repeat this for each field? or is there a way to make it so that this applies to all fields?) try { if(isDouble(investmentInput.getText())) { investmentAmt = Double.parseDouble(investmentInput.getText()); } else throw new InvalidInputException("this is invalid, sir!"); } catch(InvalidInputException iIE) { investmentInput.setText("invalid input, try again"); } ////////////////// try { if(isDouble(yearsInput.getText())) { yearsAmt = Double.parseDouble(yearsInput.getText()); } else throw new InvalidInputException("this is invalid, sir!"); } catch(InvalidInputException iIE) { yearsInput.setText("invalid input, try again"); } /////////////////// try { if(isDouble(interestInput.getText())) { interestAmt = Double.parseDouble(interestInput.getText()); } else throw new InvalidInputException("this is invalid, sir!"); } catch(InvalidInputException iIE) { interestInput.setText("invalid input, try again"); } //////////////////// double fValue = getValue(investmentAmt, yearsAmt, interestAmt); fValueOutput.setText(Double.toString(fValue)); } public double getValue(double investmentAmt, double yearsAmt, double interestAmt) { double futureValue = investmentAmt * Math.pow((1 + interestAmt), (yearsAmt * 12)); return futureValue; } } public boolean isDouble(String s) { try { Double.parseDouble(investmentInput.getText()); return true; } catch(Exception e) { return false; } } public static void main(String[] args) { Q165 frame = new Q165(); frame.setSize(400, 200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.