I’m stuck on a homework problem. The code below is functioning, but I’m trying to integrate a try, catch, and finally statement in there. Everytime I’ve tried, I’ve broken the code. I want it to handle the exception of division by zero. How do I proceed?
import java.util.Scanner;
class MathOP
{
private double first;
private double second;
public MathOP (double a , double b )
{
this.first= a;
this.second=b;
}
public double getFirst()
{
return first;
}
public void setFirst(double first)
{
this.first= first;
}
public double getSecond()
{
return second;
}
public void setSecond(double second)
{
this.second = second;
}
public double Add()
{
return first+second;
}
public double Sub()
{
return first-second;
}
}
class MathOP2 extends MathOP
{
MathOP2(double a, double b)
{
super(a, b);
}
public double Mul()
{
return super.getFirst() * super.getSecond();
}
public double Div()
{
return super.getFirst() / super.getSecond();
}
}
class ExecuteMathOP {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (true)
{
System.out.print(“Enter the First number >>”);
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();
MathOP2 mathOP2=new MathOP2(first,second);
if (symbol.equals(“+”))
{
System.out.println(“The answer is ” + mathOP2.Add());
}
else if (symbol.equals(“/”))
{
System.out.println(“The answer is ” + mathOP2.Div());
}
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)?”);
String option = in.nextLine();
if (option.equals(“Y”))
{
System.out.println(“Thanks for using our system”);
System.exit(0);
}
}
}
}
Expert Answer
import java.util.Scanner;
class MathOP
{
private double first;
private double second;
public MathOP (double a , double b )
{
this.first= a;
this.second=b;
}
public double getFirst()
{
return first;
}
public void setFirst(double first)
{
this.first= first;
}
public double getSecond()
{
return second;
}
public void setSecond(double second)
{
this.second = second;
}
public double Add()
{
return first+second;
}
public double Sub()
{
return first-second;
}
}
class MathOP2 extends MathOP
{
MathOP2(double a, double b)
{
super(a, b);
}
public double Mul()
{
return super.getFirst() * super.getSecond();
}
public double Div()
{
double result = 0.0;
try {
result = super.getFirst() / super.getSecond();
return result;
}
catch (Exception e){
e.printStackTrace();
}
finally{
System.out.println(“Aborting the operation”);
result = -1;
return result;
}
}
}
class ExecuteMathOP {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (true)
{
System.out.print(“Enter the First number >>”);
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();
MathOP2 mathOP2=new MathOP2(first,second);
if (symbol.equals(“+”))
{
System.out.println(“The answer is ” + mathOP2.Add());
}
else if (symbol.equals(“/”))
{
System.out.println(“The answer is ” + mathOP2.Div());
}
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)?”);
String option = in.nextLine();
if (option.equals(“Y”))
{
System.out.println(“Thanks for using our system”);
System.exit(0);
}
}
}
}