实验四

这个实验要求是,针对各种 checked exception,相信你在 Lab 3 的程序中均已经被“强制”考 虑过了。本节通过自定义 Exception 类型,表示程序的一些常见错误类型,为 代码可能发生的问题提供新的含义,区分代码运行时可能出现的相似问题,或给 出程序中一组共性错误的特殊含义。 考虑以下各个场景,使用 exception 和常见的错误机制来处理它们,尽可能 fail fast。为每种错误类型建立新的异常类,在 GraphFactory 及其子类的代码中 增加错误和异常处理代码,在特定函数上声明异常(throws)、抛出异常(throw)、 捕获并处理异常(try-catch-finally、try-with-resources)。

SocialNetwork 图中,不管用户对图如何修改,所有边的权值之和应始终等于 1; 

MovieGraph 图中,任意超边中包含的节点数量一定大于 1

NetworkTopology图中,不管用户对图如何修改,所有边的权值之和应始终等于 1

ConcreteGraph图中,任意超边中包含的节点数量都不大于1

在各种代码中直接输入要进行判断的东西

Vertex的构造函数中,其参数label必须满足\w+正则表达式的要求;

Vertex fillConcreteInformation(String[] args)方法中, args 中包含的参数数量应与 Vertex 子类型中规定的参数数量一致;

Actor中必须满足性别年龄的要求;

Computerip的各种要求;

DirecterPerson同上边Actor一样;

Movie中需要满足年份的要求;

其它的按照实验三的要求,用其中的特性来书写Assertion


使用等价类和边界值的测试思想,为 ADT ConcreteGraphVertexEdge 和它们的所有子类型中添加 testing strategy。 考虑 3.1 节中出现的多种非法情形,设计一组测试用例,人为制造非法输入的文件和非法输入的图操作指令,对程序进行健壮性和正确性测试,想方设法让 程序崩溃(即验证程序是否有容错能力)。 使用JUnit 为上述测试用例编写代码,并运行测试。 使用 EclEmma 查看测试的覆盖度,若覆盖度过低,继续增加测试用例,直到覆盖度逼近 100%。生成 EclEmma 的测试覆盖度报告。


public class CalculatorGui extends JFrame {
  private JButton[] btnNum = new JButton[10];
  private JButton[] btnOp = new JButton[5];
  private JButton btnCe;
  private JButton btnDecimal;
  private JPanel pnlNum;
  private JPanel pnlOp;
  private JLabel txtResult;
  private String buffer;
  private String operation;
  private float result;
  private boolean init;
  private Font largeFont = new Font("Arial", Font.BOLD, 22);
  /**
   * Multiple lines of Javadoc text are written here,
   * wrapped normally...
   */
  public CalculatorGui() {
    //variable to store the value of the state of the calculator
    result = 0;
    //String buffer for multi-digit inputs
    buffer = "";
    //String container for the type of arithmetic operation to perform
    operation = "";
    //Set whether the calculator state variables are storing valid values
    //to handle edge cases
    init = false;
   
    //set attributes
    setSize(330, 350);
    setTitle("COSC121 Calculator");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //create objects
    txtResult = new JLabel("" + result);
    txtResult.setFont(largeFont);
    txtResult.setHorizontalAlignment(JTextField.RIGHT);
   
    // buttons
    for (int i = 0; i < 10; i++) {
      btnNum[i] = new JButton(i + "");
      btnNum[i].setFont(largeFont);
      btnNum[i].addActionListener(new DigitActionListener());
    }

    btnDecimal = new JButton(".");
    btnCe = new JButton("CE");
    btnDecimal.setFont(largeFont);
    btnCe.setFont(largeFont);
    btnDecimal.addActionListener(new DigitActionListener());
    btnCe.addActionListener(new CeActionListener());
    btnOp[0] = new JButton("+");
    btnOp[1] = new JButton("-");
    btnOp[2] = new JButton("x");
    btnOp[3] = new JButton("/");
    btnOp[4] = new JButton("=");
    // Set font of Operations and add Action Listeners
    for (int i = 0; i < 5; i++) {
      btnOp[i].setFont(largeFont);
      btnOp[i].addActionListener(new OpActionListener());
    }
    // panels
    pnlNum = new JPanel(new GridLayout(3, 4));
    pnlOp = new JPanel(new GridLayout(5, 1));
    // 3) ADDING
    // add to panels
    for (int i = 0; i < btnNum.length; i++) {
      pnlNum.add(btnNum[i]);
    }
    pnlNum.add(btnDecimal);
    pnlNum.add(btnCe);
    for (int i = 0; i < btnOp.length; i++) {
      pnlOp.add(btnOp[i]);
    }
    // add to JFrame
    add(txtResult, "North");
    add(pnlNum);
    add(pnlOp, "East");
  }
  //Method to process the input of digits and decimal points
 
  /** An especially short bit of Javadoc. */
  public void processDigitInput(JButton value) {
    // ignore decimal points if they are already exists one in buffer
    if (!(buffer.contains(".") && value.getText().equals("."))) {
      buffer = buffer + value.getText();
    }
  }
  // method to handle input from operation buttons
  /** An especially short bit of Javadoc. */
  public void processOperationInput(String in) {
    switch (in) {
      case "+":
        operation = "+";
        break;
      case "-":
        operation = "-";
        break;
      case "x":
        operation = "*";
        break;
      case "/":
        operation = "/";
        break;
      default:
    }
  }
  /**Carries out appropriate arithmetic operation based on state variables.*/
  public void performOperation() {
    if (!buffer.equals(".")) {
      // Handles edge case of first input after default state where buffer
      //is empty and result is zero
      if (!init && !buffer.equals("")) {
        result = Float.parseFloat(buffer);
        txtResult.setText("" + result);
        buffer = "";
    
      //  Find appropriate arithmetic operation to perform given the user's input
      } else if (!operation.equals("") && !buffer.equals("")) {
        switch (operation) {
          case "+":
            result = result + Float.parseFloat(buffer);
            buffer = "";
            txtResult.setText("" + result);
            break;
          case "-":
            result = result - Float.parseFloat(buffer);
            buffer = "";
            txtResult.setText("" + result);
            break;
          case "x":
            result = result * Float.parseFloat(buffer);
            buffer = "";
            txtResult.setText("" + result);
            break;           
          case "/":
            buffer = "";
            txtResult.setText("" + result);
            break;
          case "=":
            txtResult.setText("" + result);
            buffer = "";
            operation = "";
            result = 0;
            init = false;
            break;
          default:
            System.out.println("wrong");
        }
        System.out.println("Current result: " + result);
      }
    }
  }
 
  /**Method to reset calculator's state to default.*/
  public void clear() {
    result = 0;
    txtResult.setText("" + result);
    buffer = "";
    operation = "";
    init = false;
  }
  // Action listener for operation buttons
  class OpActionListener extends JFrame implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent op) {
      performOperation();
      operation = ((JButton) op.getSource()).getText();
      System.out.println(operation);
    }
  }
 
  // Action listener for digit buttons
  class DigitActionListener extends JFrame implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent digit) {
      processDigitInput((JButton) digit.getSource());
      System.out.println(buffer);
    }
  }
 
  // Action listener for digit buttons
  class CeActionListener extends JFrame implements ActionListener {
   
    @Override
    public void actionPerformed(ActionEvent ce) {
      clear();
    }
  }
 
  public static void main(String[] args) {
    new CalculatorGui().setVisible(true);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值