UML 图与 Java 类实现示例

1. Triangle 类和 GeometricObject 类

UML 图

Triangle 类和 GeometricObject 类的 UML 图

Triangle 类源代码javapublic class Triangle extends GeometricObject { private double side1 = 1.0; private double side2 = 1.0; private double side3 = 1.0;

public Triangle() {}

public Triangle(double side1, double side2, double side3) {        this.side1 = side1;        this.side2 = side2;        this.side3 = side3;    }

public double getSide1() {        return side1;    }

public double getSide2() {        return side2;    }

public double getSide3() {        return side3;    }

public double getArea() {        double p = getPerimeter() / 2;        return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));    }

public double getPerimeter() {        return side1 + side2 + side3;    }

public String toString() {        return 'Triangle: side1 = ' + side1 + ' side2 = ' + side2 + ' side3 = ' + side3;    }}

测试程序源代码javaimport java.util.Scanner;

public class TestTriangle { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print('Enter three sides of the triangle: '); double side1 = input.nextDouble(); double side2 = input.nextDouble(); double side3 = input.nextDouble(); System.out.print('Enter the color of the triangle: '); String color = input.next(); System.out.print('Is the triangle filled (true / false)? '); boolean filled = input.nextBoolean(); Triangle triangle = new Triangle(side1, side2, side3); triangle.setColor(color); triangle.setFilled(filled); System.out.println('Area: ' + triangle.getArea()); System.out.println('Perimeter: ' + triangle.getPerimeter()); System.out.println('Color: ' + triangle.getColor()); System.out.println('Filled: ' + triangle.isFilled()); }}

2. Person、Student、Employee、Faculty 和 Staff 类

UML 图

Person、Student、Employee、Faculty 和 Staff 类的 UML 图

Person 类源代码javapublic class Person { private String name; private String address; private String phoneNumber; private String email;

public Person(String name, String address, String phoneNumber, String email) {        this.name = name;        this.address = address;        this.phoneNumber = phoneNumber;        this.email = email;    }

public String getName() {        return name;    }

public String getAddress() {        return address;    }

public String getPhoneNumber() {        return phoneNumber;    }

public String getEmail() {        return email;    }

public String toString() {        return getClass().getName() + ': ' + name;    }}

Student 类源代码javapublic class Student extends Person { public static final String FRESHMAN = 'Freshman'; public static final String SOPHOMORE = 'Sophomore'; public static final String JUNIOR = 'Junior'; public static final String SENIOR = 'Senior'; private String status;

public Student(String name, String address, String phoneNumber, String email, String status) {        super(name, address, phoneNumber, email);        this.status = status;    }

public String getStatus() {        return status;    }

public String toString() {        return super.toString() + ' (Student)';    }}

Employee 类源代码javapublic class Employee extends Person { private String office; private double salary; private MyDate hireDate;

public Employee(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate) {        super(name, address, phoneNumber, email);        this.office = office;        this.salary = salary;        this.hireDate = hireDate;    }

public String getOffice() {        return office;    }

public double getSalary() {        return salary;    }

public MyDate getHireDate() {        return hireDate;    }

public String toString() {        return super.toString() + ' (Employee)';    }}

Faculty 类源代码javapublic class Faculty extends Employee { private String officeHours; private String rank;

public Faculty(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate, String officeHours, String rank) {        super(name, address, phoneNumber, email, office, salary, hireDate);        this.officeHours = officeHours;        this.rank = rank;    }

public String getOfficeHours() {        return officeHours;    }

public String getRank() {        return rank;    }

public String toString() {        return super.toString() + ' (Faculty)';    }}

Staff 类源代码javapublic class Staff extends Employee { private String title;

public Staff(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate, String title) {        super(name, address, phoneNumber, email, office, salary, hireDate);        this.title = title;    }

public String getTitle() {        return title;    }

public String toString() {        return super.toString() + ' (Staff)';    }}

测试程序源代码javapublic class TestPerson { public static void main(String[] args) { Person person = new Person('John Doe', '123 Main St', '555-1234', 'johndoe@example.com'); System.out.println(person);

    Student student = new Student('Jane Smith', '456 Oak Ave', '555-5678', 'janesmith@example.com', Student.SOPHOMORE);        System.out.println(student);

    Employee employee = new Employee('Bob Jones', '789 Elm St', '555-9012', 'bobjones@example.com', '101', 50000, new MyDate(2000, 1, 1));        System.out.println(employee);

    Faculty faculty = new Faculty('Mary Johnson', '111 Pine St', '555-3456', 'maryjohnson@example.com', '102', 60000, new MyDate(1990, 1, 1), '9-11 AM', 'Associate Professor');        System.out.println(faculty);

    Staff staff = new Staff('Tom Smith', '222 Maple St', '555-7890', 'tomsmith@example.com', '103', 40000, new MyDate(2010, 1, 1), 'Secretary');        System.out.println(staff);    }}

3. Account、SavingsAccount 和 CheckingAccount 类

UML 图

Account、SavingsAccount 和 CheckingAccount 类的 UML 图

Account 类源代码javapublic class Account { private int id; private double balance; private double annualInterestRate; private MyDate dateCreated;

public Account(int id, double balance, double annualInterestRate) {        this.id = id;        this.balance = balance;        this.annualInterestRate = annualInterestRate;        this.dateCreated = new MyDate();    }

public int getId() {        return id;    }

public double getBalance() {        return balance;    }

public double getAnnualInterestRate() {        return annualInterestRate;    }

public MyDate getDateCreated() {        return dateCreated;    }

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

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

public void withdraw(double amount) {        balance -= amount;    }

public void deposit(double amount) {        balance += amount;    }

public String toString() {        return getClass().getName() + ': ' + id;    }}

SavingsAccount 类源代码javapublic class SavingsAccount extends Account { public SavingsAccount(int id, double balance, double annualInterestRate) { super(id, balance, annualInterestRate); }

public void withdraw(double amount) {        if (getBalance() - amount >= 0) {            super.withdraw(amount);        }    }

public String toString() {        return super.toString() + ' (SavingsAccount)';    }}

CheckingAccount 类源代码javapublic class CheckingAccount extends Account { private double overdraftLimit;

public CheckingAccount(int id, double balance, double annualInterestRate, double overdraftLimit) {        super(id, balance, annualInterestRate);        this.overdraftLimit = overdraftLimit;    }

public void withdraw(double amount) {        if (getBalance() + overdraftLimit - amount >= 0) {            super.withdraw(amount);        }    }

public String toString() {        return super.toString() + ' (CheckingAccount)';    }}

测试程序源代码javapublic class TestAccount { public static void main(String[] args) { Account account = new Account(1122, 1000, 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: George'); System.out.println('Interest rate: ' + account.getAnnualInterestRate() + '%'); System.out.println('Balance: $' + account.getBalance()); System.out.println('Transactions:'); System.out.println('Date Type Amount Balance Description'); for (Transaction transaction : account.getTransaction()) { System.out.println(transaction); }

    SavingsAccount savingsAccount = new SavingsAccount(1122, 1000, 1.5);        savingsAccount.deposit(30);        savingsAccount.deposit(40);        savingsAccount.deposit(50);        savingsAccount.withdraw(5);        savingsAccount.withdraw(4);        savingsAccount.withdraw(2);        System.out.println('Account holder: George');        System.out.println('Interest rate: ' + savingsAccount.getAnnualInterestRate() + '%');        System.out.println('Balance: $' + savingsAccount.getBalance());        System.out.println('Transactions:');        System.out.println('Date		Type	Amount	Balance	Description');        for (Transaction transaction : savingsAccount.getTransaction()) {            System.out.println(transaction);        }

    CheckingAccount checkingAccount = new CheckingAccount(1122, 1000, 1.5, 500);        checkingAccount.deposit(30);        checkingAccount.deposit(40);        checkingAccount.deposit(50);        checkingAccount.withdraw(5);        checkingAccount.withdraw(4);        checkingAccount.withdraw(2);        System.out.println('Account holder: George');        System.out.println('Interest rate: ' + checkingAccount.getAnnualInterestRate() + '%');        System.out.println('Balance: $' + checkingAccount.getBalance());        System.out.println('Transactions:');        System.out.println('Date		Type	Amount	Balance	Description');        for (Transaction transaction : checkingAccount.getTransaction()) {            System.out.println(transaction);        }    }}

4. Course 类

UML 图

Course 类的 UML 图

Course 类源代码javaimport java.util.ArrayList;

public class Course { private String courseName; private ArrayList students;

public Course(String courseName) {        this.courseName = courseName;        students = new ArrayList<>();    }

public void addStudent(String student) {        students.add(student);    }

public ArrayList<String> getStudents() {        return students;    }

public int getNumberOfStudents() {        return students.size();    }

public String getCourseName() {        return courseName;    }}

测试程序源代码javaimport java.util.Scanner;

public class TestCourse { public static void main(String[] args) { Scanner input = new Scanner(System.in); Course course = new Course('Java Programming'); System.out.print('Enter the number of students: '); int numberOfStudents = input.nextInt(); for (int i = 0; i < numberOfStudents; i++) { System.out.print('Enter student name: '); String studentName = input.next(); course.addStudent(studentName); } System.out.println('Number of students in ' + course.getCourseName() + ': ' + course.getNumberOfStudents()); System.out.println('Students in ' + course.getCourseName() + ':'); for (String student : course.getStudents()) { System.out.println(student); } }}

5. Transaction 类

UML 图

Transaction 类的 UML 图

Transaction 类源代码javaimport 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 + '	' + type + '	' + amount + '	' + balance + '	' + description;    }}

测试程序源代码javapublic class TestAccount { public static void main(String[] args) { Account account = new Account(1122, 1000, 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: George'); System.out.println('Interest rate: ' + account.getAnnualInterestRate() + '%'); System.out.println('Balance: $' + account.getBalance()); System.out.println('Transactions:'); System.out.println('Date Type Amount Balance Description'); for (Transaction transaction : account.getTransaction()) { System.out.println(transaction); } }}

6. MyStack 类

UML 图

MyStack 类的 UML 图

MyStack 类源代码javaimport java.util.ArrayList;

public class MyStack extends ArrayList { public E peek() { return get(size() - 1); }

public E pop() {        E o = peek();        remove(size() - 1);        return o;    }

public void push(E o) {        add(o);    }}

测试程序源代码javaimport java.util.Scanner;

public class TestMyStack { public static void main(String[] args) { Scanner input = new Scanner(System.in); MyStack stack = new MyStack<>(); System.out.print('Enter five strings: '); for (int i = 0; i < 5; i++) { stack.push(input.next()); } System.out.println('The strings in reverse order are:'); while (!stack.isEmpty()) { System.out.print(stack.pop() + ' '); }

UML 图与 Java 类实现:三角形、人员信息、银行账户、课程与交易

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

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