Exercise 1 Modify the List<T> class of Figure 21.3 in the textbook to include a method that...

50.1K

Verified Solution

Question

Programming

Exercise 1

Modify the List class of Figure 21.3 in the textbook toinclude a method that recursively searches a linked-list for aspecified value. Ensure that the name of your methodincludes your last name. The method must return areference to the value if it is found; otherwise, it must returnnull. Use your method in a test program that creates a list ofintegers. The program must prompt the user for a value to locate inthe list.

I need the code modified in java to pull out the correct inputbelow is my code, but when ran the only out I get is \"Enterintegers(-1 to stop):\" and nothing else

PLEASE HELP

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static T search_mylastname(List list, Tvalue, int index){//function name can be updated in all places tothe lastname if needed
  
if(list == null || index == list.size())
return null;
  
if(value.equals(list.get(index)))
return list.get(index);
  
return search_mylastname(list, value, index+1);
  
}
  
public static void main(String[] args) {
  
List list = new ArrayList<>();
  
System.out.println(\"Enter integers(-1 to stop): \"
       + \"2, \"
       + \"4, \"
       + \"6, \"
       + \"8, \"
       + \"10\");
int num;
Scanner sc = new Scanner(System.in);
while(true){
num = sc.nextInt();
if(num == -1)
break;
list.add(num);
}
  
System.out.print(\"Enter integer to search: \");
int key = sc.nextInt();
Integer res = search_mylastname(list, key, 0);
if(res == null)
System.out.println(key+\" is not in list\");
else
System.out.println(key+\" is available in list\");
}
}

Answer & Explanation Solved by verified expert
3.6 Ratings (287 Votes)
import javautilArrayListimport javautilListimport javautilScannerpublic class Main public static T searchmylastnameList list Tvalue int indexfunction name can be updated in all places tothe lastname if needediflist null    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students