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

In Java Assignment : The next lab assignment is another exercise in the use of t

ID: 3866873 • Letter: I

Question

In Java

Assignment:

The next lab assignment is another exercise in the use of the exception and handling of exceptions. There is another simple calculation program listed below, which takes decimal values (doubles) as input for the a, b and c coefficients of a quadratic equation and calculates the two values for X. The program should always display two values as the result; a quadratic equation always has two values as the solution even when they are identical.

The program should repeat itself if the user wishes; this makes it easier to test. Values for A and B will be cleared to 0 (zero) after each calculation.

However it does nothing to handle the situation where a user enters data which is non-double or even non numeric. Your task today is to modify that program to handle the bad input situation with the use of exception handling. Bad inputs are:

Non-numeric values where numeric values are expected

Any input that causes the output to be “NaN” (Not a Number)

Any input that causes the output to be “INFINITY”

The program should not terminate but should inform the user of the specific error and should re-request the input value which was in error

Testing:

     Test your program with the following input;

a. Non-numeric value when double is expected.

b. A = 0

c. Values which result in “NaN” or “INFINITY” for answers.

d. Correct input

In all cases the program should ask for only the input values that were invalid and should continue with the values that were correct. The request for a corrected value should be repeated as many times as the user inputs illegal values.

The program should never crash!

Explanation / Answer

import java.io.*;
import java.util.*;
import java.lang.Math.*;


class InvalidDataException extends Exception
{
  
   public InvalidDataException()
   {
     
   }
   public void displayMsg()
   {
      System.out.println("Invalid Data ");
   }
}


public class DemoException{

    public static void main(String[] args) throws InvalidDataException{

          double a=0,b=0,c=0;
          String inp;
          int valid;
          double value1,value2;
          Scanner sc=new Scanner(System.in);
    
          do {
                valid = 0;
                try {
                       System.out.println("Enter a:");
                       inp = sc.next();
                       if (!inp.matches("^[0-9]+$") || inp.contains("0")){
                          throw new InvalidDataException();
                       }
                       else {
                            a = Integer.parseInt(inp);
                            valid = 1;
                       }
                    
                }
                catch (InvalidDataException e){
                    e.displayMsg();
                }
          } while (valid == 0);
          do {
                valid = 0;
                try {
                       System.out.println("Enter b:");
                       inp = sc.next();
                       if (!inp.matches("^[0-9]+$")) {
                          throw new InvalidDataException();
                       }
                       else {
                            b = Integer.parseInt(inp);
                            valid = 1;
                       }
                    
                }
                catch (InvalidDataException e){
                    e.displayMsg();
                }
          } while (valid == 0);
          do {
                valid = 0;
                try {
                       System.out.println("Enter c:");
                       inp = sc.next();
                       if (!inp.matches("^[0-9]+$")) {
                          throw new InvalidDataException();
                       }
                       else {
                            c = Integer.parseInt(inp);
                            valid = 1;
                       }
                    
                }
                catch (InvalidDataException e){
                    e.displayMsg();
                }
          } while (valid == 0);
          value1 = ((-1)*(b) + Math.sqrt(b*b - 4*a*c))/(2*a*c);
          value2 = ((-1)*(b) - Math.sqrt(b*b - 4*a*c))/(2*a*c);
          System.out.println(" x = " + value1 + " x = " + value2);
       
    }
}

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