Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
Step: 1
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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started