1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| cd ~/demo-java-app
cat >> src/main/java/com/demo/App.java << 'EOF'
public static double divide(int a, int b) { if (b == 0) { throw new ArithmeticException("Division by zero!"); } return (double) a / b; } EOF
cat > src/main/java/com/demo/App.java << 'EOF' package com.demo;
public class App { public static void main(String[] args) { System.out.println("==================================="); System.out.println(" Demo Java Application v1.0"); System.out.println("==================================="); System.out.println("Application started successfully!"); }
public static int add(int a, int b) { return a + b; }
public static int subtract(int a, int b) { return a - b; }
public static int multiply(int a, int b) { return a * b; }
public static double divide(int a, int b) { if (b == 0) { throw new ArithmeticException("Division by zero!"); } return (double) a / b; } } EOF
cat > src/test/java/com/demo/AppTest.java << 'EOF' package com.demo;
public class AppTest { public static void main(String[] args) { System.out.println("Running tests...");
// 测试 add 方法 int result1 = App.add(2, 3); assert result1 == 5 : "add(2,3) should be 5, but got " + result1; System.out.println("✅ Test add(2,3) = 5: PASSED");
// 测试 subtract 方法 int result2 = App.subtract(10, 4); assert result2 == 6 : "subtract(10,4) should be 6, but got " + result2; System.out.println("✅ Test subtract(10,4) = 6: PASSED");
// 测试 multiply 方法 int result3 = App.multiply(3, 4); assert result3 == 12 : "multiply(3,4) should be 12, but got " + result3; System.out.println("✅ Test multiply(3,4) = 12: PASSED");
// 测试 divide 方法 double result4 = App.divide(10, 3); assert Math.abs(result4 - 3.333333) < 0.001 : "divide(10,3) ≈ 3.333"; System.out.println("✅ Test divide(10,3) ≈ 3.333: PASSED");
// 测试边界情况 int result5 = App.add(0, 0); assert result5 == 0 : "add(0,0) should be 0, but got " + result5; System.out.println("✅ Test add(0,0) = 0: PASSED");
System.out.println(""); System.out.println("All tests passed! ✅"); } } EOF
git add -A git commit -m "feat: 新增 divide 除法方法及测试"
|