编写两个类,TriAngle和TriAngleTest,其中TriAngle类中声明私有的底边长base和高height,同时声明公共方法访问私有变量。此外,提供类必要的构造器。另一个类中使用这些公共方法,计算三角形的面积。

public class TriAngle { //angle:角    angel:天使
    
    private double base;//底边长
    private double height;//高
    
    public TriAngle(){
        
    }
    
    public TriAngle(double b,double h){
        base = b;
        height = h;
    }
    
    
    public void setBase(double b){
        base = b;
    }
    public double getBase(){
        return base;
    }
    public void setHeight(double h){
        height = h;
    }
    public double getHeight(){
        return height;
    }
    
}
public class TriAngleTest {
    public static void main(String[] args) {
        
        TriAngle t1 = new TriAngle();
        t1.setBase(2.0);
        t1.setHeight(2.4);
        //t1.base = 2.5;//The field TriAngle.base is not visible
        //t1.height = 4.3;
        System.out.println("base : " + t1.getBase() + ",height : " + t1.getHeight());
        
        TriAngle t2 = new TriAngle(5.1,5.6);
        System.out.println("base : " + t2.getBase() + ",height : " + t2.getHeight());
    }
}

写一个测试程序。

  • 创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
  • 对Jane Smith操作。存入 100 元,再取出960元。再取出2000元。打印出Jane Smith 的基本信息
public class Customer {
    
    private String firstName;
    private String lastName;
    private Account account;
    
    public Customer(String f,String l){
        this.firstName = f;
        this.lastName = l;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
    
}
public class Account {
    private int id;//账号
    private double balance;//余额
    private double annualInterestRate;//年利率
    
    public Account (int id, double balance, double annualInterestRate ){
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    //在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
    public void withdraw (double amount){//取钱
        if(balance < amount){
            System.out.println("余额不足,取款失败");
            return;
        }
        balance -= amount;
        System.out.println("成功取出:" + amount);
    }
    
    public void deposit (double amount){//存钱
        if(amount > 0){
            balance += amount;
            System.out.println("成功存入:" + amount);
        }
    }

}
public class CustomerTest {
    public static void main(String[] args) {
        Customer cust = new Customer("Jane", "Smith");
        
        Account acct = new Account(1000, 2000, 0.0123);
        
        cust.setAccount(acct);
        
        cust.getAccount().deposit(100);
        cust.getAccount().withdraw(960);
        cust.getAccount().withdraw(2000);
        
        System.out.println("Customer[" + cust.getLastName() + "," + cust.getFirstName() + 
                "] has a account: id is " + cust.getAccount().getId() + ",annualInterestRate is "+
        cust.getAccount().getAnnualInterestRate() * 100 + "% ,balance is "+cust.getAccount().getBalance());
    }
}

扩展程序

public class Customer {

    
    private String firstName;
    private String lastName;
    private Account account;
    
    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
    
    
}
public class Account {
    
    private double balance;
    
    public Account(double init_balance){
        this.balance = init_balance;
    }
    
    public double getBalance(){
        return balance;
    }
    
    //存钱操作
    public void deposit(double amt){
        if(amt > 0){
            balance += amt;
            System.out.println("存钱成功");
        }
    }
    //取钱操作
    public void withdraw(double amt){
        if(balance >= amt){
            balance -= amt;
            System.out.println("取钱成功");
        }else{
            System.out.println("余额不足");
        }
    }
}
public class Bank {

    private Customer[] customers;// 存放多个客户的数组
    private int numberOfCustomers;// 记录客户的个数

    public Bank() {
        customers = new Customer[10];
    }

    // 添加客户
    public void addCustomer(String f, String l) {
        Customer cust = new Customer(f, l);
        // customers[numberOfCustomers] = cust;
        // numberOfCustomers++;
        // 或
        customers[numberOfCustomers++] = cust;
    }

