Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
11 changed files with 327 additions and 81 deletions
@ -0,0 +1,17 @@ |
|||||
|
# Maven构建产物 |
||||
|
target/ |
||||
|
|
||||
|
# IDE相关文件 |
||||
|
.idea/ |
||||
|
.vscode/ |
||||
|
|
||||
|
# 系统文件 |
||||
|
.DS_Store |
||||
|
Thumbs.db |
||||
|
|
||||
|
# 日志文件 |
||||
|
*.log |
||||
|
|
||||
|
# 临时文件 |
||||
|
*.tmp |
||||
|
*.temp |
||||
@ -1,2 +0,0 @@ |
|||||
# java |
|
||||
|
|
||||
@ -0,0 +1,53 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>com.example</groupId> |
||||
|
<artifactId>shape-calculator</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
|
||||
|
<name>图形面积计算器</name> |
||||
|
<description>图形面积计算器重构实验</description> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>17</maven.compiler.source> |
||||
|
<maven.compiler.target>17</maven.compiler.target> |
||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.junit.jupiter</groupId> |
||||
|
<artifactId>junit-jupiter-api</artifactId> |
||||
|
<version>5.9.1</version> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.junit.jupiter</groupId> |
||||
|
<artifactId>junit-jupiter-engine</artifactId> |
||||
|
<version>5.9.1</version> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||
|
<artifactId>maven-compiler-plugin</artifactId> |
||||
|
<version>3.10.1</version> |
||||
|
<configuration> |
||||
|
<source>17</source> |
||||
|
<target>17</target> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||
|
<artifactId>maven-surefire-plugin</artifactId> |
||||
|
<version>3.0.0-M7</version> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
</project> |
||||
@ -0,0 +1,50 @@ |
|||||
|
package com.example.shape; |
||||
|
|
||||
|
/** |
||||
|
* 圆形类 |
||||
|
* 继承自Shape抽象类,实现getArea()方法 |
||||
|
*/ |
||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
* @param radius 圆的半径 |
||||
|
*/ |
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 计算圆形面积 |
||||
|
* @return 圆形的面积 |
||||
|
*/ |
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取圆的半径 |
||||
|
* @return 圆的半径 |
||||
|
*/ |
||||
|
public double getRadius() { |
||||
|
return radius; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置圆的半径 |
||||
|
* @param radius 圆的半径 |
||||
|
*/ |
||||
|
public void setRadius(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 绘制圆形 |
||||
|
*/ |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个半径为 " + radius + " 的圆形"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
package com.example.shape; |
||||
|
|
||||
|
/** |
||||
|
* 矩形类 |
||||
|
* 继承自Shape抽象类,实现getArea()方法 |
||||
|
*/ |
||||
|
public class Rectangle extends Shape { |
||||
|
private double width; |
||||
|
private double height; |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
* @param width 矩形的宽度 |
||||
|
* @param height 矩形的高度 |
||||
|
*/ |
||||
|
public Rectangle(double width, double height) { |
||||
|
this.width = width; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 计算矩形面积 |
||||
|
* @return 矩形的面积 |
||||
|
*/ |
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return width * height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取矩形的宽度 |
||||
|
* @return 矩形的宽度 |
||||
|
*/ |
||||
|
public double getWidth() { |
||||
|
return width; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置矩形的宽度 |
||||
|
* @param width 矩形的宽度 |
||||
|
*/ |
||||
|
public void setWidth(double width) { |
||||
|
this.width = width; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取矩形的高度 |
||||
|
* @return 矩形的高度 |
||||
|
*/ |
||||
|
public double getHeight() { |
||||
|
return height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置矩形的高度 |
||||
|
* @param height 矩形的高度 |
||||
|
*/ |
||||
|
public void setHeight(double height) { |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 绘制矩形 |
||||
|
*/ |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个宽为 " + width + ",高为 " + height + " 的矩形"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.example.shape; |
||||
|
|
||||
|
/** |
||||
|
* 图形抽象类 |
||||
|
* 包含抽象方法getArea(),用于计算图形面积 |
||||
|
* 包含抽象方法draw(),用于绘制图形 |
||||
|
*/ |
||||
|
public abstract class Shape { |
||||
|
/** |
||||
|
* 计算图形面积 |
||||
|
* @return 图形的面积 |
||||
|
*/ |
||||
|
public abstract double getArea(); |
||||
|
|
||||
|
/** |
||||
|
* 绘制图形 |
||||
|
*/ |
||||
|
public abstract void draw(); |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.example.shape; |
||||
|
|
||||
|
/** |
||||
|
* 图形面积计算器测试类 |
||||
|
*/ |
||||
|
public class ShapeCalculatorTest { |
||||
|
public static void main(String[] args) { |
||||
|
// 创建圆形对象,半径为5
|
||||
|
Circle circle = new Circle(5); |
||||
|
System.out.println("圆形的半径: " + circle.getRadius()); |
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.ranShape(circle); |
||||
|
|
||||
|
// 创建矩形对象,宽为4,高为6
|
||||
|
Rectangle rectangle = new Rectangle(4, 6); |
||||
|
System.out.println("矩形的宽: " + rectangle.getWidth() + ", 高: " + rectangle.getHeight()); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.ranShape(rectangle); |
||||
|
|
||||
|
// 创建三角形对象,底为8,高为3
|
||||
|
Triangle triangle = new Triangle(8, 3); |
||||
|
System.out.println("三角形的底: " + triangle.getBase() + ", 高: " + triangle.getHeight()); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
ShapeUtil.ranShape(triangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
package com.example.shape; |
||||
|
|
||||
|
/** |
||||
|
* 图形工具类 |
||||
|
* 提供打印图形面积的方法和绘制图形的方法 |
||||
|
*/ |
||||
|
public class ShapeUtil { |
||||
|
/** |
||||
|
* 打印图形的面积 |
||||
|
* @param shape 图形对象 |
||||
|
*/ |
||||
|
public static void printArea(Shape shape) { |
||||
|
double area = shape.getArea(); |
||||
|
System.out.println("图形的面积为: " + area); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 绘制图形 |
||||
|
* @param s 图形对象 |
||||
|
*/ |
||||
|
public static void ranShape(Shape s) { |
||||
|
s.draw(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
package com.example.shape; |
||||
|
|
||||
|
/** |
||||
|
* 三角形类 |
||||
|
* 继承自Shape抽象类,实现getArea()方法 |
||||
|
*/ |
||||
|
public class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double height; |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
* @param base 三角形的底 |
||||
|
* @param height 三角形的高 |
||||
|
*/ |
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 计算三角形面积 |
||||
|
* @return 三角形的面积 |
||||
|
*/ |
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return base * height / 2; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取三角形的底 |
||||
|
* @return 三角形的底 |
||||
|
*/ |
||||
|
public double getBase() { |
||||
|
return base; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置三角形的底 |
||||
|
* @param base 三角形的底 |
||||
|
*/ |
||||
|
public void setBase(double base) { |
||||
|
this.base = base; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取三角形的高 |
||||
|
* @return 三角形的高 |
||||
|
*/ |
||||
|
public double getHeight() { |
||||
|
return height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置三角形的高 |
||||
|
* @param height 三角形的高 |
||||
|
*/ |
||||
|
public void setHeight(double height) { |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 绘制三角形 |
||||
|
*/ |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个底为 " + base + ",高为 " + height + " 的三角形"); |
||||
|
} |
||||
|
} |
||||
@ -1,5 +0,0 @@ |
|||||
public class HelloWorld { |
|
||||
public static void main(String[] args) { |
|
||||
System.out.println("Hello, World!"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,74 +0,0 @@ |
|||||
import java.util.Scanner; |
|
||||
|
|
||||
/** |
|
||||
* 温度转换器程序(Java版本) |
|
||||
* 支持摄氏度(C)与华氏度(F)之间互转 |
|
||||
* |
|
||||
* 对应Python原始程序的功能移植 |
|
||||
*/ |
|
||||
public class TemperatureConverter { |
|
||||
|
|
||||
/** |
|
||||
* 将摄氏度转换为华氏度 |
|
||||
* |
|
||||
* @param c 摄氏温度(浮点数) |
|
||||
* @return 对应的华氏温度(浮点数) |
|
||||
*/ |
|
||||
public static double celsiusToFahrenheit(double c) { |
|
||||
return c * 9.0 / 5.0 + 32.0; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 将华氏度转换为摄氏度 |
|
||||
* |
|
||||
* @param f 华氏温度(浮点数) |
|
||||
* @return 对应的摄氏温度(浮点数) |
|
||||
*/ |
|
||||
public static double fahrenheitToCelsius(double f) { |
|
||||
return (f - 32.0) * 5.0 / 9.0; |
|
||||
} |
|
||||
|
|
||||
public static void main(String[] args) { |
|
||||
Scanner scanner = new Scanner(System.in); |
|
||||
|
|
||||
// 提示用户输入,格式示例:“36.6 C”或“97 F”
|
|
||||
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("\\s+"); |
|
||||
|
|
||||
try { |
|
||||
// 允许用户输入两个部分:数值与单位
|
|
||||
double value = Double.parseDouble(parts[0]); |
|
||||
String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; |
|
||||
|
|
||||
// 判断单位并进行相应转换
|
|
||||
if (unit.startsWith("C")) { |
|
||||
// 从摄氏度转换为华氏度
|
|
||||
double f = celsiusToFahrenheit(value); |
|
||||
System.out.printf("%.2f °C = %.2f °F%n", value, f); |
|
||||
} else if (unit.startsWith("F")) { |
|
||||
// 从华氏度转换为摄氏度
|
|
||||
double c = fahrenheitToCelsius(value); |
|
||||
System.out.printf("%.2f °F = %.2f °C%n", value, c); |
|
||||
} else { |
|
||||
System.out.println("未知单位,请使用 C 或 F。"); |
|
||||
} |
|
||||
|
|
||||
} catch (NumberFormatException e) { |
|
||||
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
|
||||
} catch (ArrayIndexOutOfBoundsException e) { |
|
||||
System.out.println("输入格式错误,请按示例输入数值与单位,例如:36.6 C"); |
|
||||
} finally { |
|
||||
scanner.close(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue