fork download
  1. /**
  2.  
  3.  * Write a description of class quadraticformula here.
  4.  *
  5.  * @author (??)
  6.  * @[member='versions'] (8/21/15)
  7.  */
  8. import java.util.Scanner;
  9. import java.util.*;
  10. public class Main {
  11. public static double handleradicand(double a, double b, double c){ //Step one, calculate the radicand(the part inside the square root)
  12. double radicand = b * b - (4 * a * c); // b squared minus 4ac
  13. return radicand; //return the radicand
  14. }
  15. public static double rootradicand(double rad, double e){ //find the square root of the radical
  16. double radical = Math.sqrt(rad); // square root of b squared minus 4ac
  17. return radical; //return the radical
  18. }
  19. public static double divideBby2a(double b, double a) { //finding -b over 2a and the square root over 2a and storing them in seperate variables.
  20. double zerominusb = 0 - b; //storing -b as a variable because I wasn't sure if I could just type -b in a calculation
  21. double bover2a = zerominusb / (2 * a); //-b over 2a
  22. return bover2a;
  23. }
  24. public static double divideRADby2a(double rad, double a){
  25. double radover2a = rad / (2 * a); // the radical over 2a
  26. return radover2a;
  27. }
  28. public static double findroot1(double BO2A, double RO2A){ //find the roots of the equation
  29. double first_X = BO2A + RO2A; // -b over 2a plus the radical over 2a
  30. return first_X; //return the first root
  31. }
  32. public static double findroot2(double BO2A, double RO2A){
  33. double second_X = BO2A - RO2A;
  34. return second_X;
  35. }
  36. public static void main(String[] args){ //execute the previously defined methods to calculate the quadratic equation.
  37. System.out.println("What is the A value?"); //Asking the user for the A value in their quadratic equation.
  38. Scanner Ascanner = new Scanner(System.in); //Created a new scanner variable for the A value.
  39. double a = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the A value.
  40. System.out.println("What is the B value?"); //Asking the user for the B value in their quadratic equation.
  41. double b = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer for the B value.
  42. System.out.println("What is the C value?"); //Asking the user for the C value in their quadratic equation.
  43. double c = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the C value.
  44. //Finding the radicand
  45. double tem1 = handleradicand(a,b,c);
  46. System.out.println("radicand" + tem1);
  47. //Finding the radical
  48. double tem2 = rootradicand(tem1, .5);
  49. System.out.println("radical" + tem2);
  50. double tem3 = divideBby2a (b,a);
  51. double tem4 = divideRADby2a (tem2, a);
  52. double firstX = findroot1 (tem3, tem4);
  53. double secondX = findroot2 (tem3, tem4);
  54. System.out.println("Your roots are X = " + firstX + "and X = " + secondX); //displaying the roots
  55. }
  56. }
Success #stdin #stdout 0.14s 321344KB
stdin
1
3
2
stdout
What is the A value?
What is the B value?
What is the C value?
radicand1.0
radical1.0
Your roots are X = -1.0and X = -2.0