创建一个学生类 编写一个名为 Student
的类,
包含以下属性和方法:
- 属性:姓名(
name
)、年龄(age
)、学号(studentId
)、成绩(score
) - 方法:构造方法、获取姓名的方法、获取年龄的方法、获取学号的方法、获取成绩的方法、设置成绩的方法
代码实现
package kfm.bases.ObjectOriented;
public class Students {
private String name;
private Byte age;
private String studentId;
private Double score;
public Students(String name, int age, String id, double score) {
this.name = name;
this.age = (byte) age;
this.studentId = id;
this.setScore(score);
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getStudentId() {
return studentId;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public static void main(String[] args) {
Students xiao = new Students("小羊呆呆", 18, "20201301052", 100);
System.out.println(xiao.getName() + '\t' + xiao.getAge() + '\t' + xiao.getStudentId() + '\t' + xiao.getScore());
}
}
银行账户管理 创建一个名为 BankAccount
的类,用于管理银行账户。
该类包含以下属性和方法:
- 属性:账户号码(
accountNumber
)、账户持有人姓名(accountHolder
)、余额(balance
) - 方法:构造方法、获取账户号码的方法、获取账户持有人姓名的方法、获取余额的方法、存款方法、取款方法
代码实现
package kfm.bases.ObjectOriented;
public class BankAccount {
private String accountNumber;
private String accountHolder;
private Double balance;
public BankAccount(String accountNumber, String accountHolder, double balance) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountHolder() {
return accountHolder;
}
public double getBalance() {
return balance;
}
private void deposit(double amount) {
balance += amount;
System.out.println("存款成功!当前余额为:" + balance);
}
private void withdraw(double amount) {
if (balance < amount) {
System.out.println("余额不足,无法取款。当前余额:" + balance);
} else {
balance -= amount;
System.out.println("取款成功!当前余额:" + balance);
}
}
public static void main(String[] args) {
BankAccount account = new BankAccount("1314520", "小羊呆呆", 520);
System.out.println("账户号码:" + account.getAccountNumber());
System.out.println("账户持有人姓名:" + account.getAccountHolder());
System.out.println("账户余额:" + account.getBalance());
account.deposit(500.0);
account.withdraw(200.0);
account.withdraw(1500.0);
}
}
几何图形计算器 设计一个几何图形计算器,可以计算圆的面积和矩形的面积。创建名为 GeometryCalculator
的类
包含以下实例方法:
calculateCircleArea(double radius)
:计算并返回圆的面积calculateRectangleArea(double width, double height)
:计算并返回矩形的面积- 完成计算圆的面积以及计算矩形的面积等功能。
代码实现
package kfm.bases.ObjectOriented;
public class GeometryCalculator {
public double calculateCircleArea(double radius) {
if (radius < 0) {
return -1;
} else {
return Math.PI * Math.pow(radius, 2);
}
}
public double calculateRectangleArea(double width, double height) {
if (width < 0 || height < 0) {
return -1;
} else {
return width * height;
}
}
public static void main(String[] args) {
GeometryCalculator calculator = new GeometryCalculator();
double circleArea = calculator.calculateCircleArea(- 2);
System.out.println("圆的面积:" + circleArea);
double rectangleArea = calculator.calculateRectangleArea(4.0, 6.0);
System.out.println("矩形的面积:" + rectangleArea);
}
}
图书管理系统 创建一个简单的图书管理系统
包含以下类:
Book
类:表示图书,包含属性:书名(title
)、作者(author
)、出版年份(year
)、价格(price
)Library
类:表示图书馆,包含属性:书库(使用数组保存图书)、添加图书的方法、查找图书的方法、显示所有图书的方法- 完成以下功能
- 添加图书
- 查找图书
- 显示图书列表。
代码实现
public class Book {
private String title;
private String author;
private int year;
private double price;
public Book(String title, String author, int year, double price) {
this.title = title;
this.author = author;
this.year = year;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
}
public class Library {
private Book[] books;
private int numBooks;
public Library(int capacity) {
books = new Book[capacity];
numBooks = 0;
}
public void addBook(Book book) {
if (numBooks < books.length) {
books[numBooks] = book;
numBooks++;
System.out.println("成功添加图书:" + book.getTitle());
} else {
System.out.println("图书馆已满,无法添加更多图书。");
}
}
public Book findBook(String title) {
for (int i = 0; i < numBooks; i++) {
if (books[i].getTitle().equals(title)) {
return books[i];
}
}
return null;
}
public void displayBooks() {
System.out.println("图书列表:");
for (int i = 0; i < numBooks; i++) {
Book book = books[i];
System.out.println("书名:" + book.getTitle());
System.out.println("作者:" + book.getAuthor());
System.out.println("出版年份:" + book.getYear());
System.out.println("价格:" + book.getPrice());
System.out.println();
}
}
}