類和對(duì)象1

創(chuàng)建一個(gè)簡(jiǎn)單版本的銀行 Account 類

創(chuàng)建一個(gè)簡(jiǎn)單版本的 Account 類。將這個(gè)源文件放入 banking 程序包中。在創(chuàng)建單個(gè)帳戶的默認(rèn)程序包中,編寫一個(gè)測(cè)試程序 TestBanking。這個(gè)測(cè)試程序初始化帳戶余額,并可執(zhí)行幾種簡(jiǎn)單的事務(wù)處理。最后,該測(cè)試程 序顯示該帳戶的最終余額

package banking;

// 在 banking 包下創(chuàng)建 Account 類
public class Account {
    private double balance;
    
//帶有一個(gè)參數(shù)(init_balance)的公有構(gòu)造器
public Account(double init_balance){
    System.out.println("Creating an account with a "+init_balance+" balance");
    balance=init_balance;
}

//該方法用于獲取余額
public double geBalance(){
    return balance;
}

//該方法向當(dāng)前余額增加金額
public void deposit(double amt){
    System.out.println("deposit "+amt);
    balance=balance+amt;
}

//從當(dāng)前余額中減去金額
public void withdraw(double amt){
    System.out.println("deposit "+amt);
    balance=balance-amt;
}

}
package test;

import banking.Account;

public class TestBanking {

    public static void main(String[] args) {
        Account p1= new Account(500);
        p1.withdraw(150);
        p1.deposit(22.5);
        p1.withdraw(47.62);
        System.out.println("The account has a balance of "+p1.geBalance());

    }

}

圓柱體的底面積和高

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        float R,H;
        Scanner reader = new Scanner(System.in);
        Column Column=new Column();
        System.out.println("plaese input R:");
        R=reader.nextFloat();
        System.out.println("plaese input H:");
        H=reader.nextFloat();
        Column.S(R);
        Column.V(R, H);

    }

}

public class Column {

    double pi=java.lang.Math.PI;
    
    public double S(float R) {
        
        System.out.println("s="+pi*R*R);
        return pi*R*R;
        

    }
    
    public double V(float R,float H) {
        
        System.out.println("v="+pi*R*R*H);
        return pi*R*R*H;    

    }

    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容