Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.21 KB | None | 0 0
  1. import java.util.*;
  2. /**
  3. * Write a description of class ComplexNumber here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. public class ComplexNumber
  9. {
  10. /*
  11. * OK - I strated this one for you, but feel free to change instance variable and the constructor is you want.
  12. *
  13. *
  14. * Once again, all calcuulations are in radians unless otherwise stated
  15. *
  16. * all values with 0.05 of zero may be assumed to be zero
  17. */
  18. private double a; // real part
  19. private double b; // imaginary part
  20.  
  21. /**
  22. * Constructor for objects of class ComplexNumber given the complex number in the form: a + bi
  23. *
  24. * a = real part
  25. * b = imaginary part
  26. */
  27. public ComplexNumber(double realPart, double imaginaryPart)
  28. {
  29. a = realPart;
  30. b = imaginaryPart;
  31. }
  32.  
  33. /**
  34. * Constructor for objects of class ComplexNumber given the number in trig form: z = r(cos(t) + i sin(t))
  35. *
  36. * trigForm[0] = magnitude (r)
  37. * trigForm[1] = angle (t)
  38. * note; r may be negative, but t >=0
  39. */
  40. public ComplexNumber(double[] trigForm)
  41. {
  42. a = trigForm[0] * Math.cos(trigForm[1]);
  43. b = trigForm[0] * Math.sin(trigForm[1]);
  44. }
  45.  
  46. /**
  47. * @return the real part of the complex number
  48. */
  49. public double getRealPart()
  50. {
  51. return a;
  52. }
  53.  
  54. /**
  55. * @return the imaginary part of the complex number
  56. */
  57. public double getImaginaryPart()
  58. {
  59. return b;
  60. }
  61.  
  62. /**
  63. * set the real part of the complex number to x
  64. */
  65. public void setRealPart(double x)
  66. {
  67. a = x;
  68. }
  69.  
  70. /**
  71. * set the imaginary part of the complex number to y
  72. */
  73. public void setImaginaryPart(double y)
  74. {
  75. b = y;
  76. }
  77.  
  78. /*
  79. * converts the current complex number (this) in a + bi form to its trig form equivalence a + bi = |z|(cos(t) + i sin(t))
  80. *
  81. * @retuns a two item array with arr[0] = magnitude if complex number (z)
  82. * and arr[1] = the angle (t)
  83. *
  84. * Yes, I think the only difficult part will be to determine which quadrant (to add Math.PI || !)
  85. */
  86. public double[] convertToTrigForm()
  87. {
  88. double[] ans = new double[2];
  89. ans[0] = Math.sqrt(a * a + b * b);
  90. ans[1] = Math.atan(Math.abs(b / a));
  91. if (a < 0 && b > 0) // 2 quad
  92. {
  93. ans[1] = Math.PI - ans[1];
  94. }
  95. else if (a < 0 && b < 0) // 3 quad
  96. {
  97. ans[1] = Math.PI + ans[1];
  98. }
  99. else if (a > 0 && b < 0) // 4 quad
  100. {
  101. ans[1] = 2 * Math.PI - ans[1];
  102. }
  103. return ans;
  104. }
  105.  
  106. /*
  107. * converts the current complex number (this) in a + bi form to its trig form equivalence a + bi = |z|(cos(t) + i sin(t))
  108. * and raises it to an integer (n) power (think De Moivre formula)
  109. *
  110. * @retuns a two item array with arr[0] = magnitude if complex number (z)
  111. * and arr[1] = the angle (t), 0 <= t < Math.PI
  112. *
  113. */
  114. public double[] pow(int n)
  115. {
  116. double[] ans = convertToTrigForm();
  117. ans[0] = Math.pow(ans[0], n);
  118. ans[1] *= n;
  119. while (ans[1] - Math.PI * 2 >= -.0005)
  120. {
  121. ans[1] -= 2*Math.PI;
  122. }
  123. return ans;
  124. }
  125.  
  126. /*
  127. * converts the current complex number (this) in a + bi form to its trig form equivalence a + bi = |z|(cos(t) + i sin(t))
  128. * and returns all n-th (n an integer) roots (once again, think De Moivre formula)
  129. *
  130. * @returns an ArrayList of Complex Numbers
  131. *
  132. * note: the ArrayList should contain n Complex Number
  133. *
  134. */
  135. public ArrayList<ComplexNumber> nthRoot(int n)
  136. {
  137. ArrayList<ComplexNumber> ans = new ArrayList<ComplexNumber>();
  138. double step = 2*Math.PI / n;
  139. double[] curr = convertToTrigForm();
  140. double r = Math.pow(curr[0], 1.0 / n);
  141. double angle = curr[1] / n;
  142. for (int i = 0; i < n; i ++)
  143. {
  144. ans.add(new ComplexNumber(new double[] { r, angle }));
  145. angle += step;
  146. }
  147. return ans;
  148. }
  149.  
  150.  
  151. /*
  152. * @returns true if the real and imaginary values are close enough (say within 0.02)
  153. *
  154. * note: the ArrayList should contain n Complex Number
  155. *
  156. */
  157. public boolean equals(Object obj)
  158. {
  159. ComplexNumber cn = (ComplexNumber)obj;
  160. return Math.abs(cn.a - a) <= .02 && Math.abs(cn.b - b) <= .02;
  161. }
  162. }
  163.  
  164.  
  165.  
  166. /*
  167. * review for IB Math
  168. */
  169.  
  170. public class MiscMathTopics
  171. {
  172. /* no constructor needed */
  173.  
  174. /*
  175. * Annual Compound Interest, A = p(1+ r)^numYears
  176. * You invest $500 in an account that pays 3% interest compounded annually. Find the balance after 2 years.
  177. *
  178. * amount = initial investment (p)
  179. * numYears = the number of years the money is invested
  180. * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
  181. *
  182. */
  183. public static double calculateAnnualCompoundInterest(double amount, int numYears, double rate)
  184. {
  185. return amount * Math.pow(1 + rate, numYears);
  186. }
  187.  
  188. /*
  189. * compound interest // you find the formula
  190. * 1) You invest $100 in an account that pays 10% interest compounded annually.
  191. * Find the balance after 1 year.
  192. * 2) You invest $100 in an account that pays 10% interest compounded semiannually.
  193. * Find the balance after 1 year.
  194. *
  195. * amount = initial investment
  196. * num = number of compunding per year. annuanlly = once a year
  197. * semiannually = twice a year
  198. * numYears = the number of years the money is invested
  199. * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
  200. *
  201. */
  202. public static double calculateCompoundInterest(double amount, int num, int numYears, double rate)
  203. {
  204. return amount * Math.pow(1 + rate / num, num * numYears);
  205. }
  206.  
  207. /*
  208. * Applications of exponential and log equations
  209. *
  210. * The half-life of carbon-11 is 20 minutes.
  211. * How much long will it take to 600 g of carbon-11 to decay to 30 g?
  212. *
  213. * halfLife = half life of given element
  214. * iAmount = starting amount of given element
  215. * duration = the time spent waiting for the given element
  216. *
  217. * units will both iAmount and fAmount will always be the same
  218. *
  219. * the units of the returned value will be asume to be the same as halfLife
  220. *
  221. */
  222. public static double calculateHalfLifeAmount(double halfLife, double iAmount, double duration)
  223. {
  224. return iAmount * Math.pow(.5, duration / halfLife);
  225. }
  226.  
  227. /*
  228. * Applications of exponential and log equations
  229. *
  230. * 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?
  231. *
  232. * halfLife = half life of given element
  233. * iAmount = starting amount of given element
  234. * fAmount = final amount of given element
  235. *
  236. * units will both iAmount and fAmount will always be the same
  237. *
  238. * the units of the returned value will be asume to be the same as halfLife
  239. *
  240. */
  241. public static double calculateHalfLifeTime(double halfLife, double iAmount, double fAmount)
  242. {
  243. return halfLife * Math.log(fAmount / iAmount) / Math.log(.5);
  244. }
  245.  
  246. /*
  247. *
  248. *
  249. * I. Continuous Compounding
  250. * 1) Suppose that $1000 is invested at 7% interest compounded continuously.
  251. * How much money would be in the bank after 5 years?
  252. *
  253. *
  254. * amount = initial investment
  255. * numYears = the number of years the money is invested
  256. * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
  257. *
  258. */
  259. public static double calculateContinuousCompounding(double amount, int numYears, double rate)
  260. {
  261. return amount * Math.pow(Math.E, rate * numYears);
  262. }
  263.  
  264. /*
  265. * II. Continuous Compounding
  266. * 2) How long will it take to double an amount at 7%?}
  267. *
  268. * rate = interest rate paid. Given as a decimal, that is if rate = .05, the interest is 5%
  269. *
  270. */
  271. public static double calculateContinuousCompounding(double rate)
  272. {
  273. return Math.log(2) / rate;
  274. }
  275. }
  276.  
  277.  
  278. /*
  279. * review for IB Math
  280. */
  281.  
  282. public class Triangle
  283. {
  284. double sideA;
  285. double sideB;
  286. double sideC;
  287. double angA;//btw B & C
  288. double angB;//btw A & C
  289. double angC;//btw A & B
  290. int numTri = 0;
  291.  
  292. public Triangle()
  293. {
  294.  
  295. }
  296.  
  297. /*
  298. * this method will only be invoke if getNumTriangles() == 2
  299.  
  300. * In the case there are two triangles, this method returns the second (obtuse)
  301. * Triangle with ALL sides and Angles properly initialized
  302. *
  303. * use the orignal data (hint: setSSA)
  304. *
  305. */
  306. public Triangle getSecondTriangle()
  307. {
  308. Triangle ans = new Triangle();
  309. ans.setSideA(sideA);
  310. ans.setSideB(sideB);
  311. ans.setAngleA(angA);
  312.  
  313. ans.setAngleB(Math.PI - angB);
  314. ans.setAngleC(Math.PI - ans.angB - ans.angA);
  315. ans.sideC = Math.sqrt( ans.sideA*ans.sideA + ans.sideB*ans.sideB - 2*ans.sideA*ans.sideB*Math.cos(ans.angC) );
  316.  
  317. if (!ans.checkSides() || ! ans.checkAngles())
  318. {
  319. return null;
  320. }
  321. return ans;
  322. }
  323.  
  324. /*
  325. * this method may be invoke at any time
  326. *
  327. * hint: you might want to calculate once, store it and retrive it when asked for
  328. *
  329. * I make this suggestion because after the missing part of the triangle are calculated
  330. * you may no longer have the knowledge of what values were the given values
  331. *
  332. * I am not saying you have to, I am just saying to think about it first
  333. */
  334. public int getNumTriangles()
  335. {
  336. return numTri;
  337. }
  338.  
  339. /*
  340. * calculates all remaining angles and sides for this triangle.
  341. * given two angles (angle A and Angle B) and non-included side (side a)
  342. *
  343. * return true if successful
  344. *
  345. * returns false if unsuccessful (not sure how this method would be unsuccessful
  346. * but might be unsuccessful in other similar methods
  347. *
  348. */
  349. public boolean setAAS(double aA, double aB, double sA)
  350. {
  351. numTri = 1;
  352. angA = aA;
  353. angB = aB;
  354. sideA = sA;
  355.  
  356. angC = Math.PI - angA - angB;
  357. sideC = sideA * Math.sin(angC) / Math.sin(angA);
  358. sideB = sideA * Math.sin(angB) / Math.sin(angA);
  359.  
  360. return checkAngles() && checkSides();
  361. }
  362.  
  363. /*
  364. * calculates all remaining angles and sides for this triangle.
  365. * given two angles (angle A and Angle B) and included side (side C)
  366. *
  367. * return true if successful
  368. *
  369. * returns false if unsuccessful (not sure how this method would be unsuccessful
  370. * but might be unsuccessful in other similar methods
  371. *
  372. */
  373. public boolean setASA(double aA, double sC, double aB)
  374. {
  375. numTri = 1;
  376. angA = aA;
  377. sideC = sC;
  378. angB = aB;
  379.  
  380. angC = Math.PI - angA - angB;
  381. sideB = sideC * Math.sin(angB) / Math.sin(angC);
  382. sideA = sideC * Math.sin(angA) / Math.sin(angC);
  383.  
  384. return checkAngles() && checkSides();
  385. }
  386.  
  387. /*
  388. * calculates all remaining angles and sides for this triangle.
  389. * given two sides (side A and side B) and included angle (side C)
  390. *
  391. * This triangle will never be an obtuse triangle
  392. *
  393. * return true if successful
  394. *
  395. * returns false if unsuccessful (not sure how this method would be unsuccessful
  396. * but might be unsuccessful in other similar methods
  397. *
  398. */
  399. public boolean setSAS(double sA, double aC, double sB)
  400. {
  401. numTri = 1;
  402. sideA = sA;
  403. angC = aC;
  404. sideB = sB;
  405.  
  406. sideC = Math.sqrt( sideA*sideA + sideB*sideB - 2*sideA*sideB*Math.cos(angC) );
  407. angA = Math.acos( ( sideC*sideC + sideB*sideB - sideA*sideA ) / ( 2*sideC*sideB ) );
  408. angB = Math.PI - angC - angA;
  409.  
  410. return checkAngles() && checkSides();
  411. }
  412.  
  413. /*
  414. * calculates all three angles for this triangle.
  415. * given all three sides
  416. *
  417. * return true if successful
  418. *
  419. * returns false if unsuccessful
  420. * yes it is possible that no triangle may exist with the given sides
  421. *
  422. */
  423. public boolean setSSS(double sA, double sB, double sC)
  424. {
  425. numTri = 1;
  426. sideA = sA;
  427. sideB = sB;
  428. sideC = sC;
  429.  
  430. angA = Math.acos( ( sideC*sideC + sideB*sideB - sideA*sideA ) / ( 2*sideC*sideB ) );
  431. angB = Math.acos( ( sideC*sideC + sideA*sideA - sideB*sideB ) / ( 2*sideC*sideA ) );
  432. angC = Math.PI - angA - angB;
  433.  
  434. return checkAngles() && checkSides();
  435. }
  436.  
  437. /*
  438. * calculates all remaining angles and sides for this triangle.
  439. * given two sides (side A and side B) and non-included angle (side C)
  440. *
  441. * return true if successful
  442. *
  443. * returns false if unsuccessful
  444. * yes it is possible that no triangle may exist with the given values
  445. * or better yet, two triangles may exist
  446. *
  447. * it two triangles exist, find the triangle with acute angles
  448. * and return true
  449. *
  450. */
  451. public boolean setSSA(double sA, double sB, double aA)
  452. {
  453. sideA = sA;
  454. sideB = sB;
  455. angA = aA;
  456.  
  457. double temp = sideB / sideA * Math.sin(angA);
  458. if (Math.abs(temp) > 1)
  459. {
  460. numTri = 0;
  461. return false;
  462. }
  463. angB = Math.asin(temp);
  464. angC = Math.PI - angB - angA;
  465. sideC = Math.sqrt( sideA*sideA + sideB*sideB - 2*sideA*sideB*Math.cos(angC) );
  466.  
  467. if (getSecondTriangle() == null)
  468. {
  469. numTri = 1;
  470. }
  471. else
  472. {
  473. numTri = 2;
  474. }
  475.  
  476. return checkAngles() && checkSides();
  477. }
  478.  
  479. public void setAngleA(double a)
  480. {
  481. angA = a;
  482. }
  483.  
  484. public void setAngleB(double b)
  485. {
  486. angB = b;
  487. }
  488.  
  489. public void setAngleC(double c)
  490. {
  491. angC = c;
  492. }
  493.  
  494. public double getAngleA()
  495. {
  496. return angA;
  497. }
  498.  
  499. public double getAngleB()
  500. {
  501. return angB;
  502. }
  503.  
  504. public double getAngleC()
  505. {
  506. return angC;
  507. }
  508.  
  509. public void setSideA(double a)
  510. {
  511. sideA = a;
  512. }
  513.  
  514. public void setSideB(double b)
  515. {
  516. sideB = b;
  517. }
  518.  
  519. public void setSideC(double c)
  520. {
  521. sideC = c;
  522. }
  523.  
  524. public double getSideA()
  525. {
  526. return sideA;
  527. }
  528.  
  529. public double getSideB()
  530. {
  531. return sideB;
  532. }
  533.  
  534. public double getSideC()
  535. {
  536. return sideC;
  537. }
  538.  
  539. public double getPerimeter()
  540. {
  541. return sideA + sideB + sideC;
  542. }
  543.  
  544. public double getArea()
  545. {
  546. double s = getPerimeter() / 2;
  547. return Math.sqrt( s*( s - sideA )*( s - sideB )*( s - sideC ) );
  548. }
  549.  
  550. public boolean checkAngles()
  551. {
  552. return angA > 0 && angB > 0 && angC > 0 && Math.abs(angA + angB + angC - Math.PI) <= .0 || (numTri = 0) != 0;
  553. }
  554.  
  555. public boolean checkSides()
  556. {
  557. if (sideA + sideB <= sideC)
  558. {
  559. return (numTri = 0) != 0;
  560. }
  561. if (sideA + sideC <= sideB)
  562. {
  563. return (numTri = 0) != 0;
  564. }
  565. if (sideB + sideC <= sideA)
  566. {
  567. return (numTri = 0) != 0;
  568. }
  569. return sideA > 0 && sideB > 0 && sideC > 0 || (numTri = 0) != 0;
  570. }
  571. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement