Advertisement
Crenox

Quadratics Java Program

Oct 8th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.58 KB | None | 0 0
  1. // Sammy Samkough
  2. // Quadratic
  3. // Spec: Given coefficients of a quadratic (a, b, c) calculate and output the number of real roots and the roots themselves in an object-oriented program
  4.  
  5. import java.text.DecimalFormat;
  6.  
  7. public class Quadratic
  8. {
  9. private int numRealRoots;
  10. private double a, b, c, x1, x2, determinant, aos, vertexX, vertexY;
  11. private DecimalFormat fmt = new DecimalFormat("0.###");
  12. private String vertex;
  13.  
  14. /** Creates a Quadratic with instance data set to zero by default */
  15. public Quadratic()
  16. {
  17. a = 0;
  18. b = 0;
  19. c = 0;
  20. }
  21.  
  22. /** Creates a Quadratic with a, b and c values
  23. * @param a the coefficient of x squared
  24. * @param b the coefficient of x
  25. * @param c the constant */
  26. public Quadratic(Double aValue, Double bValue, Double cValue)
  27. {
  28. a = aValue;
  29. b = bValue;
  30. c = cValue;
  31. }
  32.  
  33. /** Returns the number of real roots based on the value of the determinant
  34. * @return the number of real roots */
  35. public int getNumRealRoots()
  36. {
  37. determinant = getDeterminant();
  38. if (determinant < 0)
  39. {
  40. numRealRoots = 0;
  41. }
  42. else if (determinant == 0)
  43. {
  44. numRealRoots = 1;
  45. }
  46. else if (determinant > 0)
  47. {
  48. numRealRoots = 2;
  49. }
  50.  
  51. return numRealRoots;
  52. }
  53.  
  54. /** Calculates and returns the value of the determinant
  55. * @return the value of the determinant */
  56. public double getDeterminant()
  57. {
  58. determinant = (Math.pow(b, 2) - (4 * a * c));
  59. return determinant;
  60. }
  61.  
  62. /** Uses the quadratic formula to calculate and return the positive root
  63. * @return the positive root */
  64. public double getPositiveRoot()
  65. {
  66. double posRoot = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
  67.  
  68. return posRoot;
  69. }
  70.  
  71. /** Uses the quadratic formula to calculate and determine the negative root
  72. * @return the negative root */
  73. public double getNegativeRoot()
  74. {
  75. double negRoot = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
  76.  
  77. return negRoot;
  78. }
  79.  
  80. /** Calculates the real and imaginary parts of the first root and returns as a String
  81. * Ex: -5 + 3.25i
  82. * @return one of the complex roots as a String */
  83. public String getImaginaryRoot1()
  84. {
  85. double pos = Math.sqrt(determinant);
  86. String imagRootOne = pos + "i";
  87.  
  88. return imagRootOne;
  89. }
  90.  
  91. /** Calculates the real and imaginary parts of the second root and return as a String
  92. * Ex: -5 - 3.25i
  93. * @return the other complex root of the equation as a String */
  94. public String getImaginaryRoot2()
  95. {
  96. double neg = Math.sqrt(-determinant);
  97. String imagRootTwo = neg + "i";
  98.  
  99. return imagRootTwo;
  100. }
  101.  
  102. /** Retrieves the axis of symmetry from the quadratic function */
  103. public double getAxisOfSymmetry()
  104. {
  105. aos = -b / (2 * a);
  106.  
  107. return aos;
  108. }
  109.  
  110. /** Calculates the vertex of the quadratic function */
  111. public String getVertex()
  112. {
  113. vertexX = aos;
  114. vertexY = ((a * Math.pow(aos, 2)) + (b * aos) + (c));
  115.  
  116. vertex = vertexX + ", " + vertexY;
  117.  
  118. return vertex;
  119. }
  120.  
  121. /** Builds an intelligent String that neatly displays on separate lines:
  122. * The number and kind of roots
  123. * The first root
  124. * The second root (except in the case of a double root)
  125. * return the number and type of roots along with the roots each on its own line */
  126. public String toString()
  127. {
  128. String s;
  129.  
  130. // retrieves the determinant from the getDeterminant() method
  131. determinant = getDeterminant();
  132. System.out.println("Determinant: " + determinant);
  133.  
  134. // rules for the value of the determinant
  135. if (determinant < 0)
  136. {
  137. // 0 real roots
  138. s = "There are 2 imaginary roots and " + getNumRealRoots() + " real roots, and they are: \nImaginary Root #1: " +
  139. getImaginaryRoot1() + "\n" + "Imaginary Root #2: " + getImaginaryRoot2() + "\n";
  140.  
  141. s += "\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry() + "\n";
  142. s += "The vertex of the equation is: " + getVertex() + "\n\n";
  143. }
  144. else if (determinant == 0)
  145. {
  146. // 1 real root
  147. s = "There is " + getNumRealRoots() + " real root, and it is: " + getPositiveRoot() + "\n";
  148.  
  149. s += "\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry() + "\n";
  150. s += "The vertex of the equation is: " + getVertex() + "\n\n";
  151. }
  152. else if (determinant > 0)
  153. {
  154. // 2 real roots
  155. s = "There are " + getNumRealRoots() + " real roots, and they are: \nPositive Root: " + getPositiveRoot() + "\n" +
  156. "Negative Root: " + getNegativeRoot() + "\n";
  157.  
  158. s += "\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry() + "\n";
  159. s += "The vertex of the equation is: " + getVertex() + "\n\n";
  160. }
  161. else
  162. {
  163. s = "";
  164. }
  165.  
  166. return s;
  167. }
  168. }
  169. -------------------------------------------------------------------------------------------------------------------------------
  170. // Sammy Samkough
  171. // Quadratic
  172. // Spec: Given coefficients of a quadratic (a, b, c) calculate and output the number of real roots and the roots themselves in an object-oriented program
  173.  
  174. import java.util.Scanner;
  175.  
  176. public class QuadraticClient
  177. {
  178. public static void main(String args[])
  179. {
  180. double a, b, c;
  181. Quadratic eqtn;
  182. String again;
  183. Scanner sc = new Scanner(System.in);
  184. boolean running = true;
  185.  
  186. System.out.println("Welcome to the Quadratic Program!");
  187. System.out.println("Standard Form of a Quadratic: Ax^2+Bx+C=0");
  188. System.out.println("--------------------------------------------------------------------------------");
  189.  
  190. while(running)
  191. {
  192. System.out.println("\nEnter values for A, B, and C:");
  193. System.out.print("A = ");
  194. a = sc.nextDouble();
  195. System.out.print("B = ");
  196. b = sc.nextDouble();
  197. System.out.print("C = ");
  198. c = sc.nextDouble();
  199.  
  200. eqtn = new Quadratic(a, b, c);
  201.  
  202. System.out.println(eqtn.toString());
  203.  
  204. System.out.print("Again? (Y or N): ");
  205. again = sc.next();
  206. if (again.equalsIgnoreCase("y"))
  207. {
  208. // program will continue
  209. }
  210. // farewell message
  211. else if (again.equalsIgnoreCase("n"))
  212. {
  213. System.out.println("Bye bye!");
  214. running = false;
  215.  
  216. }
  217. // if they type something else besides "y" or "n"
  218. else
  219. {
  220. System.out.println("What was that? Please type in Y or N or the program will continue.");
  221. again = sc.next();
  222. }
  223. }
  224. }
  225. }
  226. /*
  227. Welcome to the Quadratic Program!
  228. Standard Form of a Quadratic: Ax^2+Bx+C=0
  229. --------------------------------------------------------------------------------
  230.  
  231.  
  232. Enter values for A, B, and C:
  233. A = 2
  234. B = 4
  235. C = 2
  236. Determinant: 0.0
  237. There is 1 real root, and it is: -4.0
  238. The axis of symmetry of the equation is: -1.0
  239. The vertex of the equation is: -1.0, 0.0
  240.  
  241. Again? (Y or N): y
  242.  
  243. Enter values for A, B, and C:
  244. A = 1
  245. B = -1
  246. C = -6
  247. Determinant: 25.0
  248. There are 2 real roots, and they are:
  249. Positive Root: 3.0
  250. Negative Root: -2.0
  251.  
  252. The axis of symmetry of the equation is: 0.5
  253. The vertex of the equation is: 0.5, -6.25
  254.  
  255. Again? (Y or N): y
  256.  
  257. Enter values for A, B, and C:
  258. A = 5
  259. B = 8
  260. C = 19
  261. Determinant: -316.0
  262. There are 2 imaginary roots and 0 real roots, and they are:
  263. Imaginary Root #1: NaNi
  264. Imaginary Root #2: 17.776388834631177i
  265.  
  266. The axis of symmetry of the equation is: -0.8
  267. The vertex of the equation is: -0.8, 15.8
  268.  
  269. Again? (Y or N): y
  270.  
  271. Enter values for A, B, and C:
  272. A = 12
  273. B = 13
  274. C = 14
  275. Determinant: -503.0
  276. There are 2 imaginary roots and 0 real roots, and they are:
  277. Imaginary Root #1: NaNi
  278. Imaginary Root #2: 22.427661492005804i
  279.  
  280. The axis of symmetry of the equation is: -0.5416666666666666
  281. The vertex of the equation is: -0.5416666666666666, 10.479166666666668
  282.  
  283. Again? (Y or N): n
  284. Bye bye!
  285. Press any key to continue . . .
  286. */
  287. -------------------------------------------------------------------------------------------------------------------------------
  288. // Sammy Samkough
  289. // QuadraticProcedural
  290. // Spec: Given coefficients of a quadratic (a, b, c) calculate and output the number of real roots and the roots themselves in a procedural program
  291.  
  292. import java.text.DecimalFormat;
  293. import java.util.Scanner;
  294.  
  295. public class QuadraticProcedural
  296. {
  297. private static int numRealRoots;
  298. private static double x1, x2, determinant, aos, vertexX, vertexY;
  299. private static DecimalFormat fmt = new DecimalFormat("0.###");
  300. private static String vertex;
  301.  
  302. /** Returns the number of real roots based on the value of the determinant
  303. * @return the number of real roots */
  304. public static int getNumRealRoots(double a, double b, double c)
  305. {
  306. determinant = getDeterminant(a, b, c);
  307. if (determinant < 0)
  308. {
  309. numRealRoots = 0;
  310. }
  311. else if (determinant == 0)
  312. {
  313. numRealRoots = 1;
  314. }
  315. else if (determinant > 0)
  316. {
  317. numRealRoots = 2;
  318. }
  319.  
  320. return numRealRoots;
  321. }
  322.  
  323. /** Calculates and returns the value of the determinant
  324. * @return the value of the determinant */
  325. public static double getDeterminant(double a, double b, double c)
  326. {
  327. determinant = (Math.pow(b, 2) - (4 * a * c));
  328.  
  329. return determinant;
  330. }
  331.  
  332. /** Uses the quadratic formula to calculate and return the positive root
  333. * @return the positive root */
  334. public static double getPositiveRoot(double a, double b, double c)
  335. {
  336. double posRoot = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
  337.  
  338. return posRoot;
  339. }
  340.  
  341. /** Uses the quadratic formula to calculate and determine the negative root
  342. * @return the negative root */
  343. public static double getNegativeRoot(double a, double b, double c)
  344. {
  345. double negRoot = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
  346.  
  347. return negRoot;
  348. }
  349.  
  350. /** Calculates the real and imaginary parts of the first root and returns as a String
  351. * Ex: -5 + 3.25i
  352. * @return one of the complex roots as a String */
  353. public static String getImaginaryRoot1(double a, double b, double c)
  354. {
  355. double pos = Math.sqrt(determinant);
  356. String imagRootOne = pos + "i";
  357.  
  358. return imagRootOne;
  359. }
  360.  
  361. /** Calculates the real and imaginary parts of the second root and return as a String
  362. * Ex: -5 - 3.25i
  363. * @return the other complex root of the equation as a String */
  364. public static String getImaginaryRoot2(double a, double b, double c)
  365. {
  366. double neg = Math.sqrt(-determinant);
  367. String imagRootTwo = neg + "i";
  368.  
  369. return imagRootTwo;
  370. }
  371.  
  372. /** Retrieves the axis of symmetry from the quadratic function */
  373. public static double getAxisOfSymmetry(double a, double b, double c)
  374. {
  375. aos = -b / (2 * a);
  376.  
  377. return aos;
  378. }
  379.  
  380. /** Calculates the vertex of the quadratic function */
  381. public static String getVertex(double a, double b, double c)
  382. {
  383. vertexX = aos;
  384. vertexY = ((a * Math.pow(aos, 2)) + (b * aos) + (c));
  385.  
  386. vertex = vertexX + ", " + vertexY;
  387.  
  388. return vertex;
  389. }
  390.  
  391. public static void main(String args[])
  392. {
  393. double a, b, c;
  394. String again;
  395. Scanner sc = new Scanner(System.in);
  396. boolean running = true;
  397.  
  398. System.out.println("Welcome to the Quadratic Program!");
  399. System.out.println("Standard Form of a Quadratic: Ax^2+Bx+C=0");
  400. System.out.println("--------------------------------------------------------------------------------");
  401.  
  402. while(running)
  403. {
  404. System.out.println("\nEnter values for A, B, and C:");
  405. System.out.print("A = ");
  406. a = sc.nextDouble();
  407. System.out.print("B = ");
  408. b = sc.nextDouble();
  409. System.out.print("C = ");
  410. c = sc.nextDouble();
  411.  
  412. // retrieves the determinant from the getDeterminant() method
  413. determinant = getDeterminant(a, b, c);
  414. System.out.println("Determinant: " + determinant);
  415.  
  416. // rules for the value of the determinant
  417. if (determinant < 0)
  418. {
  419. // 0 real roots
  420. System.out.print("There are 2 imaginary roots and " + getNumRealRoots(a, b, c) + " real roots, and they are: \nImaginary Root #1: " +
  421. getImaginaryRoot1(a, b, c) + "\n" + "Imaginary Root #2: " + getImaginaryRoot2(a, b, c) + "\n");
  422.  
  423. System.out.print("\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry(a, b, c) + "\n");
  424. System.out.print("The vertex of the equation is: " + getVertex(a, b, c) + "\n\n");
  425. }
  426. else if (determinant == 0)
  427. {
  428. // 1 real root
  429. System.out.print("There is " + getNumRealRoots(a, b, c) + " real root, and it is: " + getPositiveRoot(a, b, c) + "\n");
  430.  
  431. System.out.print("\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry(a, b, c) + "\n");
  432. System.out.print("The vertex of the equation is: " + getVertex(a, b, c) + "\n\n");
  433. }
  434. else if (determinant > 0)
  435. {
  436. // 2 real roots
  437. System.out.print("There are " + getNumRealRoots(a, b, c) + " real roots, and they are: \nPositive Root: " + getPositiveRoot(a, b, c) + "\n" +
  438. "Negative Root: " + getNegativeRoot(a, b, c) + "\n\n");
  439.  
  440. System.out.print("\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry(a, b, c) + "\n");
  441. System.out.print("The vertex of the equation is: " + getVertex(a, b, c) + "\n");
  442. }
  443. else
  444. {
  445. System.out.print("");
  446. }
  447.  
  448. System.out.print("Again? (Y or N): ");
  449. again = sc.next();
  450. if (again.equalsIgnoreCase("y"))
  451. {
  452. // program will continue
  453. }
  454. // farewell message
  455. else if (again.equalsIgnoreCase("n"))
  456. {
  457. System.out.println("Bye bye!");
  458. running = false;
  459.  
  460. }
  461. // if they type something else besides "y" or "n"
  462. else
  463. {
  464. System.out.println("What was that? Please type in Y or N or the program will continue.");
  465. again = sc.next();
  466. }
  467. }
  468. }
  469. }
  470. /*
  471. Welcome to the Quadratic Program!
  472. Standard Form of a Quadratic: Ax^2+Bx+C=0
  473. --------------------------------------------------------------------------------
  474.  
  475.  
  476. Enter values for A, B, and C:
  477. A = 2
  478. B = 4
  479. C = 2
  480. Determinant: 0.0
  481. There is 1 real root, and it is: -4.0
  482.  
  483. The axis of symmetry of the equation is: -1.0
  484. The vertex of the equation is: -1.0, 0.0
  485.  
  486. Again? (Y or N): 1
  487. What was that? Please type in Y or N or the program will continue.
  488. 4
  489.  
  490. Enter values for A, B, and C:
  491. A = 1
  492. B = -1
  493. C = -6
  494. Determinant: 25.0
  495. There are 2 real roots, and they are:
  496. Positive Root: 3.0
  497. Negative Root: -2.0
  498.  
  499.  
  500. The axis of symmetry of the equation is: 0.5
  501. The vertex of the equation is: 0.5, -6.25
  502. Again? (Y or N): y
  503.  
  504. Enter values for A, B, and C:
  505. A = 23
  506. B = 24
  507. C = 26
  508. Determinant: -1816.0
  509. There are 2 imaginary roots and 0 real roots, and they are:
  510. Imaginary Root #1: NaNi
  511. Imaginary Root #2: 42.61455150532503i
  512.  
  513. The axis of symmetry of the equation is: -0.5217391304347826
  514. The vertex of the equation is: -0.5217391304347826, 19.73913043478261
  515.  
  516. Again? (Y or N): y
  517.  
  518. Enter values for A, B, and C:
  519. A = 75
  520. B = -43
  521. C = 3
  522. Determinant: 949.0
  523. There are 2 real roots, and they are:
  524. Positive Root: 2767.719135056202
  525. Negative Root: 457.2808649437978
  526.  
  527.  
  528. The axis of symmetry of the equation is: 0.2866666666666667
  529. The vertex of the equation is: 0.2866666666666667, -3.163333333333335
  530. Again? (Y or N): n
  531. Bye bye!
  532. Press any key to continue . . .
  533. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement