First make sure you can write the programs we developed in class/homework/lab:

 1. Develop a class of Fraction objects with add as an operation. 

    We did this in class most recently before the first exam: 

    http://silo.cs.indiana.edu:8346/c212/sum2019/0514a.phps
    
 2. Develop a static method to calculate the greatest common divisor of two integers. 
    (Add it to the Fraction class to obtain fractions in their lowest terms.)
  
 Feel free to use this recurrence but explain why it works:

    GCD(a, b) = a           if a == b
                 GCD(a-b, b) if a > b
                 GCD(a, b-a) otherwise (that is, if a < b)

 Again, we did this in class, let me know if you can't find it: 

    http://silo.cs.indiana.edu:8346/c212/sum2019

 3. Same question but use another method (just not this one). 

 Here's another method: start from c = min(a, b) and go down until you find a divisor, break. 

 public static int gcd(int a, int b) {
   int c = Math.min(a, b); // assume a > 1 and b > 1
   while (true) { 
     if (a % c == 0 && b % c == 0) { // divisor, clearly the biggest we can find 
       break; 
     } 
     c = c - 1; // try lower, eventually it will hit 1 which will work 
   } 
 } 


 4. Develop a method to calculate Fibonacci numbers using their definitions: 

    f(n) = 1               if n == 0
         = 1               if n == 1
         = f(n-1) + f(n-2) if n > 1

    This was done in class also in lab also in the first exam. 


 5. Develop a method to calculate unbounded Fibonacci numbers with BigIntegers. 

 6. Same as 4., just make sure the method also calculates very fast. 

    Use a loop or accumulator passing style as in the first exam: 

    


 7. Develop a method to calculate the square root of a (an integer) using the recurrence: 

    x_n = 1/2*(x_n + a/x_n)               [1] 
   
    Initial condition: x_0 can be taken as a

    Nick solved this in one of the first labs: 

    http://silo.cs.indiana.edu:8346/c212/sum2019/0509b.phps

 8. Same question just use another method (perhaps binary search like in C211, just not [1]). 

    We started this in one of the first lectures: 




 9. Write a program that reads a string then reports how many times each character occurs in it. 

    You don't need an array, see below. 

10. Same as 9., just don't report the same character twice. 

    We solved this in class using a loop and a method with a loop inside:

    http://silo.cs.indiana.edu:8346/c212/sum2019/0516a.phps

11. Write a program that reads a number with decimals in the interval [0, 4] and reports the closest
    letter grade. Letter grades are: F (0), D- (0.7), D (1.0), D+ (1.3), C- (1.7), C (2.0), C+ (2.3), 
    B- (2.7), B (3.0), B+ (3.3), A- (3.7) and A (4.0). Break ties in favor of the better grade, so a
    2.85 should be reported as a B (not a B-, even though it's equally far apart from both of them). 

    Nick solved this in one of the first labs:

    http://silo.cs.indiana.edu:8346/c212/sum2019/0509b.phps

12. Before 1582 a leap year was a year that was multiple of 4. After 1582 if the year is a multiple of
    4 then it is a leap year unless it's also a multiple of 100 at which point it's not leap unless it
    is also a multiple of 400. Write a program that receives a number between 0 and 2020 and determines
    and reports if the year is a leap year or not. 

    We discussed this in class, here's how you can get started:

    https://introcs.cs.princeton.edu/java/12types/LeapYear.java.html

    Here's a picture for a straightforward diagram: 

    https://cs.indiana.edu/classes/c211/gru.jpg
 
    That should be easy to implement, since the approach is similar to that at #11. 

13. Write a program that reads numbers one at a time (one per line) until the user enters "bye". After each 
    number the program reports a running total (sum, count, max, min and average) like we did in class on Tue. 
    If the user enters "bye" as the first token of input the program should print "No Data" and simply end. 

    We explained yesterday in class how you should do it:

import java.util.*;

class Exam {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter number: ");
    String line = in.nextLine();
    double max = 0, min = 0, sum = 0, count = 0;
    while (! line.equals("Done")) {
      double number = Double.parseDouble( line );
      if (count == 0) {
        max = number;
        min = number;
        sum = number;
        count = 1;
      } else {
        max = ( number > max ) ? number : max;
        min = ( number < min ) ? number : min;
        sum += number;
        count += 1;
      }
      System.out.println("Max: " + max + ", min: " + min + ", average: " + ( sum / count ) + ", range: " + (max - min));
      System.out.print("Enter number: ");
      line = in.nextLine();
    }
    System.out.print("Thanks, bye now.");
    if (count == 0)
      System.out.println(" No data entered.");
    else
      System.out.println("\nMax: " + max + ", min: " + min + ", average: " + ( sum / count ) + ", range: " + (max - min));

  }
}

  We did other things in class yesterday but those are not related to the exam. 

  However the style in which our programs worked were very much like the code shown above. 

14. Write a program that reads a number (like 14, 23 etc.) and then prints a scalable pattern of that size.
    In class we developed a program that would print a scalable uppercase N:

http://silo.cs.indiana.edu:8346/c212/sum2019/0516a.phps

    *                           * 
    * *                         * 
    *   *                       * 
    *     *                     * 
    *       *                   * 
    *         *                 * 
    *           *               * 
    *             *             * 
    *               *           * 
    *                 *         * 
    *                   *       * 
    *                     *     * 
    *                       *   * 
    *                         * * 
    *                           * 

    In class we discussed there are other interesting patterns such as:

-bash-3.2$ java Four 16
                *
              *
            *
          *
        *
      *         *
    *           *
  *             *
* * * * * * * * * * *
                *
                *
                *
                *
                *
                *
                *
-bash-3.2$ java Four 21
                    *
                  *
                *
              *
            *
          *
        *
      *             *
    *               *
  *                 *
* * * * * * * * * * * * * * *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
-bash-3.2$ java Four 9
        *
      *
    *
  *     *
* * * * * * *
        *
        *
        *
        *
-bash-3.2$

Here's an answer for Four.java: 

import java.util.Scanner;

public class Four {
  public static void main(String[] args) {
    Scanner read = new Scanner(System.in);
    System.out.print("What size: ");
    int size = read.nextInt();
    for (int row = 0; row < size; row += 1) {
      for (int col = 0; col < size; col += 1) {
        if (col + row == size/2 ||
            col == size/2 && row >= size/3 ||
            row == size/2 && col < 3 * size / 4) {
          System.out.print("* ");
        } else {
          System.out.print("  ");
        }
      }
      System.out.println();
    }
  }
}


-bash-3.2$ java Kyu 15
              *
            *   *
          *       *
        *           *
      *               *
    *                   *
  *                       *
*                           *
  *             *           *
    *             *       *
      *             *   *
        *             *
          *         *   *
            *     *       *
              * *           *
-bash-3.2$ java Kiu 23
                      *
                    *   *
                  *       *
                *           *
              *               *
            *                   *
          *                       *
        *                           *
      *                               *
    *                                   *
  *                                       *
*                                           *
  *                     *                   *
    *                     *               *
      *                     *           *
        *                     *       *
          *                     *   *
            *                     *
              *                 *   *
                *             *       *
                  *         *           *
                    *     *               *
                      * *                   *
-bash-3.2$
The pattern above resembles the letter Q, here's uppercase R:
-bash-3.2$ javac R.java
-bash-3.2$ java R 12
* * * * * * * * * * * *
*                     *
*                     *
*                     *
*                     *
*                     *
* * * * * * * * * * * *
*             *
*               *
*                 *
*                   *
*                     *
-bash-3.2$ java R 23
* * * * * * * * * * * * * * * * * * * * * * *
*                                           *
*                                           *
*                                           *
*                                           *
*                                           *
*                                           *
*                                           *
*                                           *
*                                           *
*                                           *
* * * * * * * * * * * * * * * * * * * * * * *
*                       *
*                         *
*                           *
*                             *
*                               *
*                                 *
*                                   *
*                                     *
*                                       *
*                                         *
*                                           *
-bash-3.2$ java R 8
* * * * * * * *
*             *
*             *
*             *
* * * * * * * *
*         *
*           *
*             *
-bash-3.2$
Here's how I draw uppercase Z as a scalable pattern:
-bash-3.2$ java Z 12
* * * * * * * * * * * *
                    *
                  *
                *
              *
            *
      * * * * * * *
        *
      *
    *
  *
* * * * * * * * * * * *
-bash-3.2$ java Z 23
* * * * * * * * * * * * * * * * * * * * * * *
                                          *
                                        *
                                      *
                                    *
                                  *
                                *
                              *
                            *
                          *
                        *
          * * * * * * * * * * * * *
                    *
                  *
                *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * * * * * * * * * * * * * * * *
-bash-3.2$ java Z 9
* * * * * * * * *
              *
            *
          *
    * * * * *
      *
    *
  *
* * * * * * * * *
-bash-3.2$

--

I might add here a few more examples that rely on what you learned (practiced with) during challenges.