【软件构造与演化】20级实验参考

索引

    • 前言
    • 实验1:面向对象的软件构造
      • 问题描述:
      • 参考答案:
    • 实验2:数据处理的软件构造
      • 问题描述:
      • 参考答案:

前言

不一定准确但是确实能跑出点东西。
内容可能来自博主自己手搓、吸取同学的经验、网络上内容的整合等等,仅供参考,更多内容可以查看大三下速通指南专栏。

实验1:面向对象的软件构造

问题描述:

生成100以内的加减法算式,隐含要求结果也是100以内

(1) 实现BinaryOperation及其子类;

(2) 实现Exercise和ExerciseSheet类。

参考答案:

这里使用java来实现以上功能的,当然也可以用其他的语言(c++/python等)。

以下共6个.java文件

  1. BinaryOperation.java
public abstract class BinaryOperation {
    protected int operand1;
    protected int operand2;

    public BinaryOperation(int operand1, int operand2) {
        this.operand1 = operand1;
        this.operand2 = operand2;
    }

    public abstract int getResult();

    public abstract String getOperator();

    @Override
    public String toString() {
        if (operand1 > operand2)
          return operand1 + " " + getOperator() + " " + operand2 + " = ";
        return operand2 + " " + getOperator() + " " + operand1 + " = ";
    }
}
  1. Addition.java
public class Addition extends BinaryOperation {
    public Addition(int operand1, int operand2) {
        super(operand1, operand2);
    }

    @Override
    public int getResult() {
        return operand1 + operand2;
    }

    @Override
    public String getOperator() {
        return "+";
    }
}
  1. Subtraction.java
public class Subtraction extends BinaryOperation {
    public Subtraction(int operand1, int operand2) {
        super(operand1, operand2);
    }

    @Override
    public int getResult() {
        if (operand1 > operand2)
            return operand1 - operand2;
        return operand2 - operand1;
    }

    @Override
    public String getOperator() {
        return "-";
    }
}
  1. Exercise.java
import java.util.ArrayList;
import java.util.List;

public class Exercise {
    private BinaryOperation binaryOperation;

    public Exercise(BinaryOperation binaryOperation) {
        this.binaryOperation = binaryOperation;
    }

    public String getQuestion() {
        return binaryOperation.toString();
    }

    public int getAnswer() {
        return binaryOperation.getResult();
    }
}

5.ExerciseSheet.java

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ExerciseSheet {
    private List<Exercise> exercises;

    public ExerciseSheet(int numExercises) {
        exercises = new ArrayList<>();

        Random random = new Random();

        for (int i = 0; i < numExercises; i++) {
            BinaryOperation binaryOperation;
            int operand1 = random.nextInt(100) + 1;
            int operand2 = random.nextInt(100 - operand1) + 1;

            if (random.nextBoolean()) {
                binaryOperation = new Addition(operand1, operand2);
            } else {
                binaryOperation = new Subtraction(operand1, operand2);
            }

            exercises.add(new Exercise(binaryOperation));
        }
    }

    public List<Exercise> getExercises() {
        return exercises;
    }
}
  1. Test.java
public class Test {
    public static void main(String[] args) {
        ExerciseSheet exerciseSheet = new ExerciseSheet(10);

        System.out.println("Exercise sheet:");

        int i = 1;
        for (Exercise exercise : exerciseSheet.getExercises()) {
            System.out.println(i + ". " + exercise.getQuestion());
            i++;
        }
    }

}

直接运行Test.java中的main函数就可以生成一组满足条件的式子了。

实验2:数据处理的软件构造

问题描述:

(1) 批量生成产生习题并用文件存储;

(2) 生成100以内加法/减法的算式基;

(3) 批改练习并存储。

参考答案:

以下共三个类:

ExerciseGenerator

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class ExerciseGenerator {
    private static final int NUM_EXERCISES = 10;
    private static final int MAX_NUMBER = 100;
    private static final String EXERCISES_FILE = "exercises.txt";

    public static void generateExercises() {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(EXERCISES_FILE));
            Random rand = new Random();
            for (int i = 1; i <= NUM_EXERCISES; i++) {
                int num1 = rand.nextInt(MAX_NUMBER + 1);
                int num2 = rand.nextInt(MAX_NUMBER + 1);
                char operator = (rand.nextInt(2) == 0) ? '+' : '-';
                int result = 0;
                if (operator == '+') {
                    if (num1 + num2 > 100) {
                        i--;
                        continue;
                    }
                    result = num1 + num2;
                } else {
                    if (num1 - num2 < 0) {
                        i--;
                        continue;
                    }
                    result = num1 - num2;
                }
                String exercise = num1 + " " + operator + " " + num2 + " = ?";
                System.out.println("Exercise " + i + ": " + exercise);
                writer.write(exercise + " " + result + "\n");
            }
            writer.close();
            System.out.println("Exercises generated and saved to " + EXERCISES_FILE);
        } catch (IOException e) {
            System.out.println("Error generating exercises.");
            e.printStackTrace();
        }
    }
}

ExerciseChecker

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExerciseChecker {
    private static final String EXERCISES_FILE = "exercises.txt";
    private static final String ANSWERS_FILE = "answers.txt";

    public static void checkExercises() {
        try {
            FileReader reader = new FileReader(EXERCISES_FILE);
            BufferedReader bufferedReader = new BufferedReader(reader);
            FileWriter writer = new FileWriter(ANSWERS_FILE);

            String exercise;
            int exerciseNumber = 1;
            while ((exercise = bufferedReader.readLine()) != null) {
                int answer = getAnswerFromExercise(exercise);
                int submittedAnswer = getSubmittedAnswer(exerciseNumber);
                String result = (answer == submittedAnswer) ?
                        "Exercise " + exerciseNumber + ": Correct\n" : "Exercise " + exerciseNumber + ": Incorrect\n";
                result += " " + exercise + " Submitted answer: " + submittedAnswer + " Correct answer: " + answer + "\n";
                writer.write(result);
                exerciseNumber++;
            }
            writer.close();
            System.out.println("Exercise results checked and saved to " + ANSWERS_FILE);
        } catch (IOException e) {
            System.out.println("Error checking exercises.");
            e.printStackTrace();
        }
    }

    private static int getAnswerFromExercise(String exercise) {
        String[] parts = exercise.split("\\s+");
        int num1 = Integer.parseInt(parts[0]);
        char operator = parts[1].charAt(0);
        int num2 = Integer.parseInt(parts[2]);
        return (operator == '+') ? num1 + num2 : num1 - num2;
    }

    private static int getSubmittedAnswer(int exerciseNumber) throws IOException {
        System.out.print("Enter answer for Exercise " + exerciseNumber + ": ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String answerString = reader.readLine();
        return Integer.parseInt(answerString);
    }
}

Main

public class Main {
    public static void main(String[] args) {
        ExerciseGenerator.generateExercises();
        ExerciseChecker.checkExercises();
    }
}

你可能感兴趣的:(大三下速通指南,java,android,开发语言)