Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. //QuadraticsWithMethods by Coby Boley
  2. import java.text.DecimalFormat;
  3. import javax.swing.JOptionPane;
  4.  
  5. public class QuadraticsWithMethods {
  6.  
  7. public static void main(String[] args) {
  8. JOptionPane.showMessageDialog(null, "QuadraticsWithMethods by Coby Boley");
  9. double a = inHandler("For the equation, Ax^2+Bx+C = 0, please enter the coefficient A");
  10. double b = inHandler("For the equation, Ax^2+Bx+C = 0, please enter the coefficient B");
  11. double c = inHandler("For the equation, Ax^2+Bx+C = 0, please enter the coefficient C");
  12. JOptionPane.showMessageDialog(null, "a: "+a+ "\nb: "+b+"\nc: "+c);
  13.  
  14. }
  15. public static double inHandler(String prompt){
  16. double returnValue = 0;
  17. boolean tryAgain = false;
  18. do {
  19. String s = JOptionPane.showInputDialog(prompt);
  20. try {
  21. returnValue = Double.parseDouble(s);
  22. tryAgain = false;
  23. } catch (NumberFormatException e){
  24. returnValue = Double.NaN;
  25. if (Double.isNaN(returnValue)){
  26. JOptionPane.showMessageDialog(null, "you have entered something I can't understand, please try again.");
  27. tryAgain = true;
  28. }
  29. }
  30. } while (tryAgain);
  31. return returnValue;
  32.  
  33. }
  34. public static double discriminant (double a, double b, double c) {
  35. double disc;
  36. disc = b*b - 4*a*c;
  37. return disc;
  38. }
  39.  
  40.  
  41. public static String comp(double disc, double b, double a) {
  42. String rtn;
  43. double comp = Math.sqrt(Math.abs(disc));
  44. double compA = comp/2*a;
  45. double compB = -b/2*a;
  46. compA = Double.parseDouble(new DecimalFormat("##.###").format(compA));
  47. compB = Double.parseDouble(new DecimalFormat("##.###").format(compB));
  48. if (-b == 0) {
  49. rtn = "\nThe complex roots are: " + compA + "i and -" + compA + "i";
  50. } else {
  51. rtn = "\nThe complex roots are: " + compB + "+" + compA + "i and" + compB + "-" + compA + "i";
  52. }
  53. return rtn;
  54. }
  55.  
  56. public static void root(double a,double b,double c,double disc)
  57. {
  58. //declaring roots as double
  59. double root1;
  60. double root2;
  61.  
  62. if (disc > 0)
  63.  
  64. {
  65. //real roots if two roots show
  66. root1 = (-b + Math.sqrt(disc)) / (2 * a);
  67. DecimalFormat df1 = new DecimalFormat("#.###");
  68. root1 = Double.valueOf(df1.format(root1));
  69.  
  70. root2 = (-b - Math.sqrt(disc)) / (2 * a);
  71. DecimalFormat df2 = new DecimalFormat("#.###");
  72. root2 = Double.valueOf(df2.format(root2));
  73. JOptionPane.showMessageDialog(null,"The roots are: " +root1+" and "+root2);
  74. //JOptionPane.showMessageDialog(null,"The roots of the Quadratic Equation \"Ax^2 + Bx +C = 0\" are "+root1+" and "+root2);
  75.  
  76. }
  77. else if (disc==0)
  78. {
  79. //real roots if roots are the same
  80. root1 = (-b + Math.sqrt(disc)) / (2 * a);
  81. JOptionPane.showMessageDialog(null,"Roots is:"+ root1);
  82. //System.out.println("Root is: " +root1);
  83. }
  84.  
  85. }
  86. //string to receive test values
  87. public static double TestNum(String prompt)
  88. {
  89. double returnVal = 0;
  90. String s = JOptionPane.showInputDialog(prompt);
  91.  
  92. returnVal = Double.parseDouble(s);
  93.  
  94. return returnVal;
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement