The methods foo1 and foo2 are intended to calculate the number of positive facto
ID: 3848075 • Letter: T
Question
The methods foo1 and foo2 are intended to calculate the number of positive factors| of a positive integer. For example, the six factors of 12 are 1, 2, 3, 4, 6, and 12, so foo1 (12) or foo2 (12) should return 6. Compare and contrast the methods fool and foo2. Comment on the strengths and weakness of each implementation, using the criteria of correctness, design, efficiency, and readability. Use the line numbers to refer to parts of the code in your answer. private int foo1 (int n) { for (int i = 0; iExplanation / Answer
Solution:
As given in the problem statement methods foo1 and foo2 are intended to calculate number of positive factors of a positive integer.
Considering the functions let us assume n=12. As mentioned in the example. Six factors of 12 are 1,2,3,4,6,12.
Now, taking foo1 method for explanation of its strengths and weakness of its implementation. Refering the line numbers below for explanation.
Correctness - in the method foo1, the number of positive factors of a positive integer are not calculated as given in the problem statement. Instead we can see from Line 4 that every integer from 0 to 12 which satisfies the condition in Line 3 is returned. The Line 3 will always be 0 since n%i is assigned 0 (since assignment operator = is used) and all the integers i wil be returnedirrespective of the condition in if "statement . Also , refering LIne 3 - in which comparision statement should have == equality operator instead of = This is the correct comparison (n%i==0).
Design - Line 1: private method indicates that it cannot be accessed by other methods since it is private.
Efficiency - The count of positive factors of a postive integer are not returned. as in Line 4. The for loop run from 0 to 12(As assumed n=12) The Line 3 will always be 0 since n%i is assigned 0 (since assignment operator = is used) LIne 2 is executed n times,line 3 = n times, line 4 n times. n+n+n=3n time complexity
Readability - The method foo1 is readable and can be understood by the user its implementation and functionality.
Detailed explanation for method foo1
Line 1 : n=12 is passed to the method as an argument.
Line 2 : for loop from i=0 to i<=12. i is incremented by one
Line 3: the condition is checked , but the operator used should be == instead of =. equality operator. The actual logic that If the remainder after dividing 12%0 is 0 then the statement in the next Line 4 is to be executed but here assignment operator = is used for which always n%i is assigned 0 and the statement is always satisfied and Line 4 is executed
Line 4: The value of i from 0 to 12 is returned, WHen i is 13 , the condition in line 2 fails, i<=n(13<=12) and the loop terminates.
Taking foo2 method for explanation of its strengths and weakness of its implementation. Refering the line numbers below for explanation.
Correctness: The method foo2 designed to calculate the number of positive factors of postive integer(assume: n=12). the method foo2 corectly returns the result.
Design:The foo2 method as in LIne 10 is a public method, it can be accessed by any another method in the program .
Efficiency: line 11 is executed 1 time. line 12 n times, line 13 n times, line 14 n times . line 17 is 1 time. 3n+2 is time complexity.
Readability: The method foo2 is readable and can be understood by the user its implementation and functionality as all the variables declaration and method implementation is understood step by step.
Detailed explanation for method foo2
Line 10: n=12 is passed as an argument to foo2.
Line 11: count=1 is initialzed to count variable. it is provided to count number of positive factors of a given positiv einteger.
line 12:for loop runs from i=1 to 12. by incrementing i by one after condition.
line 13: n%i==0, th remainder for eg: 12%1 is checked and if it is 0 then line 14 is executed
line 14: if line 13 condition is satisfied, then count is incremented by 1. count =2 . it goes on untill i =12 the conditon i<n fails and loop is terminated.
line 17: count is returned as 6. which is correct solution.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.