*****I need mutlple try catch blocks. I\'ve already added two but I\'m strugglin
ID: 3580581 • Letter: #
Question
*****I need mutlple try catch blocks. I've already added two but I'm struggling on the others.*****
The Number of Years can be between 1 and 20. Catch any other.
The Annual Interest Rate can be between 1% and 12.5%. Catch any other.
Error messages:
JOptionPane.showMessageDialog(null, " Error: Only 1 to 20 years accepted.");
JOptionPane.showMessageDialog(null, " Error: Annual interest is from 1% to 12.5%");
Heres my code so far.
package snippet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FutureValue extends JFrame {
private JTextField jtfInvestmentAmount;
private JTextField jtfInterestRate;
private JTextField jtfYears;
private JTextField jtfFutureValue;
private JButton jbtCalculate;
public FutureValue() {
setTitle("Future Value Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,3,5,5));
jtfInvestmentAmount = new JTextField();
jtfYears = new JTextField();
jtfInterestRate = new JTextField();
jtfFutureValue = new JTextField();
jtfFutureValue.setEditable(false);
JLabel jlInvestmentAmount = new JLabel(" Investment Amount:");
JLabel jlYears = new JLabel(" Number of Years:");
JLabel jlInterestRate = new JLabel(" Annual Interest Rate:");
JLabel jlFutureValue = new JLabel(" Future Value:");
jbtCalculate = new JButton("Calculate Value");
add (jlInvestmentAmount);
add (jtfInvestmentAmount);
add (jlYears);
add (jtfYears);
add (jlInterestRate);
add (jtfInterestRate);
add (jlFutureValue);
add (jtfFutureValue);
add (jbtCalculate);
ListenerClass listener = new ListenerClass();
jbtCalculate.addActionListener(listener);
setVisible(true);
}
public static void main(String[] args) {
new FutureValue();
}
private void CalculateValue() {
try {
double annualInterestRate = Double.parseDouble(jtfInterestRate.getText());
int Years = Integer.parseInt(jtfYears.getText());
double investmentAmount = Double.parseDouble(jtfInvestmentAmount.getText());
if(investmentAmount < 1 || investmentAmount>100000)
{
throw new Exception();
}
double monthlyInterestRate = annualInterestRate / 1200.0;
double futureValue = investmentAmount * Math.pow(1.0 + monthlyInterestRate,Years * 12);
jtfFutureValue.setText(String.format("%.2f", futureValue));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, " Error: Only integers between 1 and 100,000 accepted.";
}
}
private class ListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtCalculate) {
CalculateValue();
}
}
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FutureValue extends JFrame {
private JTextField jtfInvestmentAmount;
private JTextField jtfInterestRate;
private JTextField jtfYears;
private JTextField jtfFutureValue;
private JButton jbtCalculate;
public FutureValue() {
setTitle("Future Value Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,3,5,5));
jtfInvestmentAmount = new JTextField();
jtfYears = new JTextField();
jtfInterestRate = new JTextField();
jtfFutureValue = new JTextField();
jtfFutureValue.setEditable(false);
JLabel jlInvestmentAmount = new JLabel(" Investment Amount:");
JLabel jlYears = new JLabel(" Number of Years:");
JLabel jlInterestRate = new JLabel(" Annual Interest Rate:");
JLabel jlFutureValue = new JLabel(" Future Value:");
jbtCalculate = new JButton("Calculate Value");
add (jlInvestmentAmount);
add (jtfInvestmentAmount);
add (jlYears);
add (jtfYears);
add (jlInterestRate);
add (jtfInterestRate);
add (jlFutureValue);
add (jtfFutureValue);
add (jbtCalculate);
ListenerClass listener = new ListenerClass();
jbtCalculate.addActionListener(listener);
setVisible(true);
}
public static void main(String[] args) {
new FutureValue();
}
private void CalculateValue() {
try {
double annualInterestRate = Double.parseDouble(jtfInterestRate.getText());
int Years = Integer.parseInt(jtfYears.getText());
double investmentAmount = Double.parseDouble(jtfInvestmentAmount.getText());
if(investmentAmount < 1 || investmentAmount>100000)
{
throw new Exception();
}
double monthlyInterestRate = annualInterestRate / 1200.0;
double futureValue = investmentAmount * Math.pow(1.0 + monthlyInterestRate,Years * 12);
jtfFutureValue.setText(String.format("%.2f", futureValue));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, " Error: Only integers between 1 and 100,000 accepted.";
}
try{
int Years = Integer.parseInt(jtfYears.getText());
if(Years < 1 || Years>20){
throw new Exception();
}
}catch (Exception e){
JOptionPane.showMessageDialog(null, " Error: Only 1 to 20 years accepted.");
}
try{
float rate = Integer.parseInt(jtfInterestRate.getText());
if(rate < 1 || rate>12.5){
throw new Exception();
}
}catch (Exception e){
JOptionPane.showMessageDialog(null, " Error: Annual interest is from 1% to 12.5%");
}
}
private class ListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtCalculate) {
CalculateValue();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.