Browse Source

Merge remote-tracking branch 'origin/main'

main
zhuyanshuo 2 weeks ago
parent
commit
b2ca3b8392
  1. BIN
      out/production/Java/Car.class
  2. BIN
      out/production/Java/Testcar.class
  3. 3
      w1/AI协助记录
  4. 10
      w1/Helloworld.java
  5. 2
      w1/README.md
  6. 59
      w1/TemperatureConverter.java
  7. 2
      w1/sss.java
  8. 35
      w2/DataCleaner.java
  9. 5
      w2/实验报告
  10. 117
      w3/Car.java
  11. 34
      w3/Testcar.java
  12. BIN
      w3/w3实验报告.docx
  13. BIN
      w4/56659ce40041862627f0ca124de1db47.png
  14. BIN
      w4/743570fdfbd74c9de110a474949723bd.png
  15. 5
      w4/AI协助记录
  16. 13
      w4/Main.java
  17. 61
      w4/Shape.java
  18. BIN
      w4/be557904d4ec497ad578cc01aa266417.png
  19. BIN
      w4/dbe59697c52434ebfb81ec2eec1f4295.png
  20. 0
      w4/实验报告
  21. 18
      w4/组合vs继承

BIN
out/production/Java/Car.class

Binary file not shown.

BIN
out/production/Java/Testcar.class

Binary file not shown.

3
w1/AI协助记录

@ -0,0 +1,3 @@
- 协助完成 Java 代码结构设计,实现摄氏度/华氏度转换核心算法,并补充完整中文注释。
- 提供命令行参数处理方案,支持 `java TemperatureConverter 36.6 C` 格式的直接调用。
- 生成 README.md 模板,包含编译/运行命令、输出示例及作业提交说明。

10
w1/Helloworld.java

@ -0,0 +1,10 @@
public class Helloworld {/**
* 简单 HelloWorld 程序用于验证 Java 环境
* @author [你的姓名]
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java World!");
}
}
}

2
w1/README.md

@ -0,0 +1,2 @@
$ java TemperatureConverter 36.6 C
命令行转换: 36.6 ℃ = 97.88 ℉

59
w1/TemperatureConverter.java

@ -0,0 +1,59 @@
/**
* 温度转换工具类
* 实现摄氏度与华氏度的双向转换并提供测试用例
* @author [你的姓名]
* @version 1.0
*/
public class TemperatureConverter {
/**
* 摄氏度转华氏度
* 公式: F = C × 9/5 + 32
* @param celsius 摄氏温度值
* @return 对应的华氏温度值
*/
public static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32.0;
}
/**
* 华氏度转摄氏度
* 公式: C = (F - 32) × 5/9
* @param fahrenheit 华氏温度值
* @return 对应的摄氏温度值
*/
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;
}
/**
* 主方法程序入口包含测试用例
* @param args 命令行参数可选支持直接传入温度值和单位进行转换
*/
public static void main(String[] args) {
// 示例测试用例
double testC = 25.0;
double testF = 77.0;
System.out.println("=== 温度转换测试 ===");
System.out.printf("%.1f ℃ 转换为华氏度: %.1f ℉%n", testC, celsiusToFahrenheit(testC));
System.out.printf("%.1f ℉ 转换为摄氏度: %.1f ℃%n", testF, fahrenheitToCelsius(testF));
// 支持命令行参数模式(加分项)
if (args.length == 2) {
try {
double temp = Double.parseDouble(args[0]);
String unit = args[1].toUpperCase();
if (unit.equals("C")) {
System.out.printf("命令行转换: %.1f ℃ = %.1f ℉%n", temp, celsiusToFahrenheit(temp));
} else if (unit.equals("F")) {
System.out.printf("命令行转换: %.1f ℉ = %.1f ℃%n", temp, fahrenheitToCelsius(temp));
} else {
System.out.println("单位错误,请使用 C 或 F");
}
} catch (NumberFormatException e) {
System.out.println("温度值格式错误,请输入数字");
}
}
}
}

2
w1/sss.java

@ -1,2 +0,0 @@
public class sss {
}

