/** * Playing around with functions, conditionals, loops, and constants. * * @author Sean Barker */ public class Lec3 { /** * The freezing point of water (in F). */ private static final int FREEZING_POINT = 32; /** * The boiling point of water (in F). */ private static final int BOILING_POINT = 212; /** * Given the temperature of water, return the state of the water * (solid, liquid, or gas). * * @param temp The desired water temperature. * @return The state of the water at the given temperature. */ public static String waterType(double temp) { if (temp <= FREEZING_POINT) { return "solid"; } else if (temp <= BOILING_POINT) { return "liquid"; } else { return "gas"; } } /** * Returns whether a given int is positive (>0). * * @param num The number to check. * @return Whether the number is greater than zero. */ public static boolean isPositive(int num) { return num > 0; } /** * Find the index of the first occurrence of a letter in a word. * If the letter isn't found, returns -1. * * @param word The word to search. * @param letter The letter to search for. * @return The index of letter in word, or -1 if the letter is not found. */ public static int indexOfLetter(String word, char letter) { for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == letter) { return i; } } return -1; // only reached if the letter isn't found } /** * Given a string, return a new string that is the reverse * of the original. * * @param input The original string. * @return The reversed string. */ public static String reverseString(String input) { String reversed = ""; for (int i = input.length() - 1; i >= 0; i--) { reversed += input.charAt(i); } return reversed; } }