
Milad G. answered 04/01/20
Used in a professional setting as Software Engineer
Not sure if this is actually javascript because that language does not have named exceptions such as "ArithmeticException" and "NumberFormatException". Those exceptions you have specified are from JAVA. So i will write my answer for Java, assuming that you tagged this problem incorrectly to Javascript.
Furthermore, you really need to start specifying your questions by providing more details, if possible and even what level of school you are in so with the lack of information someone helping you can at least make assumptions on how hard the problem can get and what considerations need to be made. Here I assume this is a question you got on one of your labs or assignments for high school AP computer science or a similar entry level college level course. The exceptions you have mentioned each seek to raise issues with the following:
1) ArithmeticException - When some arithmetic you are trying to do is invalid, such as dividing by zero, which based on the problem description could be exactly what you need to watch out for since method "showResult" is doing division. Read more here: https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
2) NumberFormatException - Thrown when a string is being converted into a number. Read more here: https://www.tutorialspoint.com/how-to-handle-the-numberformatexception-unchecked-in-java
Basically what is happening here from what I can deduce, is that your method setData is taking in 2 Strings that represent numbers. If the Strings provided by the user do not have solely numeric characters than the NumberFormatException could be thrown. I hope for clarity the problem gave you the method signature of the methods you have to implement in this class. Read more about what a method signature is here: https://www.thoughtco.com/method-signature-2034235
class DivideStringNums {
int num1;
int num2;
// Throws the exception when the strings contain non numeric characters
public void setData(String A, String B) throws NumberFormatException {
this.num1 = Integer.parseInt(A);
this.num2 = Integer.parseInt(B);
}
// If dividing by zero..as in the divisor is zero, or both numbers are zero (DNE case)
// Make sure to clarify which of the two nums should be divisor and which the dividend
// Also this method does integer division meaning the remainder is left out, if you need the remainder
// clarify with your teacher, and you would change the return type from int to float or double
// based on the decimal precision that is required
public int showResult() throws ArithmeticException {
return this.num1 / this.num2;
}
}