Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses the following subroutine, to solve equations specified

ID: 3815412 • Letter: W

Question

Write a program that uses the following subroutine, to solve equations specified by the user.

/**
* Returns the larger of the two roots of the quadratic equation
* A*x*x + B*x + C = 0, provided it has any roots. If A == 0 or
* if the discriminant, B*B - 4*A*C, is negative, then an exception
* of type IllegalArgumentException is thrown.
*/
static public double root( double A, double B, double C )
                              throws IllegalArgumentException {
    if (A == 0) {
      throw new IllegalArgumentException("A can't be zero.");
    }
    else {
       double disc = B*B - 4*A*C;
       if (disc < 0)
          throw new IllegalArgumentException("Discriminant < zero.");
       return (-B + Math.sqrt(disc)) / (2*A);
    }
}

Your program should allow the user to specify values for A, B, and C. It should call the subroutine to compute a solution of the equation. If no error occurs, it should print the root. However, if an error occurs, your program should catch that error and print an error message. After processing one equation, the program should ask whether the user wants to enter another equation. The program should continue until the user answers no.

Explanation / Answer

import java.util.Scanner;

public class Equation{

     public static void main(String []args){
      
        double A,B,C;      // variables A,B,C to read values of equation
        String input;      // input variable to ask user for continue
        Scanner in = new Scanner(System.in);
      
        do{
          
          System.out.println("Please Enter the value of A : ");
          A=in.nextDouble();
      
          System.out.println("Please Enter the value of B : ");
          B=in.nextDouble();
      
          System.out.println("Please Enter the value of C : ");
          C=in.nextDouble();
      
          try{
        
            System.out.println("The Larger values of 2 roots is : "+root(A,B,C));
        
           }
          catch(Exception e){
          
             System.out.println(e.getMessage());
          
           }
         
          System.out.println("Press Yes to continue or No to terminate : ");
          input = in.next();
        
        } while(input.equalsIgnoreCase("yes"));
     }
   
     static public double root( double A, double B, double C ) throws IllegalArgumentException {
  
         if (A == 0) {
           
          throw new IllegalArgumentException("A can't be zero.");
        
         }
         else {
           
          double disc = B*B - 4*A*C;
          if (disc < 0)
            throw new IllegalArgumentException("Discriminant < zero.");
          return (-B + Math.sqrt(disc)) / (2*A);
    }
}
}

/*

ouput :

Please Enter the value of A :                                                                                                                                            

1                                                                                                                                                                        

Please Enter the value of B :                                                                                                                                            

-5                                                                                                                                                                       

Please Enter the value of C :                                                                                                                                            

6                                                                                                                                                                        

The Larger values of 2 roots is : 3.0                                                                                                                                    

Press Yes to continue or No to terminate :                                                                                                                               

yes                                                                                                                                                                      

Please Enter the value of A :                                                                                                                                            

1                                                                                                                                                                        

Please Enter the value of B :                                                                                                                                            

1                                                                                                                                                                        

Please Enter the value of C :                                                                                                                                            

6                                                                                                                                                                        

Discriminant < zero.                                                                                                                                                     

Press Yes to continue or No to terminate :                                                                                                                               

no                                                 

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote