type.lib
Class ToolBox

java.lang.Object
  extended by type.lib.ToolBox

public class ToolBox
extends java.lang.Object

This class contains various utilities. Refer to the Java By Abstraction textbook.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca

Method Summary
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 double getBMI(double weight, java.lang.String height)
          Compute the body mass index.
static java.lang.String launch(java.lang.String app, java.lang.String input)
          Launch the indicated app, pass the indicated string to its standard input, and capture (and return) its standard output.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

crash

public static void crash(boolean condition,
                         java.lang.String msg)
Test the passed condition and throw an exception if it is true. Otherwise do nothing. This is a convenience method to be used in lieu of an if and a throw statements.

Parameters:
condition - to test.
Throws:
java.lang.RuntimeException - (with the passed message)

repeat

public static java.lang.String repeat(int count,
                                      char c)
Create a string containing the passed character repeated as many times as specified. If count is less than 1, a zero-length string is returned.

Parameters:
count - the number of repetitions.
c - the character to be repeated.
Returns:
a string containing count repetitions of c.

launch

public static java.lang.String launch(java.lang.String app,
                                      java.lang.String input)
Launch the indicated app, pass the indicated string to its standard input, and capture (and return) its standard output. This method facilitates the creation of a harness to test the output of apps (as opposed to the return of methods). When the app is running, its standard output is inhibited (captured but not displayed) and its standard error (if any) is discarded. For example, the following line invokes the app Compute whose class resides in the c:\harness directory and passes 3 and 4 to its standard input:
String output = ToolBox.launch("c:\\harness\\Compute", "3\n4\n");
If the app resides on the classpath, e.g. in the current working directory, then its name does not have to be qualified.

Parameters:
app - the name of the application's class (without the .class extension). If the app does not reside on the classpath then its name must be preceded by an absolute path (using \ or / as separator). The contract here is that the class must have a static main method, and the app.class file must exist either on the classpath or in the indicated path.
input - the input string to be passed to the app's standard input. The input must be suffixed by \n to emulate pressing the ENTER key. If the app expects several inputs, then suffix each by \n and concatenate before passing to this method.
Returns:
the standard output of the app. The returned string represents a concatenation of all the output lines generated by the app (each suffixed by \n).
Throws:
java.lang.RuntimeException - if the app could not be launched, if it required more, or consumed less, input than supplied, or if it caused a runtime error. Inspect the exception's message to determine the exact cause.

mortgagePayment

public static double mortgagePayment(double amount,
                                     double rate,
                                     int period)
Compute the monthly payment on a mortgage assuming constant payments and constant interest rate.

Parameters:
amount - the present value
rate - the interest rate as an annualized percent, i.e. pass 4.5 for 4.5%
period - the amortization period in years
Returns:
the monthly payment
Throws:
java.lang.RuntimeException - if amount < 0, rate <= 0, or rate >= 100

getBMI

public static double getBMI(double weight,
                            java.lang.String height)
Compute the body mass index.

Parameters:
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.
Returns:
the body mass index (BMI) for the given weight and height
Throws:
java.lang.RuntimeException - if either weight or height is not valid as defined above.

factorial

public static double factorial(int n)
Determine the factorial of the passed integer.

Parameters:
n - a non-negative integer
Returns:
the factorial of n, n!.
Throws:
java.lang.RuntimeException - if n is negative.


Java by Abstraction: A Client-View Approach