35
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 data : sensorData) {
// 3. 致命错误:遇到 999,终止整个流程
if (data == 999) {
System.out.println("致命错误: 传感器掉线, 终止处理。");
break;
}
// 2. 无效数据:0/负数 或 >100 且不是999
if (data <= 0 || data > 100) {
System.out.printf("警告: 发现越界数据[%d], 已跳过。%n", data);
continue;
}
// 1. 正常范围:1~100(包含),计入统计
validSum += data;
validCount++;
}
// 4. 最终输出:计算并打印平均值
if (validCount > 0) {
// 注意整数除法陷阱,转成 double 计算
double average = (double) validSum / validCount;
System.out.printf("有效数据平均值: %.2f%n", average);
} else {
System.out.println("无有效数据。");
}
}
}

5
w2/实验报告

@ -0,0 +1,5 @@
警告: 发现越界数据[-5], 已跳过。
警告: 发现越界数据[0], 已跳过。
警告: 发现越界数据[105], 已跳过。
致命错误: 传感器掉线, 终止处理。
有效数据平均值: 88.50

117
w3/Car.java

@ -0,0 +1,117 @@
public class Car {
// 1. 私有属性
private final String licensePlate; // 车牌号(不可变)
private String brand; // 品牌
private String model; // 型号
private double dailyRent; // 日租金
private boolean isRented; // 是否已出租
// 静态成员(加分项):统计车辆总数
private static int totalCars = 0;
// 2. 全参构造方法
public Car(String licensePlate, String brand, String model, double dailyRent) {
this.licensePlate = licensePlate;
this.brand = brand;
this.model = model;
this.dailyRent = dailyRent;
this.isRented = false; // 初始状态:未出租
totalCars++; // 每创建一个对象,总数+1
}
// 重载构造方法:只接收车牌号、品牌、型号,日租金默认300
public Car(String licensePlate, String brand, String model) {
this(licensePlate, brand, model, 300.0); // 调用全参构造
}
// 3. Getter/Setter 方法
public String getLicensePlate() {
return licensePlate;
}
// 无setLicensePlate,保证车牌号不可修改
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getDailyRent() {
return dailyRent;
}
public void setDailyRent(double dailyRent) {
// 数据校验:新租金必须大于0
if (dailyRent > 0) {
this.dailyRent = dailyRent;
} else {
System.out.println("错误:日租金必须大于0,修改无效。");
}
}
public boolean isRented() {
return isRented;
}
// 无setIsRented,状态必须通过业务方法改变
// 静态方法:获取总车辆数
public static int getTotalCars() {
return totalCars;
}
// 4. 业务方法
/**
* 租车将车辆状态置为已出租
*/
public void rentCar() {
if (isRented) {
System.out.println("车辆已租出,无法再次租用。");
} else {
isRented = true;
System.out.println("租车成功:" + licensePlate);
}
}
/**
* 还车将车辆状态置为未出租
*/
public void returnCar() {
if (!isRented) {
System.out.println("车辆未被租用,无需归还。");
} else {
isRented = false;
System.out.println("还车成功:" + licensePlate);
}
}
/**
* 计算总租金
* @param days 租用天数假设>0不做校验
* @return 总租金
*/
public double calculateRent(int days) {
return dailyRent * days;
}
// 辅助方法:显示车辆信息
public void displayInfo() {
System.out.println("=== 车辆信息 ===");
System.out.println("车牌号:" + licensePlate);
System.out.println("品牌:" + brand);
System.out.println("型号:" + model);
System.out.println("日租金:" + dailyRent + "元/天");
System.out.println("状态:" + (isRented ? "已出租" : "可租"));
System.out.println();
}
}

34
w3/Testcar.java

@ -0,0 +1,34 @@
public class Testcar { public static void main(String[] args) {
// 1. 创建3个Car对象(使用不同构造方法)
Car car1 = new Car("京A12345", "丰田", "凯美瑞", 280.0);
Car car2 = new Car("沪B67890", "本田", "雅阁"); // 默认日租金300
Car car3 = new Car("粤C24680", "大众", "帕萨特", 320.0);
// 2. 输出所有车辆信息
System.out.println("===== 初始车辆信息 =====");
car1.displayInfo();
car2.displayInfo();
car3.displayInfo();
// 3. 测试租车/还车业务
System.out.println("===== 测试租车/还车 =====");
car1.rentCar(); // 正常租车
car1.rentCar(); // 重复租车(提示错误)
car1.returnCar(); // 正常还车
car1.returnCar(); // 重复还车(提示错误)
// 4. 计算租金(5天)
System.out.println("===== 计算租金 =====");
double rent = car1.calculateRent(5);
System.out.printf("京A12345 租用5天,总租金:%.2f元%n%n", rent);
// 5. 测试非法日租金修改
System.out.println("===== 测试非法租金修改 =====");
car1.setDailyRent(-100); // 输入非法值,提示错误
System.out.println("修改后日租金:" + car1.getDailyRent() + "元/天(保持原值)%n");
// 6. 输出总车辆数(静态成员)
System.out.println("===== 车辆总数 =====");
System.out.println("当前系统共有车辆:" + Car.getTotalCars() + "辆");
}
}

BIN
w3/w3实验报告.docx

Binary file not shown.

BIN
w4/56659ce40041862627f0ca124de1db47.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 KiB

BIN
w4/743570fdfbd74c9de110a474949723bd.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 KiB

5
w4/AI协助记录

@ -0,0 +1,5 @@
AI 协助明确本次 Java 编程任务核心要求,即通过抽象类、继承与多态实现图形面积统一计算与输出。
AI 提供完整可运行代码,包括抽象类Shape、Circle/Rectangle/Triangle子类、工具类ShapeUtil及测试类,规范代码结构。
AI 补充空值判断、格式化输出等细节,提升代码健壮性与可读性。
AI 协助生成实验报告框架、类图、组合与继承对比分析内容。
AI 对面向对象相关知识点进行讲解,帮助理解多态、继承等核心原理。

13
w4/Main.java

@ -0,0 +1,13 @@
public class Main {
public static void main(String[] args) {
// 创建图形对象
Shape circle = new Circle(6);
Shape rectangle = new Rectangle(7, 9);
Shape triangle = new Triangle(7, 10);
// 统一计算并打印面积
ShapeUtil.printArea(circle);
ShapeUtil.printArea(rectangle);
ShapeUtil.printArea(triangle);
}
}

61
w4/Shape.java

@ -0,0 +1,61 @@
abstract class Shape {//定义一个抽象类
public abstract double getArea();//强制所有图形都必须能计算面积
}
// 圆形
class Circle extends Shape {//继承shape,必须实现getarea
private double radius;//半径私有属性
public Circle(double radius) {
this.radius = radius;
}//构造方法无返回值,给对象赋初始值
@Override//重写父类方法
public double getArea() {
return Math.PI * radius * radius;
}
}
// 矩形
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
}
// 三角形
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;
}
}
// 工具类
class ShapeUtil {//专门用来打印图形面积
public static void printArea(Shape shape) {//任意接受一个图形
System.out.println("图形面积:" + shape.getArea());
}
}

BIN
w4/be557904d4ec497ad578cc01aa266417.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

BIN
w4/dbe59697c52434ebfb81ec2eec1f4295.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

0
w4/实验报告

18
w4/组合vs继承

@ -0,0 +1,18 @@
继承(本次实验使用方式)
优点:
代码复用:子类自动继承父类的抽象方法约束,统一接口。
多态支持:父类引用指向子类对象,实现统一处理(如 ShapeUtil.printArea)。
结构清晰:符合 “is-a” 关系(圆形是一种图形、矩形是一种图形)。
缺点:
耦合度高:子类与父类强绑定,父类修改会影响所有子类。
无法多继承:Java 中一个类只能继承一个父类,扩展性受限。
破坏封装:子类可能过度依赖父类的内部实现。
2. 组合
定义:通过 “包含” 其他类的实例来实现功能,符合 “has-a” 关系(如图形包含颜色、位置等属性类)。
优点:
低耦合:类之间通过接口交互,修改内部实现不影响调用方。
灵活性高:可动态组合多个对象,实现更复杂的功能。
符合 “合成复用原则”:优先使用组合而非继承。
缺点:
代码量稍多:需要手动创建对象引用并委托调用。
不直接支持多态的 “is-a” 语义表达。
Loading…
Cancel
Save