Browse Source

提交期末项目

main
baihuijuan 3 weeks ago
parent
commit
1abfa96cd6
  1. 2
      README.md
  2. 7
      W1-白慧娟-202506050330/AI_help.txt
  3. 22
      W1-白慧娟-202506050330/README.md
  4. 23
      W1-白慧娟-202506050330/TemperatureConverter.java
  5. 7
      W1-白慧娟-202506050330/output.txt
  6. 29
      student/StudentTest.java
  7. 1
      w1/HelloWorld.java
  8. 0
      w10/AI
  9. 4
      w10/AnalyzeCommand.java
  10. 27
      w10/article.java
  11. 4
      w10/articlerepository.java
  12. 27
      w11/ArticleA.java
  13. 4
      w11/ArticleRepository.java
  14. 4
      w11/BlogStrategy.java
  15. 4
      w11/Command.java
  16. 4
      w11/CrawlCommand.java
  17. 4
      w11/CrawlStrategy.java
  18. 4
      w11/CrawlerException.java
  19. 4
      w11/HnuNewsStrategy.java
  20. 4
      w11/Main11.java
  21. 4
      w11/NetworkException.java
  22. 4
      w11/NewsStrategy.java
  23. 4
      w11/ParseException.java
  24. 0
      w11/logback
  25. 0
      w11/pom
  26. 32
      w2/DataCleaner.java
  27. 56
      w3/BankAccount.java
  28. 82
      w3/Employee.java
  29. 12
      w4/Circle.java
  30. 11
      w4/Main.java
  31. 13
      w4/Rectangle.java
  32. 3
      w4/Shape.java
  33. 5
      w4/ShapeUtil.java
  34. 15
      w4/Triangle.java
  35. 6
      w5/A_Circlew5.java
  36. 5
      w5/A_Graphic.java
  37. 15
      w5/A_Main1.java
  38. 6
      w5/A_Rectanglew5.java
  39. 6
      w5/Bike.java
  40. 6
      w5/Car.java
  41. 12
      w5/Main2.java
  42. 6
      w5/Truck.java
  43. 3
      w5/Vehicle.java
  44. 32
      w6/Animal.java
  45. 21
      w6/AnimalTest.java
  46. BIN
      w7/20.jpg
  47. BIN
      w7/35.jpg
  48. 71
      w7/ScoreCalculator.java
  49. 5
      w7/scores
  50. 5
      w8/AI
  51. 26
      w8/Cache.java
  52. 23
      w8/Pair.java
  53. 14
      w9/AI
  54. 27
      w9/Article.java
  55. 8
      w9/Controller.java
  56. 28
      w9/HistoryCommand.java

2
README.md

@ -1,2 +0,0 @@
# java

7
W1-白慧娟-202506050330/AI_help.txt

@ -0,0 +1,7 @@
AI协助记录:
在完成这次Java作业过程中,我使用了AI助手。首先,我请AI将Python温度转换程序改写为Java版本,AI提供了完整的代码和中文注释。
在编译运行过程中遇到输入格式错误的问题,AI一步步指导我修改代码,采用分步输入温度和单位的方式,最终程序成功运行,可以正确转换摄氏度和华氏度。
AI还帮我创建了README.md说明文档和output.txt运行结果文件,指导我完成所有作业要求。

22
W1-白慧娟-202506050330/README.md

@ -0,0 +1,22 @@
\# 温度转换程序
\## 编译方法
javac TemperatureConverter.java
\## 运行方法
java TemperatureConverter
\## 运行示例
输入:36.6 C
输出:36.6℃ = 97.88℉

23
W1-白慧娟-202506050330/TemperatureConverter.java

@ -0,0 +1,23 @@
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入温度值:");
double value = scanner.nextDouble();
System.out.println("请输入单位(C表示摄氏度,F表示华氏度):");
String unit = scanner.next().toUpperCase();
if (unit.equals("C")) {
double f = value * 9.0 / 5.0 + 32.0;
System.out.println(value + "℃ = " + f + "℉");
} else if (unit.equals("F")) {
double c = (value - 32.0) * 5.0 / 9.0;
System.out.println(value + "℉ = " + c + "℃");
}
scanner.close();
}
}

7
W1-白慧娟-202506050330/output.txt

@ -0,0 +1,7 @@
AI协助记录:
本次作业使用AI助手将Python温度转换程序改写为Java版本。AI提供了完整的Java代码和中文注释。
在运行过程中遇到输入格式问题,AI指导修改代码,采用分步输入的方式,解决了特殊符号解析问题。程序最终成功运行,能正确转换摄氏度和华氏度。
AI还协助创建了README.md说明文档和output.txt运行结果文件,帮助完成所有作业要求。

29
student/StudentTest.java

@ -0,0 +1,29 @@
public class StudentTest {public static void main(String[] args) {
Student stu1 = new Student();
stu1.studentId = "001";
stu1.name = "Wang";
stu1.score = 85.5;
Student stu2 = stu1;
stu1.study();
stu2.study();
System.out.println("stu1 的名字: " + stu1.name);
System.out.println("stu2 的名字: " + stu2.name);
stu2.name = "Li";
System.out.println("修改后 stu1 的名字: " + stu1.name);
System.out.println("修改后 stu2 的名字: " + stu2.name);
}
}
class Student {String studentId;
String name;
double score;
public void study() {
System.out.println(name + " 正在学习");
}
}

1
w1/HelloWorld.java

@ -3,4 +3,3 @@ public class HelloWorld {
System.out.println("Hello,World!"); System.out.println("Hello,World!");
} }
} }

0
w10/AI

4
w10/AnalyzeCommand.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class AnalyzeCommand {
}

27
w10/article.java

@ -0,0 +1,27 @@
import java.util.Date;
public class Article {
private String title;
private String content;
private String url;
private String author; // 新增
private Date publishDate; // 新增
// 构造器
public Article(String title, String content, String url, String author, Date publishDate) {
this.title = title;
this.content = content;
this.url = url;
this.author = author;
this.publishDate = publishDate;
}
// Getters and Setters
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public Date getPublishDate() { return publishDate; }
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; }
// 原有字段的 getter/setter...
}

4
w10/articlerepository.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class articlerepository {
}

27
w11/ArticleA.java

@ -0,0 +1,27 @@
import java.util.Date;
public class article {
private String title;
private String content;
private String url;
private String author; // 新增
private Date publishDate; // 新增
// 构造器
public article(String title, String content, String url, String author, Date publishDate) {
this.title = title;
this.content = content;
this.url = url;
this.author = author;
this.publishDate = publishDate;
}
// Getters and Setters
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public Date getPublishDate() { return publishDate; }
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; }
// 原有字段的 getter/setter...
}

4
w11/ArticleRepository.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class ArticleRepository {
}

4
w11/BlogStrategy.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class BlogStrategy {
}

4
w11/Command.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class Command {
}

4
w11/CrawlCommand.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class CrawlCommand {
}

4
w11/CrawlStrategy.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class CrawlStrategy {
}

4
w11/CrawlerException.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class CrawlerException {
}

4
w11/HnuNewsStrategy.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class HnuNewsStrategy {
}

4
w11/Main11.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class Main {
}

4
w11/NetworkException.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class NetworkException {
}

4
w11/NewsStrategy.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class NewsStrategy {
}

4
w11/ParseException.java

@ -0,0 +1,4 @@
package PACKAGE_NAME;
public class ParseException {
}

0
w11/logback

0
w11/pom

32
w2/DataCleaner.java

@ -0,0 +1,32 @@
public class DataCleaner {public static void main(String[] args) {
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76};
int validSum = 0; // 有效数据总和
int validCount = 0; // 有效数据个数
for (int data : sensorData) {
if (data == 999) {
System.out.println("致命错误:传感器掉线,终止处理");
break;
}
if (data <= 0 || data > 100) {
System.out.println("警告:发现越界数据 " + data + ",已跳过");
continue;
}
// 正常范围:1 <= data <= 100
validSum += data;
validCount++;
}
// 最终输出
if (validCount > 0) {
double average = (double) validSum / validCount;
System.out.println("有效数据的平均值:" + average);
} else {
System.out.println("无有效数据");
}
}
}

