Assertions

This page is under construction. Please come back later.
import org.pureprogrammer.Utils;
public class Assertions1 {
static final double TEST_BASE = 0.9;
static double exponentiation(double base, int powerArg) {
assert powerArg >= 0 : "powerArg >= 0";
int power = powerArg;
double result = 1.0;
while (power > 0) {
result *= base;
--power;
}
return result;
}
public static void main(String[] args) {
for (int p = 10; p >= -1; --p) {
System.out.println(Utils.format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
}
}
}
Output
$ javac -Xlint Assertions1.java
$ java -ea Assertions1
0.900000 ^ 10 = 0.348678
0.900000 ^ 9 = 0.387420
0.900000 ^ 8 = 0.430467
0.900000 ^ 7 = 0.478297
0.900000 ^ 6 = 0.531441
0.900000 ^ 5 = 0.590490
0.900000 ^ 4 = 0.656100
0.900000 ^ 3 = 0.729000
0.900000 ^ 2 = 0.810000
0.900000 ^ 1 = 0.900000
0.900000 ^ 0 = 1.000000
Exception in thread "main" java.lang.AssertionError: powerArg >= 0
at Assertions1.exponentiation(Assertions1.java:8)
at Assertions1.main(Assertions1.java:20)
import org.pureprogrammer.Utils;
public class Assertions2 {
static final double TEST_BASE = 0.9;
static double exponentiation(double base, int powerArg) {
assert powerArg >= 0 : Utils.join("", "exponentiation() power must be non-negative, was ", powerArg);
int power = powerArg;
double result = 1.0;
while (power > 0) {
result *= base;
--power;
}
return result;
}
public static void main(String[] args) {
for (int p = 10; p >= -1; --p) {
System.out.println(Utils.format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
}
}
}
Output
$ javac -Xlint Assertions2.java
$ java -ea Assertions2
0.900000 ^ 10 = 0.348678
0.900000 ^ 9 = 0.387420
0.900000 ^ 8 = 0.430467
0.900000 ^ 7 = 0.478297
0.900000 ^ 6 = 0.531441
0.900000 ^ 5 = 0.590490
0.900000 ^ 4 = 0.656100
0.900000 ^ 3 = 0.729000
0.900000 ^ 2 = 0.810000
0.900000 ^ 1 = 0.900000
0.900000 ^ 0 = 1.000000
Exception in thread "main" java.lang.AssertionError: exponentiation() power must be non-negative, was -1
at Assertions2.exponentiation(Assertions2.java:8)
at Assertions2.main(Assertions2.java:20)
import org.pureprogrammer.Utils;
public class Assertions3 {
static final double TEST_BASE = 0.9;
static double exponentiation(double base, int powerArg) {
assert powerArg >= 0 : Utils.join("", "exponentiation() power must be non-negative, was ", powerArg);
int power = powerArg;
double result = 1.0;
while (power > 0) {
result *= base;
--power;
}
// This line will make the result incorrect.
result = -result;
// If power is odd and base is negative, then result should be negative.
// Otherwise result should be positive.
assert (power & 1) == 1 && (base < 0.0) ? result < 0.0 : result >= 0.0 : "Postcondition Failed: Result is wrong sign";
return result;
}
public static void main(String[] args) {
for (int p = 10; p >= -1; --p) {
System.out.println(Utils.format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
}
}
}
Output
$ javac -Xlint Assertions3.java
$ java -ea Assertions3
Exception in thread "main" java.lang.AssertionError: Postcondition Failed: Result is wrong sign
at Assertions3.exponentiation(Assertions3.java:19)
at Assertions3.main(Assertions3.java:25)
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Base Conversion
- Fast Exponentiation
- Fibonacci Numbers with Memoization
- Football Passer Rating (with Assertions)
- Julian Day Number
- Number of Primes (Estimate)
- Ordinal Date
- Ordinal Date (Revisited)
- Shuffle Deck
- Significant Digits
References
- [[Java Language Specification]], Java SE 17 Edition, Gosling, et. al., 2021.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]
Pure Programmer


