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

UML 图:

+--------------------+
|   MaxNumberFinder  |
+--------------------+
| -numbers: int[]    |
+--------------------+
| +findMax(): int    |
+--------------------+

源代码:

import java.util.Scanner;

public class MaxNumberFinder {
    private int[] numbers;
    
    public MaxNumberFinder(int[] numbers) {
        this.numbers = numbers;
    }
    
    public int findMax() {
        int max = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > max) {
                max = numbers[i];
            }
        }
        return max;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String[] numbersStr = input.split('\.');
        int[] numbers = new int[numbersStr.length];
        for (int i = 0; i < numbersStr.length; i++) {
            numbers[i] = Integer.parseInt(numbersStr[i]);
        }
        MaxNumberFinder maxNumberFinder = new MaxNumberFinder(numbers);
        System.out.println(maxNumberFinder.findMax());
    }
}

5. Transaction - date: java.util.Date - type: char - amount: double - balance: double - description: String *Transaction (type: char, amount: double, balance: 交易日物 交易类型,例如“W'代表取款。'D”代表存數 交易量 交易后的新氽额 交易的描述 使用给定日期、类型、余额以及描述创建- 个 Transaction double, description: String) 图 11-6 Transaction 类描述银行账户的一笔交易 编写测试程序,创建一个年利率为1.5%、余额为1000、id 为1122 而名字为 George 的 Account。向该账户存人 30 美元、40 美元和 50 美元并从该账户中取出 5 美元、4美元和2美元。 打印账户清单,显示账户持有者名字、利率、余额和所有的交易。

UML 图:

+--------------+
|   Transaction |
+--------------+
| -date: Date   |
| -type: char   |
| -amount: double |
| -balance: double |
| -description: String |
+--------------+
| +Transaction(type: char, amount: double, balance: double, description: String) |
+--------------+

源代码:

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

public 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 Date getDate() {
        return date;
    }
    
    public char getType() {
        return type;
    }
    
    public double getAmount() {
        return amount;
    }
    
    public double getBalance() {
        return balance;
    }
    
    public String getDescription() {
        return description;
    }
    
    public String toString() {
        return date.toString() + '	' + type + '	' + amount + '	' + balance + '	' + description;
    }
}

public class Account {
    private String name;
    private int id;
    private double balance;
    private double annualInterestRate;
    private ArrayList<Transaction> transactions;
    
    public Account(String name, int id, double balance, double annualInterestRate) {
        this.name = name;
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
        this.transactions = new ArrayList<>();
    }
    
    public String getName() {
        return name;
    }
    
    public int getId() {
        return id;
    }
    
    public double getBalance() {
        return balance;
    }
    
    public double getAnnualInterestRate() {
        return annualInterestRate;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public void setBalance(double balance) {
        this.balance = balance;
    }
    
    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    
    public double getMonthlyInterestRate() {
        return annualInterestRate / 12;
    }
    
    public double getMonthlyInterest() {
        return balance * getMonthlyInterestRate() / 100;
    }
    
    public void deposit(double amount, String description) {
        balance += amount;
        transactions.add(new Transaction('D', amount, balance, description));
    }
    
    public void withdraw(double amount, String description) {
        balance -= amount;
        transactions.add(new Transaction('W', amount, balance, description));
    }
    
    public ArrayList<Transaction> getTransactions() {
        return transactions;
    }
}

public class TestAccount {
    public static void main(String[] args) {
        Account account = new Account('George', 1122, 1000, 1.5);
        account.deposit(30, 'salary');
        account.deposit(40, 'bonus');
        account.deposit(50, 'gift');
        account.withdraw(5, 'lunch');
        account.withdraw(4, 'coffee');
        account.withdraw(2, 'bus');
        System.out.println('Name: ' + account.getName());
        System.out.println('Annual interest rate: ' + account.getAnnualInterestRate());
        System.out.println('Balance: ' + account.getBalance());
        System.out.println('Transactions: ');
        for (Transaction transaction : account.getTransactions()) {
            System.out.println(transaction);
        }
    }
}

6. (利用继和实現MyStack)在程序清单 11-10中,MyStack 是用组合实现的。创建一个缝承自 ArrayList 的新的栈类。 画出这些类的 UML图并实现 MyStack 类。编写测试程序,提示用户输人5个字符申,然 后以逆序显示这些字符串。

UML 图:

+---------+     +--------+
| MyStack |     | ArrayList |
+---------+     +--------+
| -list: ArrayList<E>      |
+---------+     +--------+
| +MyStack()       |
| +push(item: E): void |
| +pop(): E          |
| +peek(): E         |
| +getSize(): int    |
| +isEmpty(): boolean |
+---------+     +--------+

源代码:

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

public class MyStack<E> extends ArrayList<E> {
    private ArrayList<E> list;
    
    public MyStack() {
        list = new ArrayList<E>();
    }
    
    public void push(E item) {
        list.add(item);
    }
    
    public E pop() {
        if (list.isEmpty()) {
            return null;
        }
        return list.remove(list.size() - 1);
    }
    
    public E peek() {
        if (list.isEmpty()) {
            return null;
        }
        return list.get(list.size() - 1);
    }
    
    public int getSize() {
        return list.size();
    }
    
    public boolean isEmpty() {
        return list.isEmpty();
    }
    
    public static void main(String[] args) {
        MyStack<Character> stack = new MyStack<>();
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        for (int i = 0; i < input.length(); i++) {
            stack.push(input.charAt(i));
        }
        while (!stack.isEmpty()) {
            System.out.print(stack.pop());
        }
    }
}

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

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