Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Quadratic w/ Exception Handle [ please help ]

$
0
0
Howdy friends;
Here is my assignment:

Quote

Write a program that will solve a quadratic equation,
1. given the values of a, b and c, and
2. return the larger of the two roots.
3. Be sure to handle the appropriate exceptions.
4. You need to write the main method that catches the proper exception as well as catching any input exceptions.
5. The program should implement a finally block that tells the user that the program is existing because an error was created.
The main method should also ask for user input and read in the values of A, B and C.


I believe that I have addressed a majority of the points that were requested. However; upon running I seem to get a strange error after inputting my a, b, and c values. Any help or suggestions would greatly be appreciated once again.
package exceptionhandlerquadratic;
import java.util.InputMismatchException;
import java.util.Scanner;
class ExceptionHandlerQuadratic {
    private static Scanner console = new Scanner(System.in);
    private static double aValue = 0;
    private static double bValue = 0;
    private static double cValue = 0;
    private static boolean acceptableUserInput = false;
    public static void main(String[] args) {
        while(true){
            do{
                try{
                    System.out.println("a: ");
                    aValue = console.nextDouble();
                    System.out.println("b: ");
                    bValue = console.nextDouble();
                    System.out.println("c: ");
                    cValue = console.nextDouble();
                    acceptableUserInput = true;
                }
                catch(InputMismatchException e){
                    System.err.println("Caught InputMismatchExcpection," + 
                            " please enter a valid integer");
                    //clear buffer
                    console.next();
                    //retry
                    continue;
                }
            }while(!(acceptableUserInput));
            getRoot(aValue, bValue, cValue);
        }
    }
    static double getRoot(double A, double B, double C)
                                 throws IllegalArgumentException {
            // Returns the larger of the two roots of the quadratic equation A*x*x + B*x + C = 0.
            // (Throws an exception if A == 0 or B*B-4*A*C < 0.)

         if (A == 0) {
           throw new IllegalArgumentException("A cannot be zero.");
         }
         else {
            double disc = B*B - 4*A*C;
            System.out.println ("A = " + A + "  B = " + B + "  C = " + C);
            if (disc < 0) {
                 throw new IllegalArgumentException("Discriminant < zero.");
             }
            return  (-B + Math.sqrt(disc)) / (2*A);
         }
     }
}


Here is the output
a: 
2
b: 
3
c: 
4
A = 2.0  B = 3.0  C = 4.0
Exception in thread "main" java.lang.IllegalArgumentException: Discriminant < zero.
	at exceptionhandlerquadratic.ExceptionHandlerQuadratic.getRoot(ExceptionHandlerQuadratic.java:46)
	at exceptionhandlerquadratic.ExceptionHandlerQuadratic.main(ExceptionHandlerQuadratic.java:31)
Java Result: 1
BUILD SUCCESSFUL (total time: 23 seconds)



Thank you; =D

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>