JAVA UML. NO CODE PICTURES PLS 15.5 (Create an investment-value calculator) Writ
ID: 3737602 • Letter: J
Question
JAVA UML. NO CODE PICTURES PLS
15.5 (Create an investment-value calculator) Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The formula for the calculation is futureValue învestmentAmount * (1 + monthlyInterestRate)years.12 Use text fields for the investment amount, number of years, and annual interest rate. Display the future amount in a text field when the user clicks the Calculate button, as shown in Figure 15.27b. Exercise15 05 Investment Amount Number of Years: Annual Interest Rate: Future value: 10000 3.25 $11386.28 CalculateExplanation / Answer
Program
import javax.swing.*;
import java.awt.event.*;
class Calc implements ActionListener
{
JFrame f;
JTextField t1,t2,t3,t4;
JLabel l1,l2,l3,l4;
JButton b;
static double inv=0,anrate=0,monrate=0,years=0, futval=0;
Calc()
{
f=new JFrame("Exercise15_05");
t1=new JTextField();
t2=new JTextField();
t3=new JTextField();
t4=new JTextField();
l1=new JLabel("Investment Amount:");
l2=new JLabel("Number of years:");
l3=new JLabel("Annual Interest Rate:");
l4=new JLabel("Future Value:");
b=new JButton("CALCULATE");
l1.setBounds(20,40,180,30);
t1.setBounds(210,40,180,30);
l2.setBounds(20,100,180,30);
t2.setBounds(210,100,180,30);
l3.setBounds(20,160,180,30);
t3.setBounds(210,160,180,30);
l4.setBounds(20,220,180,30);
t4.setBounds(210,220,180,30);
b.setBounds(260,280,110,30);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(t4);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(l4);
f.add(b);
f.setLayout(null);
f.setVisible(true);
f.setSize(420,350);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{ inv=Double.parseDouble(t1.getText());
years=Double.parseDouble(t2.getText());
anrate=Double.parseDouble(t3.getText());
monrate=anrate/12; // formula to convert annual rate of interest to monthly
futval=inv* Math.pow((1+monrate),(years*12)); //formula
t4.setText("$"+Double.toString(futval));
}
}
public static void main(String...s)
{
new Calc();
}
}
Note: we are receiving annual interest rate but we need to apply monthly interest rate so you need to divide it by 12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.