diff --git a/HelloWorld/HelloWorld.java b/HelloWorld/HelloWorld.java new file mode 100644 index 0000000..d558cad --- /dev/null +++ b/HelloWorld/HelloWorld.java @@ -0,0 +1,6 @@ +public class HelloWorld { + public static void main(String[] +args){ + System.out.println("Hello,World!"); + } +} \ No newline at end of file diff --git a/w1/AI协助记录 b/w1/AI协助记录 new file mode 100644 index 0000000..def93e0 --- /dev/null +++ b/w1/AI协助记录 @@ -0,0 +1,15 @@ +AI协助记录 + +**遇到的问题:** +1. Git推送时出现“rejected”错误,远程有更新导致推送失败 +2. Java中文注释编译报错(编码GBK不可映射字符) +3. 不清楚作业文件如何整理 + +**AI提供的帮助:** +- 指导先`git pull`再`git push`的正确流程 +- 建议用UTF-8编码保存文件,编译时加`-encoding UTF-8` +- 指导创建`w1-姓名-学号`文件夹整理作业文件 +- 提供TemperatureConverter.java完整代码 + +**总结:** +通过本次作业,学会了Git规范操作和Java编码处理,顺利完成温度转换器程序。 \ No newline at end of file diff --git a/w1/README.md b/w1/README.md new file mode 100644 index 0000000..55c6ef7 --- /dev/null +++ b/w1/README.md @@ -0,0 +1,26 @@ +# \# 温度转换工具 (TemperatureConverter) + +# + +# 一个简单的 Java 命令行程序,支持摄氏度(C)和华氏度(F)之间的相互转换。 + +# + +# \## 功能说明 + +# \- 输入带单位的温度值(如 36.6 C 或 97 F),程序会自动完成温度单位转换 + +# \- 摄氏度转华氏度公式:F = C × 9/5 + 32 + +# \- 华氏度转摄氏度公式:C = (F - 32) × 5/9 + +# + +# \## 编译与运行 + +# ```bash + +# javac TemperatureConverter.java + +# java TemperatureConverter + diff --git a/w1/TemperatureConverter.java b/w1/TemperatureConverter.java new file mode 100644 index 0000000..7820cc0 --- /dev/null +++ b/w1/TemperatureConverter.java @@ -0,0 +1,54 @@ +import java.util.Scanner; +/** + * 温度转换器 + * 功能:支持摄氏度(C)与华氏度(F)之间的相互转换 + */ +public class TemperatureConverter { + /** + * 摄氏度转换为华氏度 + * @param celsius 摄氏温度值 + * @return 华氏温度值 + */ + public static double celsiusToFahrenheit(double celsius) { + return celsius * 9.0 / 5.0 + 32.0; + } + /** + * 华氏度转换为摄氏度 + * @param fahrenheit 华氏温度值 + * @return 摄氏温度值 + */ + public static double fahrenheitToCelsius(double fahrenheit) { + return (fahrenheit - 32.0) * 5.0 / 9.0; + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("===== 温度转换器 ====="); + System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); + String input = scanner.nextLine().trim(); + if (input.isEmpty()) { + System.out.println("输入为空,程序退出。"); + scanner.close(); + return; + } + String[] parts = input.split(" "); + try { + double value = Double.parseDouble(parts[0]); + String unit = "C"; + if (parts.length >= 2) { + unit = parts[1].toUpperCase(); + } + if (unit.startsWith("C")) { + double f = celsiusToFahrenheit(value); + System.out.printf("%.1f °C = %.2f °F%n", value, f); + } else if (unit.startsWith("F")) { + double c = fahrenheitToCelsius(value); + System.out.printf("%.1f °F = %.2f °C%n", value, c); + } else { + System.out.println("未知单位,请使用 C 或 F。"); + } + } catch (Exception e) { + System.out.println("输入解析失败,请按示例输入:36.6 C"); + } + scanner.close(); + } +} \ No newline at end of file diff --git a/w1/程序运行截图/联想截图_20260308230209.png b/w1/程序运行截图/联想截图_20260308230209.png new file mode 100644 index 0000000..66d3889 Binary files /dev/null and b/w1/程序运行截图/联想截图_20260308230209.png differ diff --git a/w2/DataCleaner.java b/w2/DataCleaner.java new file mode 100644 index 0000000..451baf1 --- /dev/null +++ b/w2/DataCleaner.java @@ -0,0 +1,35 @@ +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 i = 0;i < sensorData.length;i++){ + int num =sensorData[i]; + + if (num == 999) { + System.out.println("传感器断开连接,数据处理终止"); + break; + } + if (num <= 0 || num > 100) { + System.out.println("警告:发现越界数据!数值超过100。"); + continue; + } + + validSum = validSum + num; // 累加总和 + validCount = validCount + 1; // 计数加1 + System.out.println("有效数据:" + num + ",当前总和:" + validSum); + } + + System.out.println("\n===== 最终结果 ====="); + if (validCount > 0) { + double average = (double) validSum / validCount; // 计算平均值 + System.out.println("有效数据个数:" + validCount); + System.out.println("有效数据总和:" + validSum); + System.out.println("平均值:" + average); + } else { + System.out.println("无有效数据"); + } + } +} \ No newline at end of file diff --git a/w2/联想截图_20260316144831.png b/w2/联想截图_20260316144831.png new file mode 100644 index 0000000..53887ef Binary files /dev/null and b/w2/联想截图_20260316144831.png differ diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..531c763 --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,15 @@ +package ShapeAreaCalculator; + +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..6f8f78e --- /dev/null +++ b/w4/Main.java @@ -0,0 +1,13 @@ +package ShapeAreaCalculator; + +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); + + 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..96b3acc --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,16 @@ +package ShapeAreaCalculator; + +public class Rectangle extends Shape { + private double width; + private double 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..d72d164 --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,5 @@ +package ShapeAreaCalculator; + +public abstract class Shape { + public abstract double getArea(); +} diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..e96f107 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,7 @@ +package ShapeAreaCalculator; + +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("图形面积: " + shape.getArea()); + } +} diff --git a/w4/Triangle.java b/w4/Triangle.java new file mode 100644 index 0000000..8b571ad --- /dev/null +++ b/w4/Triangle.java @@ -0,0 +1,16 @@ +package ShapeAreaCalculator; + +public class Triangle extends Shape { + private double base; + private double height; + + public Triangle(double base, double height) { + this.base = base; + this.height = height; + } + + @Override + public double getArea() { + return 0.5 * base * height; + } +} diff --git a/w4/实验报告.md b/w4/实验报告.md new file mode 100644 index 0000000..058d10c --- /dev/null +++ b/w4/实验报告.md @@ -0,0 +1,24 @@ +一、实验目的 +1. 掌握抽象类的定义与使用 +2. 理解继承和多态的应用 +3. 学会统一处理不同图形对象 + +## 二、类图 +![类图](c:\Users\65452\Desktop\ketang\out\ShapeAreaCalculator\类图\类图.png) + +三、AI使用记录 +代码结构设计 + +类图生成 + +报告撰写 + +四、组合 vs 继承 +继承:子类继承父类,代码复用简单,但耦合度高 + +组合:类包含其他类对象,灵活低耦合,但代码量多 + +本实验:图形是"is-a"关系,适合用继承 + +五、实验总结 +掌握了抽象类、继承和多态的应用,能够统一处理不同类型的图形对象。 \ No newline at end of file diff --git a/w4/类图 b/w4/类图 new file mode 100644 index 0000000..e69de29 diff --git a/w4/类图.puml b/w4/类图.puml new file mode 100644 index 0000000..4108af8 --- /dev/null +++ b/w4/类图.puml @@ -0,0 +1,31 @@ +@startuml +abstract class Shape { + {abstract} + getArea(): double +} + +class Circle { + - radius: double + + getArea(): double +} + +class Rectangle { + - width: double + - height: double + + getArea(): double +} + +class Triangle { + - base: double + - height: double + + getArea(): double +} + +class ShapeUtil { + + printArea(Shape): void +} + +Shape <|-- Circle +Shape <|-- Rectangle +Shape <|-- Triangle +ShapeUtil ..> Shape +@enduml \ No newline at end of file