/*
 *  Author: B. Thom
 *  HW and problem #: Practice Midterm SOLUTIONS !
 *  Other Comments:
 */


class CS5App
{
  public static void main(String[] args)
  {
        H.out.println("problem 1");
        problem1();
        H.out.println("problem 2");
        problem2();
        H.out.println("problem 3");
        problem3();
        H.out.println("problem 4");
        problem4();
        H.out.println("problem 5");
        problem5();
  }
  
  //example midterm problem 1
  public static void problem1() 
  {
        String word1 = getWord();
        String word2 = getWord();
        printReverse(word2);
        printReverse(word1);
  }
  
  //get string from user
  public static String getWord() 
  {
        H.out.println("enter a string, any string...");
        String word = H.in.nextWord();
        return word;    
  }
  
  //print out chars in word string in reverse order
  public static void printReverse(String word) 
  {
        for(int i = word.length()-1; i >= 0; i--)
                H.out.print(word.charAt(i));
        H.out.println();
  }
  
  
  //example midterm problem 2
  public static void problem2() 
  {
        int sumSoFar = 0; //running sum
        int numItems = 0; //number of items in running sum
        
        int value;
        while((value = getInt()) != -1) 
        {
                //continue entering integers until the -1 quit
                //      loop value occurs
                sumSoFar += value;
                numItems++;
        }
        
        //calculate average, assuming average of 0 items is 0
        double result = 0;
        if(numItems > 0) 
                result = (double)sumSoFar / numItems;
                
        H.out.println("There were " + numItems + " integers entered. ");
        H.out.println("Their average is " + result);
  }
  
  //get next integer
  public static int getInt() 
  {
        H.out.print("Please type and integer (-1 to stop): ");
        int value = H.in.nextInt();
        return value;   
  }
  
  //problems 3 and 4: the easiest way to report how these problems
  //behave on specific input is to type in their code and see what
  //they do. however, it is worthwhile to THINK about how a program
  //your code works by reasoning about each step in your head
  //BEFORE coding it since this is part of the top-down design
  //process we have been emphasizing in CS5
 
  //for example in problem 3:
  //input:              fcllqvfctc 
  //produces:   hellothere
  //because this code:
  //    1) traverse forward two letters in the alphabet
  //       for characters < 'm'
  //    2) traverse backwards two letters for subsequent
  //       characters > 'm'. 
  //key point: the character 'l' will remain unchanged 
  //because it is traversed +2 and then is subsequently 
  //traversed -2
  
  //example midterm problem 3
  public static void problem3() 
  {
        H.out.println("Type a string at the prompt:  ");
        String s = H.in.nextWord();
        H.out.println();
        
        for(int i = 0; i < s.length(); ++i) 
        {
                char c = mixedUpChar(s.charAt(i));
                H.out.print(c);
        }
        H.out.println();
  }
  
  //enocde char c either two ahead or two behind unless
  //    its value is 'm' in which case it remains unchanged
  public static char mixedUpChar(char c) 
  {
        char result = c;
        if (c <= 'm') 
        {
                //for all letters before 'm' in alphabet, move forward 2
                result = (char)(result+2);
        }
        if (result >= 'm') 
        {
                //for all letters before 'm' in alphabet, move backward 2
                result = (char)(result-2);
        }
        return result;
  }
  
  //for example in problem 4:
  //input:              disappearing
  //produces:   disappearing
  //                isappearing
  //                    sappearing
  //                    appearing
  //                    ppearing
  //                    .......
  //                    ing
  //                    ng
  //                    g
  //since this code traverses over and prints all characters in the
  //input string, beginning with:
  //    - location 1 (char 'd'), proceeding to location 2 (char 'i'),
  //      ... up to location n (char 'g'), after which a carriage
  //      return is print
  //    - location 2 (char 'i'), proceeding to location 2 (char 's'),
  //      ... up to location n (char 'g'), after which a carriage
  //      return is print
  //    - ...
  //    - location n (char 'g'), after which a carriage return
  //      is print
  
  //example midterm problem 4
  public static void problem4() 
  {
        H.out.print("Type a string at the prompt: ");
        String s = H.in.nextWord();
        H.out.println();
        
        for(int i = 0; i < s.length(); ++i) 
        {
                for(int j = i; j < s.length(); ++j) 
                {
                        H.out.print(s.charAt(j));
                }
                H.out.println();
        }
        //H.out.println("done");
  }
  
  //example midterm problem 5
  public static void problem5() 
  {
        //ten terms of the sequence 2 4 8 16 32 ... 
        int result = 1;
        for(int i = 0; i < 10; i++) 
        {
                result *= 2;
                H.out.print(result + " ");
        }
        H.out.println();
        
        //ten terms of above sequence, each term repeated
        //      i number of times
        result = 1;
        for(int i = 0; i < 10; i++) 
        {
                result *= 2;
                for(int j = 0; j <= i; j++) 
                {
                        H.out.print(result);
                }               
                H.out.print(" ");
        }       
        H.out.println();
  }
}