Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * Write a description of class ComplexNumber here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class ComplexNumber
- {
- /*
- * OK - I strated this one for you, but feel free to change instance variable and the constructor is you want.
- *
- *
- * Once again, all calcuulations are in radians unless otherwise stated
- *
- * all values with 0.05 of zero may be assumed to be zero
- */
- private double a; // real part
- private double b; // imaginary part
- /**
- * Constructor for objects of class ComplexNumber given the complex number in the form: a + bi
- *
- * a = real part
- * b = imaginary part
- */
- public ComplexNumber(double realPart, double imaginaryPart)
- {
- a = realPart;
- b = imaginaryPart;
- }
- /**
- * Constructor for objects of class ComplexNumber given the number in trig form: z = r(cos(t) + i sin(t))
- *
- * trigForm[0] = magnitude (r)
- * trigForm[1] = angle (t)
- * note; r may be negative, but t >=0
- */
- public ComplexNumber(double[] trigForm)
- {
- a = trigForm[0] * Math.cos(trigForm[1]);
- b = trigForm[0] * Math.sin(trigForm[1]);
- }
- /**
- * @return the real part of the complex number
- */
- public double getRealPart()
- {
- return a;
- }
- /**
- * @return the imaginary part of the complex number
- */
- public double getImaginaryPart()
- {
- return b;
- }
- /**
- * set the real part of the complex number to x
- */
- public void setRealPart(double x)
- {
- a = x;
- }
- /**
- * set the imaginary part of the complex number to y
- */
- public void setImaginaryPart(double y)
- {
- b = y;
- }
- /*
- * converts the current complex number (this) in a + bi form to its trig form equivalence a + bi = |z|(cos(t) + i sin(t))
- *
- * @retuns a two item array with arr[0] = magnitude if complex number (z)
- * and arr[1] = the angle (t)
- *
- * Yes, I think the only difficult part will be to determine which quadrant (to add Math.PI || !)
- */
- public double[] convertToTrigForm()
- {
- double[] ans = new double[2];
- ans[0] = Math.sqrt(a * a + b * b);
- ans[1] = Math.atan(Math.abs(b / a));
- if (a < 0 && b > 0) // 2 quad
- {
- ans[1] = Math.PI - ans[1];
- }
- else if (a < 0 && b < 0) // 3 quad
- {
- ans[1] = Math.PI + ans[1];
- }
- else if (a > 0 && b < 0) // 4 quad
- {
- ans[1] = 2 * Math.PI - ans[1];
- }
- return ans;
- }
- /*
- * converts the current complex number (this) in a + bi form to its trig form equivalence a + bi = |z|(cos(t) + i sin(t))
- * and raises it to an integer (n) power (think De Moivre formula)
- *
- * @retuns a two item array with arr[0] = magnitude if complex number (z)
- * and arr[1] = the angle (t), 0 <= t < Math.PI
- *
- */
- public double[] pow(int n)
- {
- double[] ans = convertToTrigForm();
- ans[0] = Math.pow(ans[0], n);
- ans[1] *= n;
- while (ans[1] - Math.PI * 2 >= -.0005)
- {
- ans[1] -= 2*Math.PI;
- }
- return ans;
- }
- /*
- * converts the current complex number (this) in a + bi form to its trig form equivalence a + bi = |z|(cos(t) + i sin(t))
- * and returns all n-th (n an integer) roots (once again, think De Moivre formula)
- *
- * @returns an ArrayList of Complex Numbers
- *
- * note: the ArrayList should contain n Complex Number
- *
- */
- public ArrayList<ComplexNumber> nthRoot(int n)
- {
- ArrayList<ComplexNumber> ans = new ArrayList<ComplexNumber>();
- double step = 2*Math.PI / n;
- double[] curr = convertToTrigForm();
- double r = Math.pow(curr[0], 1.0 / n);
- double angle = curr[1] / n;
- for (int i = 0; i < n; i ++)
- {
- ans.add(new ComplexNumber(new double[] { r, angle }));
- angle += step;
- }
- return ans;
- }
- /*
- * @returns true if the real and imaginary values are close enough (say within 0.02)
- *
- * note: the ArrayList should contain n Complex Number
- *
- */
- public boolean equals(Object obj)
- {
- ComplexNumber cn = (ComplexNumber)obj;
- return Math.abs(cn.a - a) <= .02 && Math.abs(cn.b - b) <= .02;
- }
- }
- /*
- * review for IB Math
- */
- public class MiscMathTopics
- {
- /* no constructor needed */
- /*
- * Annual Compound Interest, A = p(1+ r)^numYears
- * You invest $500 in an account that pays 3% interest compounded annually. Find the balance after 2 years.
- *
- * amount = initial investment (p)
- * numYears = the number of years the money is invested
- * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
- *
- */
- public static double calculateAnnualCompoundInterest(double amount, int numYears, double rate)
- {
- return amount * Math.pow(1 + rate, numYears);
- }
- /*
- * compound interest // you find the formula
- * 1) You invest $100 in an account that pays 10% interest compounded annually.
- * Find the balance after 1 year.
- * 2) You invest $100 in an account that pays 10% interest compounded semiannually.
- * Find the balance after 1 year.
- *
- * amount = initial investment
- * num = number of compunding per year. annuanlly = once a year
- * semiannually = twice a year
- * numYears = the number of years the money is invested
- * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
- *
- */
- public static double calculateCompoundInterest(double amount, int num, int numYears, double rate)
- {
- return amount * Math.pow(1 + rate / num, num * numYears);
- }
- /*
- * Applications of exponential and log equations
- *
- * The half-life of carbon-11 is 20 minutes.
- * How much long will it take to 600 g of carbon-11 to decay to 30 g?
- *
- * halfLife = half life of given element
- * iAmount = starting amount of given element
- * duration = the time spent waiting for the given element
- *
- * units will both iAmount and fAmount will always be the same
- *
- * the units of the returned value will be asume to be the same as halfLife
- *
- */
- public static double calculateHalfLifeAmount(double halfLife, double iAmount, double duration)
- {
- return iAmount * Math.pow(.5, duration / halfLife);
- }
- /*
- * Applications of exponential and log equations
- *
- * The half-life of carbon-11 is 20 minutes. How long will it take to 600 g of carbon-11 to decay to 30 g?
- *
- * halfLife = half life of given element
- * iAmount = starting amount of given element
- * fAmount = final amount of given element
- *
- * units will both iAmount and fAmount will always be the same
- *
- * the units of the returned value will be asume to be the same as halfLife
- *
- */
- public static double calculateHalfLifeTime(double halfLife, double iAmount, double fAmount)
- {
- return halfLife * Math.log(fAmount / iAmount) / Math.log(.5);
- }
- /*
- *
- *
- * I. Continuous Compounding
- * 1) Suppose that $1000 is invested at 7% interest compounded continuously.
- * How much money would be in the bank after 5 years?
- *
- *
- * amount = initial investment
- * numYears = the number of years the money is invested
- * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
- *
- */
- public static double calculateContinuousCompounding(double amount, int numYears, double rate)
- {
- return amount * Math.pow(Math.E, rate * numYears);
- }
- /*
- * II. Continuous Compounding
- * 2) How long will it take to double an amount at 7%?}
- *
- * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
- *
- */
- public static double calculateContinuousCompounding(double rate)
- {
- return Math.log(2) / rate;
- }
- }
- /*
- * review for IB Math
- */
- public class Triangle
- {
- double sideA;
- double sideB;
- double sideC;
- double angA;//btw B & C
- double angB;//btw A & C
- double angC;//btw A & B
- int numTri = 0;
- public Triangle()
- {
- }
- /*
- * this method will only be invoke if getNumTriangles() == 2
- * In the case there are two triangles, this method returns the second (obtuse)
- * Triangle with ALL sides and Angles properly initialized
- *
- * use the orignal data (hint: setSSA)
- *
- */
- public Triangle getSecondTriangle()
- {
- Triangle ans = new Triangle();
- ans.setSideA(sideA);
- ans.setSideB(sideB);
- ans.setAngleA(angA);
- ans.setAngleB(Math.PI - angB);
- ans.setAngleC(Math.PI - ans.angB - ans.angA);
- ans.sideC = Math.sqrt( ans.sideA*ans.sideA + ans.sideB*ans.sideB - 2*ans.sideA*ans.sideB*Math.cos(ans.angC) );
- if (!ans.checkSides() || ! ans.checkAngles())
- {
- return null;
- }
- return ans;
- }
- /*
- * this method may be invoke at any time
- *
- * hint: you might want to calculate once, store it and retrive it when asked for
- *
- * I make this suggestion because after the missing part of the triangle are calculated
- * you may no longer have the knowledge of what values were the given values
- *
- * I am not saying you have to, I am just saying to think about it first
- */
- public int getNumTriangles()
- {
- return numTri;
- }
- /*
- * calculates all remaining angles and sides for this triangle.
- * given two angles (angle A and Angle B) and non-included side (side a)
- *
- * return true if successful
- *
- * returns false if unsuccessful (not sure how this method would be unsuccessful
- * but might be unsuccessful in other similar methods
- *
- */
- public boolean setAAS(double aA, double aB, double sA)
- {
- numTri = 1;
- angA = aA;
- angB = aB;
- sideA = sA;
- angC = Math.PI - angA - angB;
- sideC = sideA * Math.sin(angC) / Math.sin(angA);
- sideB = sideA * Math.sin(angB) / Math.sin(angA);
- return checkAngles() && checkSides();
- }
- /*
- * calculates all remaining angles and sides for this triangle.
- * given two angles (angle A and Angle B) and included side (side C)
- *
- * return true if successful
- *
- * returns false if unsuccessful (not sure how this method would be unsuccessful
- * but might be unsuccessful in other similar methods
- *
- */
- public boolean setASA(double aA, double sC, double aB)
- {
- numTri = 1;
- angA = aA;
- sideC = sC;
- angB = aB;
- angC = Math.PI - angA - angB;
- sideB = sideC * Math.sin(angB) / Math.sin(angC);
- sideA = sideC * Math.sin(angA) / Math.sin(angC);
- return checkAngles() && checkSides();
- }
- /*
- * calculates all remaining angles and sides for this triangle.
- * given two sides (side A and side B) and included angle (side C)
- *
- * This triangle will never be an obtuse triangle
- *
- * return true if successful
- *
- * returns false if unsuccessful (not sure how this method would be unsuccessful
- * but might be unsuccessful in other similar methods
- *
- */
- public boolean setSAS(double sA, double aC, double sB)
- {
- numTri = 1;
- sideA = sA;
- angC = aC;
- sideB = sB;
- sideC = Math.sqrt( sideA*sideA + sideB*sideB - 2*sideA*sideB*Math.cos(angC) );
- angA = Math.acos( ( sideC*sideC + sideB*sideB - sideA*sideA ) / ( 2*sideC*sideB ) );
- angB = Math.PI - angC - angA;
- return checkAngles() && checkSides();
- }
- /*
- * calculates all three angles for this triangle.
- * given all three sides
- *
- * return true if successful
- *
- * returns false if unsuccessful
- * yes it is possible that no triangle may exist with the given sides
- *
- */
- public boolean setSSS(double sA, double sB, double sC)
- {
- numTri = 1;
- sideA = sA;
- sideB = sB;
- sideC = sC;
- angA = Math.acos( ( sideC*sideC + sideB*sideB - sideA*sideA ) / ( 2*sideC*sideB ) );
- angB = Math.acos( ( sideC*sideC + sideA*sideA - sideB*sideB ) / ( 2*sideC*sideA ) );
- angC = Math.PI - angA - angB;
- return checkAngles() && checkSides();
- }
- /*
- * calculates all remaining angles and sides for this triangle.
- * given two sides (side A and side B) and non-included angle (side C)
- *
- * return true if successful
- *
- * returns false if unsuccessful
- * yes it is possible that no triangle may exist with the given values
- * or better yet, two triangles may exist
- *
- * it two triangles exist, find the triangle with acute angles
- * and return true
- *
- */
- public boolean setSSA(double sA, double sB, double aA)
- {
- sideA = sA;
- sideB = sB;
- angA = aA;
- double temp = sideB / sideA * Math.sin(angA);
- if (Math.abs(temp) > 1)
- {
- numTri = 0;
- return false;
- }
- angB = Math.asin(temp);
- angC = Math.PI - angB - angA;
- sideC = Math.sqrt( sideA*sideA + sideB*sideB - 2*sideA*sideB*Math.cos(angC) );
- if (getSecondTriangle() == null)
- {
- numTri = 1;
- }
- else
- {
- numTri = 2;
- }
- return checkAngles() && checkSides();
- }
- public void setAngleA(double a)
- {
- angA = a;
- }
- public void setAngleB(double b)
- {
- angB = b;
- }
- public void setAngleC(double c)
- {
- angC = c;
- }
- public double getAngleA()
- {
- return angA;
- }
- public double getAngleB()
- {
- return angB;
- }
- public double getAngleC()
- {
- return angC;
- }
- public void setSideA(double a)
- {
- sideA = a;
- }
- public void setSideB(double b)
- {
- sideB = b;
- }
- public void setSideC(double c)
- {
- sideC = c;
- }
- public double getSideA()
- {
- return sideA;
- }
- public double getSideB()
- {
- return sideB;
- }
- public double getSideC()
- {
- return sideC;
- }
- public double getPerimeter()
- {
- return sideA + sideB + sideC;
- }
- public double getArea()
- {
- double s = getPerimeter() / 2;
- return Math.sqrt( s*( s - sideA )*( s - sideB )*( s - sideC ) );
- }
- public boolean checkAngles()
- {
- return angA > 0 && angB > 0 && angC > 0 && Math.abs(angA + angB + angC - Math.PI) <= .0 || (numTri = 0) != 0;
- }
- public boolean checkSides()
- {
- if (sideA + sideB <= sideC)
- {
- return (numTri = 0) != 0;
- }
- if (sideA + sideC <= sideB)
- {
- return (numTri = 0) != 0;
- }
- if (sideB + sideC <= sideA)
- {
- return (numTri = 0) != 0;
- }
- return sideA > 0 && sideB > 0 && sideC > 0 || (numTri = 0) != 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement