public class Utility
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static void |
crash(boolean condition,
java.lang.String msg)
Test the passed condition and throw an exception if it is
true . |
static double |
factorial(int n)
Determine the factorial of the passed integer.
|
static int |
gcd(int a,
int b)
Determine the GCD (Greatest Common Divisor) of the two passed integers.
|
static double |
getBMI(double weight,
java.lang.String height)
Compute the body mass index.
|
static int |
gf(int x)
Determine the Greatest Factor (GF) of the passed integer.
|
static java.lang.String |
m2FtInch(double h)
Convert from meters to feet and inches.
|
static double |
mark()
Mark the current time and determine the elapsed time since the last
invocation.
|
static double |
mortgagePayment(double amount,
double rate,
int period)
Compute the monthly payment on a mortgage assuming constant payments and
constant interest rate.
|
static java.lang.String |
repeat(int count,
char c)
Create a string containing the passed character repeated as many times as
specified.
|
public static void crash(boolean condition, java.lang.String msg)
true
. Otherwise do nothing. This is a convenience method to
be used in lieu of an if
and a throw
statements.condition
- to test.msg
- a custom exception message.java.lang.RuntimeException
- (with the passed message)public static java.lang.String repeat(int count, char c)
count
- the number of repetitions.c
- the character to be repeated.count
repetitions of
c
.public static double mortgagePayment(double amount, double rate, int period)
amount
- the present valuerate
- the interest rate as an annualized percent, i.e. pass 4.5 for
4.5%period
- the amortization period in yearsjava.lang.RuntimeException
- if amount < 0, rate ≤ 0, or rate ≥ 100public static double getBMI(double weight, java.lang.String height)
weight
- the weight in pounds. To be valid, weight must be positive.height
- the height in feet'inches. To be valid, height must have a
feet component (a positive integer) optionally followed by an
inches component (a non-negative integer less than 12). And if
both components are present then they must be separated by a
single quote.java.lang.RuntimeException
- if either weight or height is not valid as defined above.public static double factorial(int n)
n
- a non-negative integern
, n!
.java.lang.RuntimeException
- if n
is negative.public static int gcd(int a, int b)
a
- a positive integer.b
- a positive integer.java.lang.RuntimeException
- if either integer is not positive.public static int gf(int x)
x
is an integer in [1,x)
that
divides x
evenly.x
- the positive integer to factor.x
.java.lang.RuntimeException
- if x ≤ 0
.public static java.lang.String m2FtInch(double h)
h
- the meter amount to be converted.F'I"
where F and I are
the corresponding feet and inches. The inches are rounded to the
nearest inch.public static double mark()