/** MiscMethods.java presents miscellaneous methods.
 *  @author Joel Adams, for Alice+Java.
 */
public class MiscMethods {
	public static void main(String[] args) {
		double energy = MiscMethods.massToEnergy(1.0);
		System.out.println(energy);
		
		char initial1 = MiscMethods.firstInitial("Homer Jay Simpson");
		char initial2 = MiscMethods.lastInitial("Homer Jay Simpson");
		System.out.println(initial1 + "." + initial2 + ".");
		System.out.println( twoInitials("Home Jay Simpson") );

	}

	/** massToEnergy computes the energy obtained from
	 *   a quantity of mass, using Einstein's formula.
	 * @param mass, (a double) in kilograms
	 * @return the kilojoules when mass is converted to energy
	 */
	public static double massToEnergy(double mass) {
		final double SPEED_OF_LIGHT = 2.99792458e8;
		return mass * Math.pow(SPEED_OF_LIGHT, 2.0);
	}
	
	/** firstInitial returns the first initial of a person's name
	 * @param name, a String.
	 * Precondition: the first thing in name is a person's first name.
	 * @return the first character of name.
	 */
	public static char firstInitial(String name) {
		return name.charAt(0);
	}
	
	/** lastInitial returns the last initial of a person's name
	 * @param name, a String.
	 * Precondition: name contains a person's first name
	 *            and last name, separated by a space.
	 * @return the first character of the last name.
	 */
	public static char lastInitial(String name) {
		int indexOfSpace = name.lastIndexOf(' ');
		return name.charAt(indexOfSpace + 1);
	}

	public static String twoInitials(String name) {
		String result = "";           // start with empty string
		result += firstInitial(name); // append first initial
		result += lastInitial(name);  // append last initial
		return result;                // we're done!
	}
}
