Hello I am working on a Java assignment to make an investment calculator. I need
ID: 3763745 • Letter: H
Question
Hello I am working on a Java assignment to make an investment calculator.
I need help making sure this is as close to the assignment as possible and to add JPanel for the user to enter the info into instead of the console! Please Help!
Assignment:
Create a class InvestCalc that computes the future value of an investment at
a given annual rate of interest for a specified number of years. The formula
is: futureValue = investAmount * (1 + monthlyInterestRate)years * 12. Use text
fields for investment amount, annual interest rate, years, and future value
(the latter is uneditable). The Operation menu contains two items: Calculate
and Exit. The Help menu contains one item: Future Value, which when
selected displays in a JOPtionPane how the future value is calculated. The
Future Value should be displayed whenever the user selects Calculate from
the Operation menu OR clicks the Calculate button.
My program so far:
package investcalc;
import java.io.*;
class InvestCalc {
public static void main(String[] args)
throws java.io.IOException {
String s1, s2, s3;
double p, r, f, rate;
int t;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Please input the principal: ");
s1 = br.readLine();
p = Double.parseDouble(s1);
System.out.print("Please input the annual interest rate: ");
s2 = br.readLine();
r = Double.parseDouble(s2);
System.out.print("Please input the number of years invested: ");
s3 = br.readLine();
t = Integer.parseInt(s3);
rate = (1+r)*12;
f = p * Math.pow(rate, t);
System.out.println("The future value of your investment after " + t + " years is: " + f);
}
}
Explanation / Answer
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class calc extends JFrame
{
private JTextField jtfInvestmentAmount, jtfYears,
jtfAnnualInterestRate, jtfFutureValue;
private JButton jbtCalculate = new JButton("Calculate");
public Exercies15_6()
{
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 calc();
frame.setTitle("calc");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.