The following code contains two classes both containing implementations of the factorial function. Calculator works fine...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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) throw ew IllegalArgumentException(); else return super. factorial (input); } (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! [2 marks] (b) SafeCalculator is an example of unsafe inheritance; this class violates a core principle of object-oriented design. Name this principle, and explain how it is violated [3 marks] (c) Write a simple JUnit test case to test the correctness of Calculator. This test(s) should check whether the first four factorial numbers (1->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] 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) throw ew IllegalArgumentException(); else return super. factorial (input); } (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! [2 marks] (b) SafeCalculator is an example of unsafe inheritance; this class violates a core principle of object-oriented design. Name this principle, and explain how it is violated [3 marks] (c) Write a simple JUnit test case to test the correctness of Calculator. This test(s) should check whether the first four factorial numbers (1->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]
Expert Answer:
Answer rating: 100% (QA)
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 the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
What is the difference between MouseListener and MouseAdapter? [3 marks] (b) Via suitable HTML, the compiled version of the following Java code is presented to the appletviewer application: import...
-
Prepare the schedule of year-end deferred tax assets and liabilities for each year. The schedule should separately list the sources of the firms ending deferred tax assets and deferred tax...
-
On April 18, 2019, Bontanica Company, a garden retailer, purchased $9,800 of seed, terms 2/10, n/30, from Whitetail Seed Co. Even though the discount period had expired, Shelby Davey subtracted the...
-
For the manometer shown in Fig. 3.30, calculate (p A - p B ). Water IB 150 mm - Mercury (sg = 13.54) 900 mm Oil (sg = 0.86) 600 mm
-
Under what condition is difference estimation more appropriate than ratio estimation?
-
The management of Ballard MicroBrew is considering the purchase of an automated bottling machine for $120,000. The machine would replace art old piece of equipment that costs $30,000 per year to...
-
A light rope is used to lift a 10-kg mass and give it an acceleration of 2.0 m/s [up] (g = 9.81 m/s [down]). What is the net force?
-
below: A Diccatis rate, r 1 2 3 Year 4 Cash flow You are considering a project whose cash flows are given B 25% 0 -1,000 C D E F LL G H 1 2 3 4 5 6 7 100 200 300 400 500 600 700 Calculate the present...
-
Show that it follows directly from the equation of state P = RT/, that for an ideal gas the heat capacity at constant volume, C v , is, at most, a function of temperature.
-
For an infinitesimal volume, informally show that where is some (differentiable) property of the fluid. Hence informally deduce that D Dt Do (V) = pV : Dt (P1.1)
-
Consider a massive string of constant linear mass density and length L whose endpoints are fixed at A (x = 0, z = z0) and B (x = a, z = z1). The string lies in the vertical plane (x, z), and it is...
-
Show that the derivative of an integral is given by By generalizing to three dimensions show that the material derivative of an integral of a fluid property is given by where the surface integral ( s...
-
Consider a pendulum of length l and mass m2 hanging on a point of mass m1 that moves horizontally without friction on a rail.We note x the abscissa of m1 and the angle with the vertical direction....
-
Under the property of Convolution solve the following sequences: x 1 [n] = (2000) n u(n) x 2 [n]=(06) n u(n) Find: x[n] = x 1 [n] * x2[n]
-
An investor sells a European call on a share for $4. The stock price is $47 and the strike price is $50. Under what circumstances does the investor make a profit? Under what circumstances will the...
-
William sold Section 1245 property for $25,000 in 2012. The property cost $35,000 when it was purchased 5 years ago. The depreciation claimed on the property was $16,000. a. Calculate the adjusted...
-
Carl Conch and Mary Duval are married and file a joint return. They live at 1234 Mallory Sq. Apt. 64, Key West, FL 33040. Carl works for the Key Lime Pie Company and Mary is a homemaker after losing...
-
Brian and Kim have a 12-year-old child, Stan. For 2012, Brian and Kim have taxable income of $52,000, and Stan has nonqualifying dividend income of $4,500 and investment expenses of $250. No election...
-
Fill in the Blanks. The mechanical clock represents a(n) __________ pendulum.
-
Fill in the Blanks. The free vibration of an undamped system represents interchange of__________ and energies __________.
-
Fill in the Blanks. A system undergoing simple harmonic motion is called \(a(n)\) __________ oscillator.
Study smarter with the SolutionInn App