Java 编程练习:数值序列最大值、银行账户交易、栈实现

1. 查找数值序列最大值

题目: 编写测试程序,提示用户输入一个以‘.’结尾的数值序列,调用该方法返回输入的最大数值。

代码:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println('请输入以‘.’结尾的数值序列:');
        String str = input.nextLine();
        String[] strArr = str.split('\.');
        double max = Double.MIN_VALUE;
        for (String s : strArr) {
            double temp = Double.parseDouble(s);
            if (temp > max) {
                max = temp;
            }
        }
        System.out.println('最大值为:' + max);
    }
}

2. 模拟银行账户交易

题目: 改写程序清单 10-6 中的 Course 类,使用 ArrayList 代替数组来存储学生。为该类绘制新的 UML 图。不能改变 Course 类之前的合约(即构造方法和方法的定义都不能改变,但私有的成员可以改变)。

代码:

import java.util.ArrayList;
import java.util.Date;

public class Account {
    private String name;
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;
    private ArrayList<Transaction> transactions;

    public Account(String name, int id, double balance) {
        this.name = name;
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = 0;
        this.dateCreated = new Date();
        this.transactions = new ArrayList<>();
    }

    public void deposit(double amount) {
        balance += amount;
        transactions.add(new Transaction('D', amount, balance, '存款'));
    }

    public void withdraw(double amount) {
        balance -= amount;
        transactions.add(new Transaction('W', amount, balance, '取款'));
    }

    public void setAnnualInterestRate(double rate) {
        annualInterestRate = rate;
    }

    public double getMonthlyInterestRate() {
        return annualInterestRate / 12;
    }

    public double getMonthlyInterest() {
        return balance * getMonthlyInterestRate();
    }

    public ArrayList<Transaction> getTransactions() {
        return transactions;
    }

    public String toString() {
        return '账户名:' + name + '\n余额:' + balance + '\n年利率:' + annualInterestRate;
    }
}

class Transaction {
    private Date date;
    private char type;
    private double amount;
    private double balance;
    private String description;

    public Transaction(char type, double amount, double balance, String description) {
        this.date = new Date();
        this.type = type;
        this.amount = amount;
        this.balance = balance;
        this.description = description;
    }

    public String toString() {
        return date + '\t' + type + '\t' + amount + '\t' + balance + '\t' + description;
    }
}

public class Test {
    public static void main(String[] args) {
        Account account = new Account('George', 1122, 1000);
        account.setAnnualInterestRate(0.015);

        account.deposit(30);
        account.deposit(40);
        account.deposit(50);
        account.withdraw(5);
        account.withdraw(4);
        account.withdraw(2);

        System.out.println(account);
        ArrayList<Transaction> transactions = account.getTransactions();
        System.out.println('交易清单:');
        for (Transaction t : transactions) {
            System.out.println(t);
        }
    }
}

3. 实现栈数据结构

题目: 在程序清单 11-10 中,MyStack 是用组合实现的。创建一个继承自 ArrayList 的新的栈类。画出这些类的 UML 图并实现 MyStack 类。编写测试程序,提示用户输入 5 个字符,然后以逆序显示这些字符串。

UML 图:

MyStack
-------
- list: ArrayList<Object>

+ MyStack()
+ isEmpty(): boolean
+ getSize(): int
+ peek(): Object
+ pop(): Object
+ push(o: Object): void

代码:

import java.util.ArrayList;
import java.util.Scanner;

public class MyStack extends ArrayList<Object> {
    public MyStack() {
    }

    public boolean isEmpty() {
        return super.isEmpty();
    }

    public int getSize() {
        return super.size();
    }

    public Object peek() {
        return super.get(super.size() - 1);
    }

    public Object pop() {
        Object o = super.get(super.size() - 1);
        super.remove(super.size() - 1);
        return o;
    }

    public void push(Object o) {
        super.add(o);
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println('请输入5个字符:');
        String str = input.nextLine();
        MyStack stack = new MyStack();
        for (int i = 0; i < str.length(); i++) {
            stack.push(str.charAt(i));
        }
        System.out.print('逆序输出:');
        while (!stack.isEmpty()) {
            System.out.print(stack.pop());
        }
    }
}
Java 编程练习:数值序列最大值、银行账户交易、栈实现

原文地址: https://www.cveoy.top/t/topic/n4nt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录