JAVA黑皮书课后练习题15章15.5

*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 = investmentAmount * (1 + monthlyInterestRate)^(years*12)

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, as shown in Figure 15.25b

 

 

package section_15;

import javafx.application.Application;
import javafx.geometry.HPos;
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.stage.Stage;

public class _05 extends Application {
	public void start(Stage primaryStage) {
		GridPane pane = new GridPane();

		Label labelInvestmentAmount = new Label("Investment Amount:");
		Label labelNumberOfYears = new Label("Number of Years:");
		Label labelAnnualInterestRate = new Label("Annual Interest Rate:");
		Label labelFutureValue = new Label("Future Value:");

		TextField tfInvestmentAmount = new TextField();
		tfInvestmentAmount.setAlignment(Pos.BASELINE_RIGHT);
		TextField tfNumberOfYears = new TextField();
		tfNumberOfYears.setAlignment(Pos.BASELINE_RIGHT);
		TextField tfAnnualInterestRate = new TextField();
		tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT);
		TextField tfFutureValue = new TextField();
		tfFutureValue.setAlignment(Pos.BASELINE_RIGHT);

		Button btCalculate = new Button("Calculate");
		GridPane.setHalignment(btCalculate, HPos.RIGHT);
		btCalculate.setOnAction(event -> {
			double investmentAmount = Double.parseDouble(tfInvestmentAmount.getText());
			double numberOfYears = Double.parseDouble(tfNumberOfYears.getText());
			double annualInterestRate = Double.parseDouble(tfAnnualInterestRate.getText());
			double futureValue = investmentAmount * Math.pow(1 + (annualInterestRate / 1200), numberOfYears * 12);

			futureValue = Math.round(futureValue * 100) / 100.0;
			tfFutureValue.setText("$" + futureValue);
		});

		pane.add(labelInvestmentAmount, 0, 0);
		pane.add(labelNumberOfYears, 0, 1);
		pane.add(labelAnnualInterestRate, 0, 2);
		pane.add(labelFutureValue, 0, 3);
		pane.add(tfInvestmentAmount, 1, 0);
		pane.add(tfNumberOfYears, 1, 1);
		pane.add(tfAnnualInterestRate, 1, 2);
		pane.add(tfFutureValue, 1, 3);
		pane.add(btCalculate, 1, 4);
		pane.setHgap(10);
		pane.setVgap(5);
		pane.setPadding(new Insets(20));
		Scene scene = new Scene(pane);

		primaryStage.setTitle("_15_05");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值