Java 编程练习:UML 图和源代码
4. 查找最大值
UML 图

源代码
import java.util.Scanner;
public class MaxNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a sequence of numbers (end with 0): ");
int max = max(input);
System.out.println("The maximum number is " + max);
}
public static int max(Scanner input) {
int max = input.nextInt();
while (input.hasNext()) {
int number = input.nextInt();
if (number == 0) {
break;
}
if (number > max) {
max = number;
}
}
return max;
}
}
11.5 课程管理
UML 图

源代码
import java.util.ArrayList;
public class Course {
private String courseName;
private ArrayList<String> students;
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
students = new ArrayList<>();
}
public void addStudent(String student) {
students.add(student);
numberOfStudents++;
}
public String[] getStudents() {
return (String[]) students.toArray(new String[students.size()]);
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
students.remove(student);
numberOfStudents--;
}
}
5. 银行账户管理
UML 图

源代码
import java.util.ArrayList;
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private String name;
private ArrayList<Transaction> transactions;
public Account(int id, double balance, String name) {
this.id = id;
this.balance = balance;
this.name = name;
annualInterestRate = 0;
dateCreated = new Date();
transactions = new ArrayList<>();
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public String getName() {
return 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();
}
public void withdraw(double amount) {
balance -= amount;
transactions.add(new Transaction('W', amount, balance, "Withdrawal"));
}
public void deposit(double amount) {
balance += amount;
transactions.add(new Transaction('D', amount, balance, "Deposit"));
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
}
import java.util.ArrayList;
public class Transaction {
private java.util.Date date;
private char type;
private double amount;
private double balance;
private String description;
public Transaction(char type, double amount, double balance, String description) {
date = new java.util.Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public java.util.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 class TestAccount {
public static void main(String[] args) {
Account account = new Account(1122, 1000, "George");
account.setAnnualInterestRate(1.5);
account.deposit(30);
account.deposit(40);
account.deposit(50);
account.withdraw(5);
account.withdraw(4);
account.withdraw(2);
System.out.println("Account holder name: " + account.getName());
System.out.println("Interest rate: " + account.getAnnualInterestRate() + "%");
System.out.println("Balance: $" + account.getBalance());
ArrayList<Transaction> transactions = account.getTransactions();
System.out.printf("%-35s%-10s%-10s%-10s\n", "Date", "Type", "Amount", "Balance");
for (Transaction transaction : transactions) {
System.out.printf("%-35s%-10s%-10s%-10s\n", transaction.getDate(), transaction.getType(), transaction.getAmount(), transaction.getBalance());
}
}
}
6. 栈的实现
UML 图

源代码
import java.util.ArrayList;
public class MyStack extends ArrayList<Character> {
public boolean isEmpty() {
return super.isEmpty();
}
public int getSize() {
return super.size();
}
public char peek() {
return super.get(getSize() - 1);
}
public char pop() {
char ch = super.get(getSize() - 1);
super.remove(getSize() - 1);
return ch;
}
public void push(char ch) {
super.add(ch);
}
}
import java.util.Scanner;
public class TestMyStack {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter five characters: ");
String str = input.nextLine();
MyStack stack = new MyStack();
for (int i = 0; i < str.length(); i++) {
stack.push(str.charAt(i));
}
System.out.print("The reversed string is ");
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
}
}
原文地址: https://www.cveoy.top/t/topic/n4nX 著作权归作者所有。请勿转载和采集!