‌用Java任务构造3.1和3.2 构造任务3.1:实现BinaryOperation及其子类。 构造任务3.2:实现 Exercise 和 ExerciseSheet。 ‍‌内容:在构造任务2.5基础上用面向对象构造技术,实现运算数、运算结果值的范围都在[0.100]的加法、减法、加减混合50道题 ‌用junit验证:分别输出符合要求的50道加法、减法、加减混合题个2套,美观输出。

时间: 2023-12-14 20:36:16 浏览: 41
任务3.1:实现BinaryOperation及其子类 首先,我们需要定义一个BinaryOperation抽象类,它代表二元运算: ```java public abstract class BinaryOperation { protected double operand1; protected double operand2; protected double result; public BinaryOperation(double operand1, double operand2) { this.operand1 = operand1; this.operand2 = operand2; } public abstract void calculate(); public double getOperand1() { return operand1; } public double getOperand2() { return operand2; } public double getResult() { return result; } } ``` 我们定义了三个属性,分别代表两个运算数和运算结果。我们还定义了一个抽象的calculate方法,子类需要实现该方法来完成具体的运算。 接下来,我们定义三个子类,分别代表加法、减法和加减混合运算: ```java public class Addition extends BinaryOperation { public Addition(double operand1, double operand2) { super(operand1, operand2); } @Override public void calculate() { result = operand1 + operand2; } } public class Subtraction extends BinaryOperation { public Subtraction(double operand1, double operand2) { super(operand1, operand2); } @Override public void calculate() { result = operand1 - operand2; } } public class MixedOperation extends BinaryOperation { private boolean isAddition; public MixedOperation(double operand1, double operand2, boolean isAddition) { super(operand1, operand2); this.isAddition = isAddition; } @Override public void calculate() { if (isAddition) { result = operand1 + operand2; } else { result = operand1 - operand2; } } } ``` Addition和Subtraction类分别实现了加法和减法运算,而MixedOperation类则实现了加减混合运算。我们在MixedOperation类中添加了一个boolean类型的isAddition属性,用于标记当前运算是加法还是减法。 任务3.2:实现Exercise和ExerciseSheet 现在我们需要实现Exercise和ExerciseSheet类,以便生成50道题目。 首先,我们定义一个Exercise类,表示一道练习题: ```java public class Exercise { private String expression; private double answer; public Exercise(String expression, double answer) { this.expression = expression; this.answer = answer; } public String getExpression() { return expression; } public double getAnswer() { return answer; } } ``` 我们定义了两个属性,分别代表题目表达式和答案。 接下来,我们定义ExerciseSheet类,表示练习题集合: ```java import java.util.ArrayList; import java.util.List; import java.util.Random; public class ExerciseSheet { private List<Exercise> exercises; public ExerciseSheet() { exercises = new ArrayList<>(); } public void generateExercises(int count) { Random random = new Random(); for (int i = 0; i < count; i++) { int type = random.nextInt(3); // 随机生成加法、减法、加减混合运算 double operand1 = random.nextDouble() * 100; // 随机生成第一个运算数 double operand2 = random.nextDouble() * 100; // 随机生成第二个运算数 BinaryOperation operation; if (type == 0) { operation = new Addition(operand1, operand2); } else if (type == 1) { operation = new Subtraction(operand1, operand2); } else { boolean isAddition = random.nextBoolean(); // 随机生成加减混合运算是加法还是减法 operation = new MixedOperation(operand1, operand2, isAddition); } operation.calculate(); Exercise exercise = new Exercise(operation.getOperand1() + " " + (type == 0 ? "+" : "-") + " " + operation.getOperand2() + " =", operation.getResult()); exercises.add(exercise); } } public List<Exercise> getExercises() { return exercises; } } ``` 我们定义了一个List类型的exercises属性,用于存储练习题。generateExercises方法用于生成指定数量的练习题,它先随机生成加法、减法、加减混合运算类型,然后随机生成两个运算数,并根据运算类型创建相应的BinaryOperation对象进行计算,最后将表达式和答案封装成Exercise对象并添加到exercises中。 验证代码: ```java import org.junit.Test; public class ExerciseSheetTest { @Test public void testGenerateExercises() { ExerciseSheet sheet = new ExerciseSheet(); sheet.generateExercises(50); for (int i = 0; i < 50; i++) { System.out.println(sheet.getExercises().get(i).getExpression() + " " + sheet.getExercises().get(i).getAnswer()); } } } ``` 这段代码会生成一套50道练习题,并将其输出到控制台上。 美观输出: 为了实现美观输出,我们需要使用表格。这里我们使用第三方库apache poi。 我们需要在pom.xml中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 然后,我们可以在ExerciseSheet类中添加以下方法: ```java public void exportToExcel(String fileName) throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("练习题"); // 设置表头 XSSFRow header = sheet.createRow(0); header.createCell(0).setCellValue("序号"); header.createCell(1).setCellValue("题目"); header.createCell(2).setCellValue("答案"); // 填充数据 for (int i = 0; i < exercises.size(); i++) { Exercise exercise = exercises.get(i); XSSFRow row = sheet.createRow(i + 1); row.createCell(0).setCellValue(i + 1); row.createCell(1).setCellValue(exercise.getExpression()); row.createCell(2).setCellValue(exercise.getAnswer()); } // 自动调整列宽 for (int i = 0; i < 3; i++) { sheet.autoSizeColumn(i); } // 输出到文件 FileOutputStream outputStream = new FileOutputStream(fileName); workbook.write(outputStream); workbook.close(); outputStream.close(); } ``` 这个方法会将练习题导出到一个Excel文件中,并设置表头、序号、题目和答案三列。我们还调用了autoSizeColumn方法来自动调整列宽。 最后,我们可以在测试代码中添加以下代码,将练习题导出到一个Excel文件中: ```java @Test public void testExportToExcel() throws IOException { ExerciseSheet sheet = new ExerciseSheet(); sheet.generateExercises(50); sheet.exportToExcel("exercise.xlsx"); } ``` 这段代码会生成一套50道练习题,并将其导出到当前目录下的exercise.xlsx文件中。

相关推荐

最新推荐

recommend-type

JESD220E ufs3.1协议

JESD220E ufs3.1协议 ,Univeral Flash Storage,我们可以将它视为eMMC的进阶版,是由多个闪存芯片、主控、缓存组成的阵列式存储模块。UFS弥补了eMMC仅支持半双工运行(读写必须分开执行)的缺陷,可以实现全双工运行...
recommend-type

osg3.6.3和osgearth3.1的编译全过程.docx

使用vs2017和cmake编译osg3.6.3;编译osg3.1;并使用osgearth
recommend-type

PD 3.1 最新官方协议文档 V1.3

PD 3.1 最新官方协议文档 V1.3,官网下载地址 https://www.usb.org/document-library/usb-power-delivery
recommend-type

RVCT3.1编译器环境的搭建

RVCT3.1编译器环境的搭建, 适用于开发sim800系列产品,经测试
recommend-type

QAC工具介绍和使用说明(供一种可量化措施的代码度量值属性:33基于功能 32基于文件和4个项目级别)

1、 QAC介绍和使用说明 其他的功能概括 1、提供一种可量化措施的代码度量值属性:33基于功能 32基于文件和4个项目级别 2、功能结构关系图,以提供控制流动洞察 3、展示全局调用函数的关系图引用和文件树结构 4、提供...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。