Question

***Please use java code for the question below*** Write a program that calculates the future value...

***Please use java code for the question below***

  1. Write a program that calculates the future value of a given investment at a given interest rate for a specified number of years. The formula for this calculation is:

value = investmentAmount * (1 + monthly interest rate)years*12

Use text fields for the user to enter the numbers (Investment Amount, Number of Years, and Annual Interest). To get/load data from the textbox, (in this case doubles) use the following structure: (bold are identifiers you chose)

double incomingdata = Double.parseDouble(textboxName.getText());

Add a button called Calculate that will display the value (future amount) in a text field when clicked. You can use any pane you desire. The logic portion is easy (formula is given) – start with the layout and figuring out what is required (some labels, some text boxes, button, etc.). Draw it out on paper, figure out where everything goes – program (just the nodes) and adjust to get the layout you are happy with. Then, you can add the listener/handler – using implements and override. Also, generally, it is cleaner to import only what you need, vice doing a “*” (wildcard) –but, ensure you have imported the right stuff.  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File : Calculator.java

Note : Formula is modified because the provided one was not working.

import java.awt.event.*;
import javax.swing.*;
class Calculator extends JFrame implements ActionListener
{
// JTextField
static JTextField txtAmount, txtYears, txtRate, txtFamount ;
// JFrame
static JFrame f;   
// JButton
static JButton b;   
// label to display text
static JLabel l1,l2,l3,l4;
// default constructor
Calculator()
{
}
  
// main class
public static void main(String[] args)
{
// create a new frame to store text field and button
f = new JFrame("Calculator");   
// create a label to display text
l1 = new JLabel("Amount:");
       l2 = new JLabel("Number of Years:");
       l3 = new JLabel("Interest Rate:");  
       l4 = new JLabel("Future Amount:");
       l1.setBounds(100, 80, 100, 30);
       l2.setBounds(100, 120, 100, 30);
       l3.setBounds(100, 160, 100, 30);  
       l4.setBounds(100, 240, 100, 30);
// create a new button
b = new JButton("Calculate");
b.setBounds(200, 200, 100, 30);
// create a object of the text class
Calculator c = new Calculator();   
// addActionListener to button
b.addActionListener(c);   
// create a object of JTextField with 20 columns
txtAmount = new JTextField(20);
       txtYears = new JTextField(20);  
       txtRate = new JTextField(20);
       txtFamount = new JTextField(20);
       txtAmount.setBounds(200, 80, 100, 30);
       txtYears.setBounds(200, 120, 100, 30);
       txtRate.setBounds(200, 160, 100, 30);
       txtFamount.setBounds(200, 240, 100, 30);   
       f.setLayout(null);
f.add(txtAmount);
       f.add(txtYears);
       f.add(txtRate);
       f.add(txtFamount);   
       f.add(l1);
       f.add(l2);
       f.add(l3);
       f.add(l4);
f.add(b);
  
// set the size of frame
f.setSize(400, 400);   
f.show();
}
  
// if the button is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
Double amount=0.0, years=0.0, rate=0.0, famount=0.0;
if (s.equals("Calculate"))
{
// set the text of the label to the text of the field
   amount=Double.parseDouble(txtAmount.getText());
   years=Double.parseDouble(txtYears.getText());
   rate=Double.parseDouble(txtRate.getText());
   famount=(amount*(1+((rate/100)*years)));
   famount= (double)(Math.round(famount*100.0)/100.0);// Round it to two decimal
   txtFamount.setText(""+famount);   
  
// set the text of field to blank
  
}
}
}

Screens:

Add a comment
Know the answer?
Add Answer to:
***Please use java code for the question below*** Write a program that calculates the future value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this code: Write a program that calculates the future value of an...

    I need help with this code: 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 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, as shown in the following figure. It needs to be done in...

  • Please help me do this program. Add some comments on it. *15.5 (Create an investment-value calculator)...

    Please help me do this program. Add some comments on it. *15.5 (Create an investment-value calculator) Write a program that calculates the future value of an investment at a for the calculation is given interest rate for a specified number of years. The formula investmentAmount * (1monthlyInterestRate ) years *12 futureValue 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,...

  • Write a program that calculates the amount of money that you must invest In a savings...

    Write a program that calculates the amount of money that you must invest In a savings account at a given interest rate for a given number of years to have a certain dollar amount at me end of the period Y our program will ask the user for the amount the user wants to have at the end of the term (future value). the number of years the user plans to let the money stay in the account, and the...

  • *Use Java to create this program* For this assignment, you will be building a Favorite Songs...

    *Use Java to create this program* For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input...

  • please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart...

    please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart with the JavaFX version of the Future Value application presented in chapter 17. Create error message labels for each text field that accepts user input. Use the Validation class from chapter 17 to validate user input.Format the application so that the controls don’t change position when error messages are displayed. package murach.business; public class Calculation {        public static final int MONTHS_IN_YEAR =...

  • Please Help, JavaFX assignment. This assignment will focus on the use anonymous inner class handlers to...

    Please Help, JavaFX assignment. This assignment will focus on the use anonymous inner class handlers to implement event handling. Assignment 12 Assignment 12 Submission Follow the directions below to submit Assignment 12: This assignment will be a modification of the Assignment 11 program. This program should use the controls and layouts from the previous assignment. No controls or layouts should be added or removed for this assignment. Add event handlers for the three buttons. The event handlers should be implemented...

  • Computing Future Investment Value Problem Description: Write a method that computes future investment value at a...

    Computing Future Investment Value Problem Description: Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the following formula: 12 futurelnvestmentValue investmentAmountx(1+ monthlylnterestRate) Use the following method header: publie static double futureInvestnentValue double investnentAmount, double monthlyInterestRate, int years) For example, futur Investmentvalue( 1eeee, อ.es/12, 5) returns 12833.59 Write a test program that prompts the user to enter the investment amount (e.g. 1000) and the interest...

  • I tried to add exception handling to my program so that when the user puts in...

    I tried to add exception handling to my program so that when the user puts in an amount of change that is not the integer data type, the code will catch it and tell them to try again, but my try/catch isnt working. I'm not sure how to fix this problem. JAVA code pls package Chapter9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIlab extends JFrame implements ActionListener {    private static final int WIDTH = 300;    private...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Chapter 2 slides 40 to 47 discuss an example program, which computes the future value of...

    Chapter 2 slides 40 to 47 discuss an example program, which computes the future value of an investment given an initial principal and an interest rate. Your task is to modify this program to calculate and print the future values of an investment after 10 years, using several different interest rates.: from 1% to 20%, in increments of 1%. That is, given an initial principal, your program should compute the future value after 10 years given an interest rate of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT