package lec04math;

import processing.core.PApplet;

public class Lec04Math extends PApplet {

	// DIRECTIONS: 
	// 1. UNCOMMENT ONE OF THE LINES IN setup()
	// 2. RUN THE PROGRAM USING THE PLAY BUTTON
	// 3. CHECK THE CONSOLE WINDOW FOR THE RESULT
	// 4. RECOMMENT THE LINE IN setup()
	// 5. REPEAT 1-4 FOR A DIFFERENT LINE
	
	public void setup() {
		// p1();
		// p2();
		// p3();
		// p4();
		// p5();
		// p6();
		// p7();
		// p8();
		// p9();
		// fp1();
		// fp2(-5); // change the value from 20 to some other integer too
		// fp3(); // you will need to write some additional code for this one
	}

	public void p1() {
		// Problem 1
		int x = 5;
		int y = 9;
		int xy = x / y;
		int yx = y / x;
		System.out.println("xy: " + xy);
		System.out.println("yx: " + yx);
	}

	public void p2() {
		// Problem 2
		int x = 5;
		int y = 9;
		int xy = x % y;
		int yx = y % x;
		System.out.println("xy: " + xy);
		System.out.println("yx: " + yx);
	}

	public void p3() {
		// Problem 3
		int x = 5;
		double a = 2;
		double fax = a / x;
		double fxa = x / a;
		System.out.println("fax: " + fax);
		System.out.println("fxa: " + fxa);
	}

	public void p4() {
		// Problem 4
		// Recall from class that the code cannot be run
		// Therefore, all of the code is commented out
		// take the comment lines out to see the error

		/*
		int x = 5;
		double a = 2;
		int iax = a / x;
		int ixa = x / a;
		System.out.println("iax: " + iax);
		System.out.println("ixa: " + ixa);
		*/
		
		System.out.println("See the comments in the p4() method.");
	}

	public void p5() {
		// Problem 5
		int x = 5;
		double a = 2;
		int iax = (int) (a / x);
		int ixa = (int) (x / a);
		System.out.println("iax: " + iax);
		System.out.println("ixa: " + ixa);
	}

	public void p6() {
		// Problem 6
		int x = 5;
		int y = 2;
		double fxy = y / x;
		double fyx = x / y;
		System.out.println("fxy: " + fxy);
		System.out.println("fyx: " + fyx);
	}

	public void p7() {
		// Problem 7
		int x = 5;
		int y = 2;
		double fxy = y / (double) x;
		double fyx = x / (double) y;
		System.out.println("fxy: " + fxy);
		System.out.println("fyx: " + fyx);
	}

	public void p8() {
		// Problem 8
		int x = 5;
		double a = 2.2;
		x = x * x;
		a = a + a;
		System.out.println("x: " + x);
		System.out.println("a: " + a);
	}

	public void p9() {
		// Problem 9
		int x = 5;
		int y = 3;
		double d1 = (double) (x / y);
		double d2 = x / (double) y;
		System.out.println("d1: " + d1);
		System.out.println("d2: " + d2);
	}
	
	public void fp1() {
		// I need to borrow $15K for a car loan
		// I have been given two offers
		// Offer 1: 0.9% APR over 3 years
		// Offer 2: 2.9% APR over 5 years
		// Let's see the monthly payment and total cost of each loan

		int myCost = 0;
		double myPmt = 0;

		// Determine Loan 1
		myPmt = monPmt(15000, 0.9, 36); // determine the monthly payment
		myCost = loanCost(myPmt, 36, 15000); // determine the cost
		myPmt = (int)myPmt; // just show the whole dollars, not cents 
		System.out.println("Loan 1: " + myPmt + "\t" + myCost);

		// repeat for Loan 2
		myPmt = monPmt(15000, 2.9, 60);
		myCost = loanCost(myPmt, 60, 15000);
		myPmt = (int)myPmt;
		System.out.println("Loan 2: " + myPmt + "\t" + myCost);
	}
	
	public double monPmt(int amt, double rate, int numPmts) {
		// step 1: convert yearly percentage to monthly rate
		rate = (rate / 100) / 12;

		// step 2: determine simple compound interest rate
		double compRate = Math.pow(1 + rate, numPmts);

		// step 3: determine monthly payment
		double monPmt = (amt * rate * compRate) / (compRate - 1);
		
		return monPmt;
	}
	
	public int loanCost(double monPmt, int numPmts, int amt) {
		int totPaid = (int)(monPmt * numPmts);
		int totCost = totPaid - amt;
		return totCost;
	}
	
	public void fp2(int c) {
		int f = toDegF(c);
		System.out.println(c + "C is " + f + "F");
	}

	public int toDegF(int degC) {
		int degF = 9 * degC; // multiply by 9
		degF = degF / 5; // divide by 5
		degF = degF + 32; // add 32
		return degF;
	}
	
	
	public void fp3() {
		// write code here to call your kg -> lb function
	}
	
	// implement the kg -> lb function here

	
	public void draw() {
		background(0,127,0);
	}
}
