JAVA编程语言设计形考四任务源代码截图
任务一:编写一个简单的Java程序
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
任务二:实现一个求解阶乘的Java方法
public class Factorial {
public static int calculateFactorial(int num) {
if (num == 0 || num == 1) {
return 1;
} else {
return num * calculateFactorial(num - 1);
}
}
}
任务三:编写一个Java类,实现一个简单的计算器
public class Calculator {
public static int add(int num1, int num2) {
return num1 + num2;
}
public static int subtract(int num1, int num2) {
return num1 - num2;
}
public static int multiply(int num1, int num2) {
return num1 * num2;
}
public static int divide(int num1, int num2) {
if (num2 == 0) {
throw new IllegalArgumentException("Cannot divide by zero!");
}
return num1 / num2;
}
}
任务四:实现一个简单的Java图书管理系统
import java.util.ArrayList;
import java.util.List;
public class BookManagementSystem {
private List<Book> books;
public BookManagementSystem() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
ve(book);
}
public List<Book> searchBooks(String keyword) {
List<Book> searchResults = new ArrayList<>();
for (Book book : books) {
if (Title().contains(keyword) || Author().contains(keyword)) {
searchResults.add(book);
}
}
return searchResults;
}
}
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
java arraylist用法}
public String getAuthor() {
return author;
}
}
以上是JAVA编程语言设计形考四任务的源代码截图。希望对你有帮助!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论