<repo_name>jayvpp/FAUSpringClasses2017<file_sep>/Object Oriented Design/Homework/src/q5/PrimeFactorTest.java
package q5;

import java.util.ArrayList;
import java.util.Scanner;
/**
 * Created by <NAME> on 1/11/2017.
 */
public class PrimeFactorTest {

    public  static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("Enter the number you want to factorize, number should be greater than 1.");
            int number = scanner.nextInt();
            PrimeFactorizer primeFactorizer = new PrimeFactorizer(number);
            System.out.print(primeFactorizer);
        }
        catch (IllegalArgumentException iae)
        {
            System.out.println(iae.toString());
        }
    }
}
<file_sep>/Object Oriented Design/Homework/src/q5/PrimeFactorizer.java
package q5;

import java.util.ArrayList;

/**
 * Created by jason on 1/11/2017.
 */
public class PrimeFactorizer {

    private final int n;
    private String factorizationRepresentation;
    boolean computedBefore = false;
    private ArrayList<Integer> primes, exponents;

    /**
     * Constuctor for PrimeFactorizaer
     * Initialize the object with value n
     * @param n N has to be greater than 1, otherwise we will throw IllegalArgumentException
     */
    public PrimeFactorizer(int n) {
        if (n <= 1) {
            throw new IllegalArgumentException("N has to be greater than 1");
        } else
        {
            this.n = n;
            primes = new ArrayList<>();
            exponents = new ArrayList<>();
        }
    }

    /**
     * Getter, return the target number we want to factorize
     * @return
     */
    public int getN() {
        return n;
    }

    /***
     * Compute Factorization. We do not re-compute if it was calculated before
     */
    public void compute() {
        if(computedBefore) return;

        if(n == 1) {System.out.println("No possible decomposition, 1 is neither a prime number nor a composite number."); return;}

        int factorCandidate = 1;
        int currentFactor = n;

        do{
            currentFactor = computePrimeFactor(currentFactor, factorCandidate);
            factorCandidate++;
        }while(currentFactor != 1);

        computedBefore = true;
        generateFactorizationString();
    }

    //This Api function is weard
    /*public void getFactorAndExponents(int n, ArrayList<Integer> primes, ArrayList<Integer> exponents)
    {
        PrimeFactorizer factorizer = new PrimeFactorizer(n);
        factorizer.compute();
        primes =  factorizer.primes;
        exponents = factorizer.exponents;
    }*/

    /***
     *This function is for internal use of the clase, no entry point for public access.
     * @param n number you want to compute the maximun prime factor using prime k.
     * @param k number used as a base
     * @return
     */
    private int computePrimeFactor(int n, int k) {
        if (k == 1) return n;
        int count = 0;
        while (n % k == 0) {
            count++;
            n /= k;
        }
        if (count == 0) return n;
        primes.add(k);
        exponents.add(count);
        return n;
    }

    /***
     * Generate the nice factorization string base on the prime factorization generated using method
     * compute.
     */
    private void generateFactorizationString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(n + " = ");

       for(int i= 0; i < primes.size(); i++)
       {
           int fact = primes.get(i);
           int exponent = exponents.get(i);
           if(exponent == 1 )
               stringBuilder.append(fact);
           else
               stringBuilder.append(fact + "^" + exponent);
           if(i < primes.size()-1) stringBuilder.append("*");
       }

       stringBuilder.append("\n");
       factorizationRepresentation = stringBuilder.toString();
    }

    /***
     * @return The string representing the factorization of the N.
     * If we previusly calculate the value, do not re-compute, we are caching the
     * value
     */
    public String toString()
    {
        if(!computedBefore)
            compute();
        return factorizationRepresentation;
    }


}
<file_sep>/Object Oriented Design/Homework/src/q1/Fib.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package q1;

/**
 *
 * @author <NAME>
 */
public class Fib {

    private final int f0, f1;

    /**
     * This Class represents a Fib function, this class encapsulate the values for Fib(0) and Fib(1)
     * @param f0 Value of Fib(0)
     * @param f1 Value of Fib(1)
     */
    public Fib(int f0, int f1) {
        this.f0 = f0;
        this.f1 = f1;
    }

    /**
     *Iterative solution, O(n).
     * @param n Index of Fibonacci number you want to calculate.
     * @return  Returns the nth Fibonacci number using iterative algorithms.
     */
    public int f(int n) {
        
        if (n < 0) throw new NumberFormatException("Value of n must be >= 0");   
        if (n == 0) return f0;
        if (n == 1) return f1; 
        
        int a = f0;
        int b = f1;
        int fib = 0;
        
        for (int i = 2; i <= n; i++) {
            fib = a + b;
            a = b;
            b = fib;
        }

        return fib;
    }

    /**
     * Recursive solution, O(2^n) which is very bad. Can be improved using memorization or other dynamic programming technique
     * @param n Index of Fibonacci number you want to calculate.
     * @return  Returns the nth Fibonacci number using iterative algorithms.
     */
    public int fRec(int n) {
        if (n < 0) throw new NumberFormatException("Value of n must be >= 0");
        if (n == 0) return f0; 
        if (n == 1) return f1; 
        return fRec(n - 1) + fRec(n - 2);
    }

}
