public class Demo {
    
    // tests strings to see whether they are palindromes
    public static void p1(String[] pars) {
        String str, another= "y";
        int left, right;
        
        while (another.equalsIgnoreCase("y")) {// allow y or Y
            
            System.out.println("Enter a potential palindrome:");
            str= Keyboard.readString();
            
            left= 0;
            right=  str.length() - 1;
        
            while (str.charAt(left) == str.charAt(right) && left < right) {
                left= left + 1; right= right -1;
            }
            if (left < right)
                System.out.println("It is not a palindrome");
            else System.out.println("It is a palindrome");
            
            System.out.println("Test another palindrome (y or n)");
            another= Keyboard.readString();
        }
    }
    
    /* The outer loop controls how many strings are tested, and
     * the inner
     * loop scans through each string, character by characater,
     * until it 
     * determines whether the string is a palindrom.
     * 
     * Variables left and right contain indexes of two characters.
     * They
     * initially indicate the characters on either end of the
     * string. Each
     * iteration of the inner loop compares the two characters
     * indicated
     * by left and right. We fall out of the loop when either
     * the characters
     * don't match, meaning the string is not a palindrome, or
     * when the
     * value of left becomes equal to or greater than the value
     * of right,
     * which means the entire string has been tested and it is a palindrome.
    */
    
    // test strings read from keyboard to see whether they are palindromes
    public static void p2(String[] pars) {
        String again= "y";

        /** inv: All previously read strings have been tested and
                 another should be tested if again = y or Y */
        while (again.equalsIgnoreCase("y")) {
            
            // Read in another string from keyboard and print
            // whether or not it is a palindrome
                System.out.println("Enter a potential palindrome:");
                String str= Keyboard.readString();
            
                int h= 0;
                int k=  str.length() - 1;
                // inv: b[0..h-1] is the reverse of b[k+1..] and
                //      b[h..k] remains to be tested
                while (h < k && str.charAt(h) == str.charAt(k)) {
                     h= h + 1; k= k -1;
                }
               
                // post: invariant and
                //       str is a palindrome iff h is not < k
               
                if (h < k)
                    System.out.println("It is not a palindrome");
                else System.out.println("It is a palindrome");
            
                System.out.println("Test another palindrome (y or n)");
            again= Keyboard.readString();
        }
    }
    
    /** Demo throwing an exception */
    public static void d1() {
        int x;
        x=  5/0;
    }
    
    /** Demo throwing an exception */
    public static void d2() {
        d1();
    }
    
    /** Demo explicit throwing of an exception */
    public static void d3() {
        throw new ArithmeticException("We threw this");
    }
    
    /** Demo throwing an exception */
    public static void d4() {
        d3();
    }
    
    /** Demo catching an exception */
    public static void d5() {
        int x;
        try {
            x=  5/0;
        }
        catch (ArithmeticException e) {
            System.out.println("We caught exception " + e);
        }
    }
    
    /** Demo catching an exception */
    public static void d6() {
        int x;
        try {
            x=  5/0;
        }
        catch (ArithmeticException e) {
            System.out.println("We caught exception " + e);
        }
    }
    
    /** Demo of NOT catching an exception */
    public static void d7() {
        int x;
        try {
            x=  5/0;
        }
        catch (OutOfMemoryError e) {
            System.out.println("We caught exception " + e);
        }
    }
    
    /** Demo throwing an exception */
    public static void d10() {
        int x;
        x=  5/0;
    }
    
    /** Demo of catching an exception in a different method */
    public static void d11() {
        try {
           d10();
        }
        catch (ArithmeticException e) {
            System.out.println("We did catch exception " + e);
        }
    }
    
    /** Demo a reason for catching an exception.
     *  Method to read an integer from the keyboard and return it.
     *  If the line read is not an integer, keep asking the user
     *  for one until they give it. */
    public static int readInt()  {
        System.out.println("Type an integer");
        // inv: The user has been prompted to type an integer, and
        //      they haven't typed one yet
        while (true) {
            String s= Keyboard.readString();
            try {
                int ans= Integer.parseInt(s);
                return ans;
            }
            catch (NumberFormatException e){
                 System.out.println("That wasn't an integer; type an integer");
            }
        }
        
    }
    
    
}