Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Quadratic
  4. {
  5.  
  6.  
  7. public static void main(String[] args)
  8. {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. while(scanner.toString() != "NO")
  12. {
  13. try
  14. {
  15. System.out.println("Enter A");
  16. double a = scanner.nextDouble();
  17. scanner.nextLine();
  18.  
  19. System.out.println("Enter B");
  20. double b = scanner.nextDouble();
  21. scanner.nextLine();
  22.  
  23. System.out.println("Enter C");
  24. double c = scanner.nextDouble();
  25. scanner.nextLine();
  26.  
  27. root(a,b,c);
  28.  
  29.  
  30. System.out.println("It works!");
  31.  
  32.  
  33. }
  34. catch (IllegalArgumentException oops)
  35. {
  36. oops.printStackTrace();
  37. }
  38. catch(Exception other)
  39. {
  40. other.printStackTrace();
  41. }
  42.  
  43. System.out.println("Do you want to continue ?");
  44. System.out.println("YES / NO");
  45. String a1 = scanner.next();
  46. }
  47.  
  48. System.out.println("Exit");
  49. }
  50.  
  51. static double root(double A, double B, double C) throws IllegalArgumentException
  52. {
  53. if (A == 0)
  54. {
  55. throw new IllegalArgumentException("A can't be zero");
  56. }
  57. else
  58. {
  59. double disc = B*B - 4*A*C;
  60. if (disc < 0)
  61. {
  62. throw new IllegalArgumentException("Discriminant < zero.");
  63. }
  64. return (-B + Math.sqrt(disc)) / (2*A);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement