TestAssert1.java
Select all
/* TestAssert1.java CIS 260 2/16/2005 David Klick Demonstrates a Java assertion which uses the default AssertionError constructor. */ public class TestAssert1 { public static void main(String[] args) { double a = 3.5, b = 5.2; System.out.println(divide(a, b)); System.out.println(divide(a, 0.0)); } static double divide(double x, double y) { // note that division by 0 in floating point // numbers is actually allowed in Java, but // now we are disallowing it assert y != 0.0; return x / y; } } /* Sample output: 0.673076923076923 Exception in thread "main" java.lang.AssertionError at TestAssert1.divide(TestAssert1.java:13) at TestAssert1.main(TestAssert1.java:5) */