Defind Bank Class


Submit solution

Points: 50 (partial)
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem types
Allowed languages
Java 19, Java 8

題目說明

根據主程式需求,定義Bank類別的程式碼

本利和 = 本金 * (1+月利率)^期數

import java.util.Scanner;

// 在此定義Bank類別

public class MyProgram {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double mIR = input.nextDouble();    // 月利率
        double p = input.nextDouble();      // 本金
        int t = input.nextInt();            // 期數

        Bank bank = new Bank(mIR, p, t);    // 宣告物件
        System.out.printf("%.3f", bank.calculate()); // 呼叫物件方法(計算本利和)

    }

}

輸入

月利率 本金 期數

輸出

計算出的本利和(四捨五入到小數點後第三位)

測試資料1 輸入

0.01 1000 5

測試資料1 輸出

1051.010
本利和 = 1000 * (1+0.01)^5 = 1051.010

Comments


  • 1
    scu09156146  commented on April 12, 2024, 5:40 p.m. edited

    題解

    import java.util.Scanner;
    
    class Bank {
        // Field,定義Bank類別的參數
        private double principal; // 存款本金不可讓外部直接存取,設定成private
        public double monthlyIR;
        public int period;
    
        // Constructor,建構Bank物件
        public Bank(double mIR, double p, int t) {
            monthlyIR = mIR;
            principal = p;
            period = t;
        }
    
        // Method,使用Bank參數,執行本利和計算
        public double calculate() {
            return principal * Math.pow(1 + monthlyIR, period);
        }
    }
    
    public class MyProgram {
    
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
            double mIR = input.nextDouble();    // 月利率
            double p = input.nextDouble();      // 本金
            int t = input.nextInt();            // 期數
    
            Bank bank = new Bank(mIR, p, t);    // 宣告物件
            System.out.printf("%.3f", bank.calculate()); // 呼叫物件方法
    
        }
    
    }