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

I\'ve been trying to work an exception handler into my code for text accidentall

ID: 3887960 • Letter: I

Question

I've been trying to work an exception handler into my code for text accidentally input into a numerical operations program. How would I approach it with my code provided below?

import java.util.Scanner;/* includes a class that permits use of user input in the program */

class MathOP/* first class which introduces variables first and second */
{
   private double first;
   private double second;

   public MathOP (double a , double b )/* constructors where variables are input */
{
   this.first= a; /* objects created as a and b */
   this.second=b;
}

   public double getFirst() /* method gets the value of the first object */
   {
   return first;
   }
   public void setFirst(double first)/* method sets the value of the first object */
   {
   this.first= first;
   }
   public double getSecond()
   {
   return second;
   }
   public void setSecond(double second)
   {
   this.second = second;
   }
   public double Add()/* operation method */
   {
   return first+second;
   }
   public double Sub()
   {
   return first-second;
   }
   }
class MathOP2 extends MathOP /* The first class is extended to include the new class to introduce multiplication and division */
{

MathOP2(double a, double b)
{
super(a, b);
}
public double Mul()
{
return super.getFirst() * super.getSecond();/* super is used to return to the value obtain in the original class method */
}
   public double Div() /* This is the method I will use for my exception which is division by zero */
{
double result=0; /* New Variable to return exception message */
try /* Begins exception for user to input condition */
{
result=super.getFirst() / super.getSecond();
return result;
}
catch (Exception e) /* Named exception for the program to catch and handle */
{
e.printStackTrace();
}
finally
{
System.out.println ("Error, Divsion by Zero"); /* Message to user to inform them the exception has been input */
result =0;
System.out.println ("Try Again"); /* Directs user to be prepared to input new values */

return result;
}
}
}

class ExecuteMathOP { /* This class will execute based upon the defined classes previously. It will then verify the inheritance feature of Java */

public static void main(String[] args)

try{

      Scanner in = new Scanner(System.in);

while (true)
{
         System.out.print("Enter the First number >>"); /* user input */
         double first = Double.parseDouble(in.nextLine());
         System.out.print("Enter the Second number >>");
         double second = Double.parseDouble(in.nextLine());
         System.out.print("Enter operator >>");
         String symbol = in.nextLine(); /* this takes the operator entered to a string object */
         double first =0, second=0;
         String Symbol="";
       
         while (sc.hasNext())
       
{
         MathOP2 mathOP2=new MathOP2(first,second); /* new object created with original class properties */
         if (symbol.equals("+"))
{
         System.out.println("The answer is " + mathOP2.Add()); /* performs methods outlined in the first class */
       
         }
       
         else if (symbol.equals("/"))
       
         {
       
         System.out.println("The answer is " + mathOP2.Div()); /* performs methods outlined in the second class */

         }
         else if (symbol.equals("*"))
         {
            System.out.println("The answer is " + mathOP2.Mul());

         }
          else
         {
            System.out.println("The answer is " + mathOP2.Sub());

         }

         System.out.print("Do you want to exit (Y/N)?"); /* user input */
         String option = in.nextLine();

         if (option.equals("Y"))
         {
       
         System.out.println("Thanks for using our system");
         System.exit(0); /* termination of program */
       
         }
      }

   }

}

Explanation / Answer

Hi,

I have uupdated the code and highlighted the code changes below.

import java.util.Scanner;/* includes a class that permits use of user input in the program */

class MathOP/* first class which introduces variables first and second */

{

private double first;

private double second;

public MathOP(double a, double b)/* constructors where variables are input */

{

this.first = a; /* objects created as a and b */

this.second = b;

}

public double getFirst() /* method gets the value of the first object */

{

return first;

}

public void setFirst(double first)/* method sets the value of the first object */

{

this.first = first;

}

public double getSecond() {

return second;

}

public void setSecond(double second) {

this.second = second;

}

public double Add()/* operation method */

{

return first + second;

}

public double Sub() {

return first - second;

}

}

class MathOP2 extends MathOP /*

* The first class is extended to include the new

* class to introduce multiplication and division

*/

{

MathOP2(double a, double b) {

super(a, b);

}

public double Mul() {

return super.getFirst() * super.getSecond();/*

* super is used to return

* to the value obtain in

* the original class method

*/

}

public double Div() /*

* This is the method I will use for my exception which

* is division by zero

*/

{

double result = 0; /* New Variable to return exception message */

try /* Begins exception for user to input condition */

{

result = super.getFirst() / super.getSecond();

return result;

} catch (Exception e) /*

* Named exception for the program to catch and

* handle

*/

{

e.printStackTrace();

} finally {

System.out.println("Error, Divsion by Zero"); /*

* Message to user to

* inform them the

* exception has been

* input

*/

result = 0;

System.out.println("Try Again"); /*

* Directs user to be prepared to

* input new values

*/

return result;

}

}

}

public class ExecuteMathOP { /*

* This class will execute based upon the defined

* classes previously. It will then verify the

* inheritance feature of Java

*/

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

try{

while (true) {

System.out.print("Enter the First number >>"); /* user input */

double first = Double.parseDouble(in.nextLine());

System.out.print("Enter the Second number >>");

double second = Double.parseDouble(in.nextLine());

System.out.print("Enter operator >>");

String symbol = in.nextLine(); /*

* this takes the operator entered

* to a string object

*/

String Symbol = "";

while (in.hasNext())

{

MathOP2 mathOP2 = new MathOP2(first, second); /*

* new object

* created with

* original class

* properties

*/

if (symbol.equals("+")) {

System.out.println("The answer is " + mathOP2.Add()); /*

* performs

* methods

* outlined

* in

* the

* first

* class

*/

}

else if (symbol.equals("/"))

{

System.out.println("The answer is " + mathOP2.Div()); /*

* performs

* methods

* outlined

* in

* the

* second

* class

*/

} else if (symbol.equals("*")) {

System.out.println("The answer is " + mathOP2.Mul());

} else {

System.out.println("The answer is " + mathOP2.Sub());

}

System.out.print("Do you want to exit (Y/N)?"); /* user input */

String option = in.nextLine();

if (option.equals("Y")) {

System.out.println("Thanks for using our system");

System.exit(0); /* termination of program */

}

}

}

}catch(NumberFormatException e){

System.out.println("Invalid inputs.");

}

}

}

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