56
w3/BankAccount.java

@ -0,0 +1,56 @@
public class BankAccount {
private final String accountNumber;
private String ownerName;
private double balance;
public BankAccount(){
this("000000","未命名");
}
public BankAccount(String accountNumber, String ownerName) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance=0.0;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public void deposit(double amount) {
if(amount>0){
this.balance += amount;
System.out.println("存款成功,当前余额:" +this.balance);
}else{
System.out.println("存款必须大于0");
}
}
public void withdraw(double amount) {
if(amount<=0){
System.out.println("取款金额必须大于0");
}else if(amount>this.balance){
System.out.println("金额不足,当前余额:"+this.balance);
}else {
this.balance -= amount;
System.out.println("取款成功!当前余额:" + this.balance);
}
}
// 查询余额的方法
public double getBalance() {
return this.balance;
}
// 获取账户号的方法(因为账户号不可修改,只提供getter)
public String getAccountNumber() {
return this.accountNumber;
}
// 获取户主姓名的方法
public String getOwnerName() {
return this.ownerName;
}
}

82
w3/Employee.java

@ -0,0 +1,82 @@
public class Employee {
private String id;
private String name;
private String department;
private double salary;
// 全参构造方法
public Employee(String id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
setSalary(salary);
}
// getter和setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if (salary >= 2000) {
this.salary = salary;
} else {
System.out.println("工资不能低于2000!");
}
}
// 涨薪方法
public void raiseSalary(double percent) {
double newSalary = salary * (1 + percent / 100);
setSalary(newSalary);
}
}
// 测试类
class TestEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee("1001", "张三", "技术部", 5000);
Employee emp2 = new Employee("1002", "李四", "销售部", 3000);
System.out.println("=== 初始信息 ===");
System.out.println(emp1.getName() + " - " + emp1.getDepartment() + " - 工资:" + emp1.getSalary());
System.out.println(emp2.getName() + " - " + emp2.getDepartment() + " - 工资:" + emp2.getSalary());
System.out.println("\n=== 涨薪后 ===");
emp1.raiseSalary(10);
System.out.println(emp1.getName() + "涨薪后工资:" + emp1.getSalary());
System.out.println("\n=== 修改部门 ===");
emp2.setDepartment("市场部");
System.out.println(emp2.getName() + "新部门:" + emp2.getDepartment());
System.out.println("\n=== 测试非法工资 ===");
emp2.setSalary(1500);
}
}

12
w4/Circle.java

@ -0,0 +1,12 @@
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}

11
w4/Main.java

@ -0,0 +1,11 @@
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
Shape triangle = new Triangle(3, 4, 5);
ShapeUtil.printArea(circle);
ShapeUtil.printArea(rectangle);
ShapeUtil.printArea(triangle);
}
}

13
w4/Rectangle.java

@ -0,0 +1,13 @@
public class Rectangle extends Shape {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}

3
w4/Shape.java

@ -0,0 +1,3 @@
public abstract class Shape {
public abstract double getArea();
}

5
w4/ShapeUtil.java

@ -0,0 +1,5 @@
public class ShapeUtil {
public static void printArea(Shape shape) {
System.out.println("面积为: " + shape.getArea());
}
}

15
w4/Triangle.java

@ -0,0 +1,15 @@
public class Triangle extends Shape {
private double a, b, c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double getArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
}

6
w5/A_Circlew5.java

@ -0,0 +1,6 @@
public class A_Circlew5 extends A_Graphic {
@Override
public void draw() {
System.out.println("绘制一个圆形");
}
}

5
w5/A_Graphic.java

@ -0,0 +1,5 @@
public class A_Graphic {
public void draw() {
System.out.println("绘制一个图形");
}
}

15
w5/A_Main1.java

@ -0,0 +1,15 @@
public class A_Main1 {public static void drawGraphic(A_Graphic g) {
g.draw();
}
public static void main(String[] args) {
A_Graphic graphic = new A_Graphic();
A_Circlew5 circle = new A_Circlew5();
A_Rectanglew5 rectangle = new A_Rectanglew5();
drawGraphic(graphic);
drawGraphic(circle);
drawGraphic(rectangle);
}
}

6
w5/A_Rectanglew5.java

@ -0,0 +1,6 @@
public class A_Rectanglew5 extends A_Graphic {
@Override
public void draw() {
System.out.println("绘制一个矩形");
}
}

6
w5/Bike.java

@ -0,0 +1,6 @@
public class Bike extends Vehicle {
@Override
public void run() {
System.out.println("自行车在自行车道上骑行");
}
}

6
w5/Car.java

@ -0,0 +1,6 @@
public class Car extends Vehicle {
@Override
public void run() {
System.out.println("汽车在公路上行驶");
}
}

12
w5/Main2.java

@ -0,0 +1,12 @@
public class Main2 {
public static void main(String[] args) {
Vehicle[] vehicles = new Vehicle[3];
vehicles[0] = new Car();
vehicles[1] = new Bike();
vehicles[2] = new Truck();
for (Vehicle v : vehicles) {
v.run();
}
}
}

6
w5/Truck.java

@ -0,0 +1,6 @@
public class Truck extends Vehicle {
@Override
public void run() {
System.out.println("卡车在高速上运输货物");
}
}

3
w5/Vehicle.java

@ -0,0 +1,3 @@
public abstract class Vehicle {
public abstract void run();
}

32
w6/Animal.java

@ -0,0 +1,32 @@
// 抽象类 Animal
abstract class Animal {
public abstract void makeSound();
}
// 接口 Swimmable
interface Swimmable {
void swim();
}
// Dog 类继承 Animal 并实现 Swimmable
class Dog extends Animal implements Swimmable {
@Override
public void makeSound() {
System.out.println("狗叫:汪汪汪!");
}
@Override
public void swim() {
System.out.println("狗会游泳,正在狗刨...");
}
}
// Cat 类继承 Animal,不实现 Swimmable
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("猫叫:喵喵喵~");
}
}

21
w6/AnimalTest.java

@ -0,0 +1,21 @@
// 测试类
public class AnimalTest {
public static void main(String[] args) {
// 多态调用
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound();
cat.makeSound();
// 判断 Dog 是否实现了 Swimmable 接口并调用
if (dog instanceof Swimmable) {
((Swimmable) dog).swim();
}
// Cat 不能游泳
if (!(cat instanceof Swimmable)) {
System.out.println("猫不会游泳");
}
}
}

BIN
w7/20.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 511 KiB

BIN
w7/35.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

71
w7/ScoreCalculator.java

@ -0,0 +1,71 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ScoreCalculator {
public static void main(String[] args) {
String fileName = "java/w7/ scores";
double average = calculateAverageScore(fileName);
if (!Double.isNaN(average)) {
System.out.printf("平均分: %.2f%n", average);
}
}
/**
* 计算成绩文件的平均分
* @param fileName 文件名
* @return 平均分如果出错返回 NaN
*/
public static double calculateAverageScore(String fileName) {
int sum = 0;
int count = 0;
// 1. 检查文件是否存在
Path filePath = Paths.get(fileName);
if (!Files.exists(filePath)) {
System.err.println("错误: 文件 '" + fileName + "' 不存在!");
return Double.NaN;
}
// 2. 使用 try-with-resources 确保流自动关闭
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
// 跳过空行
if (line.trim().isEmpty()) {
continue;
}
try {
int score = Integer.parseInt(line.trim());
sum += score;
count++;
} catch (NumberFormatException e) {
// 数字格式错误,记录错误但继续处理其他行
System.err.println("警告: 无效的数据格式 '" + line + "',已跳过此条记录");
}
}
// 检查是否有有效数据
if (count == 0) {
System.err.println("错误: 文件中没有有效的成绩数据!");
return Double.NaN;
}
return (double) sum / count;
} catch (IOException e) {
// 读取错误
System.err.println("错误: 读取文件 '" + fileName + "' 时发生异常!");
System.err.println("详细信息: " + e.getMessage());
return Double.NaN;
}
// 无需显式 close(),try-with-resources 会自动处理
}
}

5
w7/scores

@ -0,0 +1,5 @@
85
90
78
92
88

5
w8/AI

@ -0,0 +1,5 @@
3.AI协同学习:泛型擦除后如何通过反射获取泛型信息?
答案:擦除后,运行时拿不到类型参数k、V的实际值。但可以通过反射获取字段、方法参数/返回值上声明的泛型信息(如List<String>中的String)
4.思考题:为什么Java泛型不支持基本类型?
根本原因:Java泛型通过类型擦除实现,编译后所有泛型参数都被替换为Object(或上界类型)。而基本类型(int、double等)不继承自 object,无法直接放入0bject容器中。
解法:使用对应的包装类(Integer Double).依赖自动装箱/拆箱

26
w8/Cache.java

@ -0,0 +1,26 @@
import java.util.HashMap;
import java.util.Map;
public class Cache<K, V> {
private final Map<K, V> cache = new HashMap<>();
public void put(K key, V value) {
cache.put(key, value);
}
public V get(K key) {
return cache.get(key);
}
public boolean containsKey(K key) {
return cache.containsKey(key);
}
public int size() {
return cache.size();
}
public void clear() {
cache.clear();
}
}

23
w8/Pair.java

@ -0,0 +1,23 @@
public class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
// 交换后 key/value 类型可能变化,所以返回新的 Pair<V, K>
public Pair<V, K> swap() {
return new Pair<>(this.value, this.key);
}
public static void main(String[] args) {
Pair<String, Integer> p = new Pair<>("Age", 25);
Pair<Integer, String> swapped = p.swap();
System.out.println(swapped.getKey() + "=" + swapped.getValue()); // 25=Age
}
}

14
w9/AI

@ -0,0 +1,14 @@
3. AI 架构审计(MVC 三层检查)
发送给 AI 的指令模板:
作为 Java 架构审计师,请检查我的 MVC 三层划分是否存在越权行为?
我的项目结构:
· Model:Article(数据实体)、Storage(数据存取)
· View:ConsoleView(控制台输入输出)
· Controller:CommandController(解析命令、调用 Model)
请从以下角度审计:
1. View 是否直接调用了 Model 层?(越权)
2. Controller 是否做了 View 的工作?(职责混淆)
3. Model 层是否包含了业务逻辑而非仅仅是数据存取?
4. 各层之间的依赖方向是否正确?(View → Controller → Model)

27
w9/Article.java

@ -0,0 +1,27 @@
import java.util.Date;
public class Article {
private String title;
private String content;
private String url;
private String author; // 新增
private Date publishDate; // 新增
// 构造器
public Article(String title, String content, String url, String author, Date publishDate) {
this.title = title;
this.content = content;
this.url = url;
this.author = author;
this.publishDate = publishDate;
}
// Getters and Setters
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public Date getPublishDate() { return publishDate; }
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; }
// 原有字段的 getter/setter...
}

8
w9/Controller.java

@ -0,0 +1,8 @@
public class Controller {
private HistoryCommand history = new HistoryCommand();
public void executeCommand(String command) {
history.addCommand(command); // 记录每一条命令
// ... 原有命令处理逻辑
}
}

28
w9/HistoryCommand.java

@ -0,0 +1,28 @@
import java.util.ArrayList;
import java.util.List;
public class HistoryCommand {
private List<String> commandHistory = new ArrayList<>();
// 添加命令到历史
public void addCommand(String command) {
commandHistory.add(command);
}
// 获取所有历史命令
public List<String> getHistory() {
return new ArrayList<>(commandHistory); // 返回副本,保护原数据
}
// 打印历史记录
public void printHistory() {
for (int i = 0; i < commandHistory.size(); i++) {
System.out.println((i + 1) + ". " + commandHistory.get(i));
}
}
// 清空历史
public void clear() {
commandHistory.clear();
}
}
Loading…
Cancel
Save