这个实验要求是,针对各种 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中必须满足性别年龄的要求;
在Computer中ip的各种要求;
在Directer和Person同上边Actor一样;
在Movie中需要满足年份的要求;
其它的按照实验三的要求,用其中的特性来书写Assertion。
使用等价类和边界值的测试思想,为 ADT ConcreteGraph、Vertex、Edge 和它们的所有子类型中添加 testing strategy。 考虑 3.1 节中出现的多种非法情形,设计一组测试用例,人为制造非法输入的文件和非法输入的图操作指令,对程序进行健壮性和正确性测试,想方设法让 程序崩溃(即验证程序是否有容错能力)。 使用JUnit 为上述测试用例编写代码,并运行测试。 使用 EclEmma 查看测试的覆盖度,若覆盖度过低,继续增加测试用例,直到覆盖度逼近 100%。生成 EclEmma 的测试覆盖度报告。
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 operation;
private float result;
private boolean init;
* 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[1] = new JButton("-");
btnOp[2] = new JButton("x");
btnOp[3] = new JButton("/");
btnOp[4] = new JButton("=");
for (int i = 0; i < 5; i++) {
btnOp[i].setFont(largeFont);
btnOp[i].addActionListener(new OpActionListener());
}
pnlNum = new JPanel(new GridLayout(3, 4));
pnlOp = new JPanel(new GridLayout(5, 1));
// add to panels
for (int i = 0; i < btnNum.length; i++) {
pnlNum.add(btnNum[i]);
}
pnlNum.add(btnDecimal);
pnlNum.add(btnCe);
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();
}
}
/** 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:
}
}
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;
}
class OpActionListener extends JFrame implements ActionListener {
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 {
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);
}
}