warfighter67

Quadtratics solver Java

Oct 6th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class EquationsMain {
  5.  
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args)
  10.     {
  11.         double a, b, c, vx, vy, xans1, xans2;
  12.         Scanner i = new Scanner(System.in);
  13.         boolean doAgain;
  14.         do
  15.         {
  16.             doAgain=false;
  17.             System.out.print("A value: ");
  18.             a = i.nextDouble();
  19.             i.nextLine();
  20.             System.out.print("B value: ");
  21.             b = i.nextDouble();
  22.             i.nextLine();
  23.             System.out.print("C value: ");
  24.             c = i.nextDouble();
  25.             i.nextLine();
  26.             String e = "Equation: " + a + "x^2";
  27.             if(b<0)
  28.             {
  29.                 e += "" + b + "x";
  30.             }
  31.             else if(b>0)
  32.             {
  33.                 e += "+" + b + "x";
  34.             }
  35.             if(c<0)
  36.             {
  37.                 e += "" + c;
  38.             }
  39.             else if(c>0)
  40.             {
  41.                 e += "+" + c;
  42.             }
  43.             e += "=0";
  44.             System.out.println(e);
  45.             String s = "Solution: ";
  46.             xans1 = ((b*-1) + Math.sqrt((b*b)-4*a*c))/(2*a);
  47.             xans2 = ((b*-1) - Math.sqrt((b*b)-4*a*c))/(2*a);
  48.             vx=-(b)/(2*a);
  49.             vy = a*(vx*vx) + b*vx + c;
  50.             double b2 = b*b, a2 = a* -4;
  51.             double discr = b2 + (a2*c);
  52.             s+= xans1;
  53.             if(xans1 != xans2)
  54.             {
  55.                 s+= ", ";
  56.                 s+= xans2;
  57.             }
  58.             if(((b*b)-4*a*c) < 0)
  59.             {
  60.                 System.out.println("There are no solutions.");
  61.             }
  62.             else
  63.             {
  64.                 System.out.println(s + '\n' + "Time: " + (Math.sqrt(xans2)));
  65.             }
  66.             System.out.println("Exact answer: (" + -b + " +/- Sqrt(" + discr + ")) / " + (a*2));
  67.             String v = "Vertex: (" + vx + ", " + vy + ")";
  68.             System.out.println(v + "\nDiscriminant: " + discr);
  69.            
  70.             double x2=vx+1, x3=vx+2, y2, y3;
  71.             y2 = a*(x2*x2) + b*x2 + c;
  72.             y3 = a*(x3*x3) + b*x3 + c;
  73.            
  74.             System.out.println("  x  |  y  ");
  75.             System.out.println("-----------");
  76.             System.out.println( x2+" | "+y2 );
  77.             System.out.println( x3+" | "+y3 );
  78.            
  79.             boolean v1;
  80.             do
  81.             {
  82.                 System.out.print("Do another equation? ");
  83.                 String d = i.nextLine();
  84.                 String yn = d.charAt(0) + "";
  85.                 v1=true;
  86.                 if(yn.equalsIgnoreCase("y"))
  87.                 {
  88.                     doAgain=true;
  89.                 }
  90.                 else if(!(yn.equalsIgnoreCase("n")))
  91.                 {
  92.                     System.out.println("Please type YES or NO");
  93.                     v1=false;
  94.                 }
  95.             }
  96.             while(!v1);
  97.         }
  98.         while(doAgain);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment