diff --git a/README.md b/README.md deleted file mode 100644 index a8687f1..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# java - diff --git a/W1-白慧娟-202506050330/AI_help.txt b/W1-白慧娟-202506050330/AI_help.txt new file mode 100644 index 0000000..92fb32f --- /dev/null +++ b/W1-白慧娟-202506050330/AI_help.txt @@ -0,0 +1,7 @@ +AI协助记录: + +在完成这次Java作业过程中,我使用了AI助手。首先,我请AI将Python温度转换程序改写为Java版本,AI提供了完整的代码和中文注释。 + +在编译运行过程中遇到输入格式错误的问题,AI一步步指导我修改代码,采用分步输入温度和单位的方式,最终程序成功运行,可以正确转换摄氏度和华氏度。 + +AI还帮我创建了README.md说明文档和output.txt运行结果文件,指导我完成所有作业要求。 \ No newline at end of file diff --git a/W1-白慧娟-202506050330/README.md b/W1-白慧娟-202506050330/README.md new file mode 100644 index 0000000..ce3e66e --- /dev/null +++ b/W1-白慧娟-202506050330/README.md @@ -0,0 +1,22 @@ +\# 温度转换程序 + + + +\## 编译方法 + +javac TemperatureConverter.java + + + +\## 运行方法 + +java TemperatureConverter + + + +\## 运行示例 + +输入:36.6 C + +输出:36.6℃ = 97.88℉ + diff --git a/W1-白慧娟-202506050330/TemperatureConverter.java b/W1-白慧娟-202506050330/TemperatureConverter.java new file mode 100644 index 0000000..ffa3fa7 --- /dev/null +++ b/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(); + } +} \ No newline at end of file diff --git a/W1-白慧娟-202506050330/output.txt b/W1-白慧娟-202506050330/output.txt new file mode 100644 index 0000000..acbfbd3 --- /dev/null +++ b/W1-白慧娟-202506050330/output.txt @@ -0,0 +1,7 @@ +AI协助记录: + +本次作业使用AI助手将Python温度转换程序改写为Java版本。AI提供了完整的Java代码和中文注释。 + +在运行过程中遇到输入格式问题,AI指导修改代码,采用分步输入的方式,解决了特殊符号解析问题。程序最终成功运行,能正确转换摄氏度和华氏度。 + +AI还协助创建了README.md说明文档和output.txt运行结果文件,帮助完成所有作业要求。 \ No newline at end of file diff --git a/student/StudentTest.java b/student/StudentTest.java new file mode 100644 index 0000000..d0b98c1 --- /dev/null +++ b/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 + " 正在学习"); + } + +} diff --git a/w1/HelloWorld.java b/w1/HelloWorld.java index e0a3456..3479e71 100644 --- a/w1/HelloWorld.java +++ b/w1/HelloWorld.java @@ -3,4 +3,3 @@ public class HelloWorld { System.out.println("Hello,World!"); } } - \ No newline at end of file diff --git a/w10/AI b/w10/AI new file mode 100644 index 0000000..e69de29 diff --git a/w10/AnalyzeCommand.java b/w10/AnalyzeCommand.java new file mode 100644 index 0000000..8064112 --- /dev/null +++ b/w10/AnalyzeCommand.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class AnalyzeCommand { +} diff --git a/w10/article.java b/w10/article.java new file mode 100644 index 0000000..6fe5fe9 --- /dev/null +++ b/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... +} \ No newline at end of file diff --git a/w10/articlerepository.java b/w10/articlerepository.java new file mode 100644 index 0000000..d14d42c --- /dev/null +++ b/w10/articlerepository.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class articlerepository { +} diff --git a/w11/ArticleA.java b/w11/ArticleA.java new file mode 100644 index 0000000..0042984 --- /dev/null +++ b/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... +} \ No newline at end of file diff --git a/w11/ArticleRepository.java b/w11/ArticleRepository.java new file mode 100644 index 0000000..6c379dc --- /dev/null +++ b/w11/ArticleRepository.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class ArticleRepository { +} diff --git a/w11/BlogStrategy.java b/w11/BlogStrategy.java new file mode 100644 index 0000000..37c8963 --- /dev/null +++ b/w11/BlogStrategy.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class BlogStrategy { +} diff --git a/w11/Command.java b/w11/Command.java new file mode 100644 index 0000000..e8cf8ac --- /dev/null +++ b/w11/Command.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class Command { +} diff --git a/w11/CrawlCommand.java b/w11/CrawlCommand.java new file mode 100644 index 0000000..40dde5e --- /dev/null +++ b/w11/CrawlCommand.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class CrawlCommand { +} diff --git a/w11/CrawlStrategy.java b/w11/CrawlStrategy.java new file mode 100644 index 0000000..092aa93 --- /dev/null +++ b/w11/CrawlStrategy.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class CrawlStrategy { +} diff --git a/w11/CrawlerException.java b/w11/CrawlerException.java new file mode 100644 index 0000000..bff0d88 --- /dev/null +++ b/w11/CrawlerException.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class CrawlerException { +} diff --git a/w11/HnuNewsStrategy.java b/w11/HnuNewsStrategy.java new file mode 100644 index 0000000..8bb5e3c --- /dev/null +++ b/w11/HnuNewsStrategy.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class HnuNewsStrategy { +} diff --git a/w11/Main11.java b/w11/Main11.java new file mode 100644 index 0000000..1e936b3 --- /dev/null +++ b/w11/Main11.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class Main { +} diff --git a/w11/NetworkException.java b/w11/NetworkException.java new file mode 100644 index 0000000..5baf7ca --- /dev/null +++ b/w11/NetworkException.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class NetworkException { +} diff --git a/w11/NewsStrategy.java b/w11/NewsStrategy.java new file mode 100644 index 0000000..0e9404d --- /dev/null +++ b/w11/NewsStrategy.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class NewsStrategy { +} diff --git a/w11/ParseException.java b/w11/ParseException.java new file mode 100644 index 0000000..fbbd6d4 --- /dev/null +++ b/w11/ParseException.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class ParseException { +} diff --git a/w11/logback b/w11/logback new file mode 100644 index 0000000..e69de29 diff --git a/w11/pom b/w11/pom new file mode 100644 index 0000000..e69de29 diff --git a/w2/DataCleaner.java b/w2/DataCleaner.java new file mode 100644 index 0000000..7deb17d --- /dev/null +++ b/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("无有效数据"); + } +} +} + diff --git a/w3/BankAccount.java b/w3/BankAccount.java new file mode 100644 index 0000000..a0cfc88 --- /dev/null +++ b/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; + } +} + + diff --git a/w3/Employee.java b/w3/Employee.java new file mode 100644 index 0000000..d511349 --- /dev/null +++ b/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); + } +} + + diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..b3a64bc --- /dev/null +++ b/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; + } +} diff --git a/w4/Main.java b/w4/Main.java new file mode 100644 index 0000000..65461fa --- /dev/null +++ b/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); + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..6cd9205 --- /dev/null +++ b/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; + } +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..ac3b8fb --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,3 @@ +public abstract class Shape { + public abstract double getArea(); +} diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..a5e9fac --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,5 @@ +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("面积为: " + shape.getArea()); + } +} \ No newline at end of file diff --git a/w4/Triangle.java b/w4/Triangle.java new file mode 100644 index 0000000..c1311c4 --- /dev/null +++ b/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)); + } +} diff --git a/w5/A_Circlew5.java b/w5/A_Circlew5.java new file mode 100644 index 0000000..7d31409 --- /dev/null +++ b/w5/A_Circlew5.java @@ -0,0 +1,6 @@ +public class A_Circlew5 extends A_Graphic { + @Override + public void draw() { + System.out.println("绘制一个圆形"); + } +} \ No newline at end of file diff --git a/w5/A_Graphic.java b/w5/A_Graphic.java new file mode 100644 index 0000000..bb84016 --- /dev/null +++ b/w5/A_Graphic.java @@ -0,0 +1,5 @@ +public class A_Graphic { + public void draw() { + System.out.println("绘制一个图形"); + } +} diff --git a/w5/A_Main1.java b/w5/A_Main1.java new file mode 100644 index 0000000..a4c812b --- /dev/null +++ b/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); + } +} + diff --git a/w5/A_Rectanglew5.java b/w5/A_Rectanglew5.java new file mode 100644 index 0000000..f150a2d --- /dev/null +++ b/w5/A_Rectanglew5.java @@ -0,0 +1,6 @@ +public class A_Rectanglew5 extends A_Graphic { + @Override + public void draw() { + System.out.println("绘制一个矩形"); + } +} diff --git a/w5/Bike.java b/w5/Bike.java new file mode 100644 index 0000000..6cdc0bf --- /dev/null +++ b/w5/Bike.java @@ -0,0 +1,6 @@ +public class Bike extends Vehicle { + @Override + public void run() { + System.out.println("自行车在自行车道上骑行"); + } +} diff --git a/w5/Car.java b/w5/Car.java new file mode 100644 index 0000000..3888c0e --- /dev/null +++ b/w5/Car.java @@ -0,0 +1,6 @@ +public class Car extends Vehicle { + @Override + public void run() { + System.out.println("汽车在公路上行驶"); + } +} diff --git a/w5/Main2.java b/w5/Main2.java new file mode 100644 index 0000000..ca43736 --- /dev/null +++ b/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(); + } + } +} diff --git a/w5/Truck.java b/w5/Truck.java new file mode 100644 index 0000000..c60f669 --- /dev/null +++ b/w5/Truck.java @@ -0,0 +1,6 @@ +public class Truck extends Vehicle { + @Override + public void run() { + System.out.println("卡车在高速上运输货物"); + } +} diff --git a/w5/Vehicle.java b/w5/Vehicle.java new file mode 100644 index 0000000..ecef84f --- /dev/null +++ b/w5/Vehicle.java @@ -0,0 +1,3 @@ +public abstract class Vehicle { + public abstract void run(); +} diff --git a/w6/Animal.java b/w6/Animal.java new file mode 100644 index 0000000..5482c89 --- /dev/null +++ b/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("猫叫:喵喵喵~"); + } +} + + diff --git a/w6/AnimalTest.java b/w6/AnimalTest.java new file mode 100644 index 0000000..8619fc4 --- /dev/null +++ b/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("猫不会游泳"); + } + } +} diff --git a/w7/20.jpg b/w7/20.jpg new file mode 100644 index 0000000..51f4ba4 Binary files /dev/null and b/w7/20.jpg differ diff --git a/w7/35.jpg b/w7/35.jpg new file mode 100644 index 0000000..34b84d8 Binary files /dev/null and b/w7/35.jpg differ diff --git a/w7/ScoreCalculator.java b/w7/ScoreCalculator.java new file mode 100644 index 0000000..5155f79 --- /dev/null +++ b/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 会自动处理 + } +} \ No newline at end of file diff --git a/w7/scores b/w7/scores new file mode 100644 index 0000000..35d93ee --- /dev/null +++ b/w7/scores @@ -0,0 +1,5 @@ +85 +90 +78 +92 +88 \ No newline at end of file diff --git a/w8/AI b/w8/AI new file mode 100644 index 0000000..8efb943 --- /dev/null +++ b/w8/AI @@ -0,0 +1,5 @@ +3.AI协同学习:泛型擦除后如何通过反射获取泛型信息? +答案:擦除后,运行时拿不到类型参数k、V的实际值。但可以通过反射获取字段、方法参数/返回值上声明的泛型信息(如List中的String) +4.思考题:为什么Java泛型不支持基本类型? +根本原因:Java泛型通过类型擦除实现,编译后所有泛型参数都被替换为Object(或上界类型)。而基本类型(int、double等)不继承自 object,无法直接放入0bject容器中。 +解法:使用对应的包装类(Integer Double).依赖自动装箱/拆箱 \ No newline at end of file diff --git a/w8/Cache.java b/w8/Cache.java new file mode 100644 index 0000000..01b861c --- /dev/null +++ b/w8/Cache.java @@ -0,0 +1,26 @@ +import java.util.HashMap; +import java.util.Map; + +public class Cache { + private final Map 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(); + } +} \ No newline at end of file diff --git a/w8/Pair.java b/w8/Pair.java new file mode 100644 index 0000000..d572c9c --- /dev/null +++ b/w8/Pair.java @@ -0,0 +1,23 @@ +public class Pair { + 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 + public Pair swap() { + return new Pair<>(this.value, this.key); + } + + public static void main(String[] args) { + Pair p = new Pair<>("Age", 25); + Pair swapped = p.swap(); + System.out.println(swapped.getKey() + "=" + swapped.getValue()); // 25=Age + } +} \ No newline at end of file diff --git a/w9/AI b/w9/AI new file mode 100644 index 0000000..390d39a --- /dev/null +++ b/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) \ No newline at end of file diff --git a/w9/Article.java b/w9/Article.java new file mode 100644 index 0000000..6fe5fe9 --- /dev/null +++ b/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... +} \ No newline at end of file diff --git a/w9/Controller.java b/w9/Controller.java new file mode 100644 index 0000000..029bbc9 --- /dev/null +++ b/w9/Controller.java @@ -0,0 +1,8 @@ +public class Controller { + private HistoryCommand history = new HistoryCommand(); + + public void executeCommand(String command) { + history.addCommand(command); // 记录每一条命令 + // ... 原有命令处理逻辑 + } +} \ No newline at end of file diff --git a/w9/HistoryCommand.java b/w9/HistoryCommand.java new file mode 100644 index 0000000..2857540 --- /dev/null +++ b/w9/HistoryCommand.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; +import java.util.List; + +public class HistoryCommand { + private List commandHistory = new ArrayList<>(); + + // 添加命令到历史 + public void addCommand(String command) { + commandHistory.add(command); + } + + // 获取所有历史命令 + public List 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(); + } +} \ No newline at end of file