    // 获取客户的个数
    public int getNumOfCustomers() {
        return numberOfCustomers;
    }

    // 获取指定位置上的客户
    public Customer getCustomer(int index) {
        // return customers[index];//可能报异常
        if (index >= 0 && index < numberOfCustomers) {
            return customers[index];
        }

        return null;
    }

}
public class BankTest {
    public static void main(String[] args) {
        
        Bank bank = new Bank();
        
        bank.addCustomer("Jane", "Smith");
        
        //连续操作
        bank.getCustomer(0).setAccount(new Account(2000));
        
        bank.getCustomer(0).getAccount().withdraw(500);
        
        double balance = bank.getCustomer(0).getAccount().getBalance();
        System.out.println("客户:" + bank.getCustomer(0).getFirstName() + "的账户余额为:" + balance);
        
        System.out.println("***********************");
        bank.addCustomer("万里", "杨");
        
        System.out.println("银行客户的个数为:" + bank.getNumOfCustomers());
        
        
    }
}

编写ArrayUtil工具类

public class ArrayUtil {

    //求数组的最大值
    public int getMax(int[] arr){
        int maxValue = 0;
        for(int i = 0;i < arr.length;i++){
            if(arr[i] > maxValue){
                maxValue = arr[i];
            }
        }
        return maxValue;
    }
    
    //求数组最小值
    public int getMin(int[] arr){
        int minValue = arr[1];
        for(int i = 0;i < arr.length;i++){
            if(arr[i] < minValue){
                minValue = arr[i];
            }
        }
        return minValue;
    }
    
    //求数组的总和
    public int getSum(int[] arr){
        int temp = 0;
        for(int i = 0;i < arr.length;i++){
            temp += arr[i];
        }
        return temp;
    }
    
    //求数组的平均值
    public double getAvg(int[] arr){
        double average = getSum(arr) / arr.length;
        return average;
    }
    
    //反转数组
    public void reverse(int[] arr){
        for(int i = 0;i < arr.length / 2;i++){
            int temp = arr[i];
            arr[i] = arr[arr.length - i - 1];
            arr[arr.length - i - 1] = temp;
        }
    }
    
    //复制数组
    public int[] copy(int[] arr){
        int[] newArr = new int[arr.length];
        for(int i = 0; i < arr.length; i++){
            newArr[i] = arr[i];
        }
        return newArr;
    }
    
    //数组排序
    public void sort(int[] arr){
        for(int i = 0;i < arr.length - 1;i++){
            for(int j = 0;j < arr.length - i -1;j++){
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    
    //遍历数组
    public void print(int[] arr){
        for(int i = 0;i < arr.length;i++){
            System.out.print(arr[i] + "\t");
        }
    }
    
    //查找数值元素
    public int getIndex(int[] arr, int dest){
        /**二分法
        int head = 0;
        int end = arr.length - 1;
        boolean isFlag = true;
        int temp = 0;
        
        while(head <= end){
            int middle = (head + end) / 2;
            if(dest == arr[middle]){
                temp = middle;
                isFlag = false;
                break;
            }else if(arr[middle] > dest){
                end = middle - 1;
            }else{
                head = middle + 1;
            }
        }
        */
        for(int i = 0; i < arr.length; i++){
            if(dest == arr[i]){
                return i;
            }
        }
        return -1;
    }
}

测试ArrayUtil类

public class ArrayUtilTest {

    public static void main(String[] args) {
        ArrayUtil util = new ArrayUtil();
        int[] arr = new int[]{34,15,-15,-196,26,55,97,0,-45,34,55};
        int max = util.getMax(arr);
        System.out.println("最大值为:" + max);
        
        util.sort(arr);
        util.print(arr);
        System.out.println();
        int index = util.getIndex(arr, 34);
        System.out.println(index);
        
    }
    
}
最后修改:2021 年 08 月 06 日 10 : 14 AM
如果觉得我的文章对你有用,请随意赞赏