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

Writ in java and please donot use any break statements. Please use try and catch

ID: 3706940 • Letter: W

Question

Writ in java and please donot use any break statements. Please use try and catch block to throw exceptions in this question

In your second programming project you created a java file called Quadratic.java, which took the Coefficients of a quadratic equation and gave the two solutions for the variable X.At the time we assumed the user would give us three values that would successfully work.However if the user gave us a zero for the A term or a set of values that caused ( B2-4AC ) to be non-positive, we would end up not getting two rational solutions.Your job is to add the code required to throw exceptions if either of these above conditions happen.Put try and catch blocks in your code in a way that will force the user to enter the values again if they are not good ones. The program only needs to loop in a way so that you end up with 2 valid answers.The program can then end. You can pull your program down from Blackboard if you need to.

Example:

This program will solve the quadratic equation given coefficients a, b, and c. Please enter a: (Cannot be zero)

>>>0

A cannot be zero. Please try again.

>>>1

Enter B:

>>>2

Enter C:

>>>3

These values will give a imaginary answers (equation under the square root evaluates to a negative value). Try again.

This program will solve the quadratic equation given coefficients a, b, and c. Please enter a: (Cannot be zero)

… etc.

Explanation / Answer

import java.util.*;

public class Quad

{

    public static void main(String[] args)

    {

            Scanner sc=new Scanner(System.in);

            System.out.println("please enter ‘a’ value should be greater than 0 ");

            System.out.println("Enter a: ");

            double a = sc.nextDouble();

            System.out.println("Enter b: ");

            double b = sc.nextDouble();

            System.out.println("Enter c: ");

        double c = sc.nextDouble();

        //Finding out the roots

        double temp1 = Math.sqrt(b * b - 4 * a * c);

        double root1 = (-b + temp1) / (2*a) ;

        double root2 = (-b - temp1) / (2*a) ;

        System.out.println("The roots of the Quadratic Equation are "+root1+" and "+root2);

    }

}

Output:

Suppose our Quadratic Equation to be solved is 2x2 + 6x + 4 = 0 (Assuming that both roots are real valued).

General form of a Quadratic Equation is ax2 + bx + c = 0 where 'a' is not equal to 0

Hence a = 2, b = 6 and c = 4

The roots of the Quadratic Equation are -1.0 and -2.0

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