Answered step by step
Verified Expert Solution
Link Copied!

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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Why are cheques and credit cards not money?

Answered: 1 week ago