Write a JavaFX program name InvestmentCalc that calculates the future value of a
ID: 3836975 • Letter: W
Question
Write a JavaFX program name InvestmentCalc 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 = investmentAmount * (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. Your display should look similar to the one below:
Investment calculator Investment Amount Number of Years Annual Interest Rate: Future value: 10000 3.25 $11386.28 CalculateExplanation / Answer
Hi Please find the complete program below. In case of any issue, please let us know. Thanks
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class InvestmentCalc extends Application { //Extending Application class
@Override
public void start(Stage calculatorPrimaryStage) throws Exception {
InvestmentCalculatorPane calculatorPane = new InvestmentCalculatorPane();
/*
* Creating screen for the InvestmentCalc applcication
*/
calculatorPrimaryStage.setScene(new Scene(calculatorPane, calculatorPane.getPrefWidth(), calculatorPane.getPrefHeight()));
calculatorPrimaryStage.setTitle("Investment calculator"); //Setting Application name that is displayed in Title bar
calculatorPrimaryStage.show();
}
public static void main(String[] args) {
Application.launch(args); //Launches the FX applicaiton
}
/*
* Creating a class InvestmentCalculatorPane which extends GridPane for creating labels and textfields
*/
private class InvestmentCalculatorPane extends GridPane {
/*
* Creating test fields and labels for InvestmentCalc :: Start
*/
Label InvestmentAmountLabel = new Label("Investment Amount:");
TextField InvestmentAmountTF = new TextField();
Label NumberOfYearsLabel = new Label("Number Of Years:");
TextField NumberOfYearsTF = new TextField();
Label AnnualInterestRateLabel = new Label("Annual Interest Rate:");
TextField AnnualInterestRateTF = new TextField();
Label FutureValueLabel = new Label("Future Value:");
TextField FutureValueTF = new TextField();
Button CalculateButton = new Button("Calculate");
/*
* Creating test fields and labels for InvestmentCalc :: End
*/
private InvestmentCalculatorPane() {
// Create layout and Positioning test fields and labels in the screen
setPadding(new Insets(15, 15, 15, 15));
setAlignment(Pos.CENTER);
setHgap(12);
setVgap(12);
add(InvestmentAmountLabel, 0, 0);
add(InvestmentAmountTF, 1, 0);
add(NumberOfYearsLabel, 0, 1);
add(NumberOfYearsTF, 1, 1);
add(AnnualInterestRateLabel, 0, 2);
add(AnnualInterestRateTF, 1, 2);
add(FutureValueLabel, 0, 3);
add(FutureValueTF, 1, 3);
HBox buttons = new HBox();
buttons.getChildren().add(CalculateButton);
buttons.setAlignment(Pos.BOTTOM_RIGHT);
add(buttons, 1, 4);
CalculateButton.setOnAction(e-> calcFutureValue());
// Editing TextField settings
TextField[] textFields = (TextField[])getArray(
InvestmentAmountTF, NumberOfYearsTF, AnnualInterestRateTF, FutureValueTF);
//Setting alignment for test fields
for (TextField tf : textFields) {
tf.setAlignment(Pos.BASELINE_RIGHT);
}
// FutureValue test field is enabled. To disable make it ture
FutureValueTF.setDisable(false);
}
private Object[] getArray(Object... objects) {
Object[] temp = new TextField[objects.length];
for (int i = 0; i < objects.length; i++) {
temp[i] = objects[i];
}
return temp;
}
public void calcFutureValue() {
//Reading values from text fields
double investmentAmount = Double.parseDouble(InvestmentAmountTF.getText());
double years = Double.parseDouble(NumberOfYearsTF.getText());
double monthlyInterestRate = Double.parseDouble(AnnualInterestRateTF.getText()) / 12 / 100;
//calculating future investment
double futureValue = investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
//displaying future value in FutureValue Text field
FutureValueTF.setText(String.format("$%.2f", futureValue));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.