Question: The following code contains two classes both containing implementations of the factorial function. Calculator works fine for positive numbers, but not for negative numbers

The following code contains two classes both containing implementations of the factorial  

The following code contains two classes both containing implementations of the factorial function. Calculator works fine for positive numbers, but not for negative numbers or 0. SafeCalculator tries to solve this problem by throwing an exception when negative or 0 input values are encountered. Note that both classes can be compiled successfully. public class Calculator ( public int factorial (int input) { if (input--1) return 1; else return input factorial (input-1); } public class SafeCalculator extends Calculator | @Override public int factorial (int input) { if (input 1,2->2,3-6,4->24) are correctly calculated by Calculator. [2 marks] (d) Assume you have written all test cases in question c) correctly, and all those tests pass. Does this imply that Calculator is a correct implementation of the factorial number algorithm? Discuss! [2 marks]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

a SafeCalculator throws an exception in line 10 without declaring or catching it This seems to violate the catch or specify requirement Explain why this is accepted by the compiler The reason the comp... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!