Question: You must assign the grades for a programming class. Right now the class is studying recursion, and students have been given this simple assignment: Write
You must assign the grades for a programming class. Right now the class is
studying recursion, and students have been given this simple assignment: Write
a recursive method sumSquares that takes a reference to a linked list of Integer elements and returns the sum of the squares of the elements. The list nodes are of class LLNode
Example:

Assume that the list is not empty.
You have received quite a variety of solutions. Grade the methods below, marking errors where you see them.
a)
int sumSquares ( LLNode
{
return 0;
if ( list != null )
return ( list.getInfo( )
* list.getInfo( )
+ sumSquares ( list.getLink( ) ) ) ;
}
b)
int sumSquares ( LLNode
{
int sum = 0;
while ( list != null )
{
sum = list.getInfo( ) + sum;
list = list.getLink( ) ;
}
return sum;
}
c)
int sumSquares ( LLNode
{
if ( list == null )
return 0;
else
return list.getInfo( ) * list.getInfo( )
+ sumSquares ( list.getLink( ) ) ;
}
d)
int sumSquares ( LLNode
{
if ( list . getLink( ) == null )
return list.getInfo( ) *
list.getInfo( ) ;
else
return list.getInfo( ) * list.getinfo( )
+ sumSquares ( list.getLink( ) ) ;
}
e)
int sumSquares ( LLNode
{
if ( list == null )
return 0;
else
return ( sumSquares ( list.getLink( ) ) *
sumSquares ( list.getLink( ) ) ) ;
}
list 1 S s umSquares(1ist) yields (5 5)(2 *2)+33)+(1 *1) 39
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
