Question

In: Computer Science

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

Exercise 1

Modify the List<T> class of Figure 21.3 in the textbook to include a method that recursively searches a linked-list for a specified value. Ensure that the name of your method includes your last name. The method must return a reference to the value if it is found; otherwise, it must return null. Use your method in a test program that creates a list of integers. The program must prompt the user for a value to locate in the list.

I need the code modified in java to pull out the correct input below is my code, but when ran the only out I get is "Enter integers(-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> T search_mylastname(List<T> list, T value, int index){//function name can be updated in all places to the 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<Integer> 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");
}
}

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static <T> T search_mylastname(List<T> list, T value, int index){//function name can be updated in all places to the 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<Integer> 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");
}
}

Output:

The output when the above code is run is attached below.

Please check it.

Explanation:

When vthe program is run, the output displayed is "Enter integers(-1 to stop):2,4,6,8,10"

Then the input numbers must be entered.

The numbers entered are as follows.

2

4

6

8

10

-1

When -1 is entered, the statement "Enter integer to search: " is displayed.

Then the number 5 is entered by us.

As 5 is not in the numbers provided, it displays the result as "5 is not in list."


Related Solutions

Exercise 1 Modify the List class of Figure 21.3 in the textbook to include a method...
Exercise 1 Modify the List class of Figure 21.3 in the textbook to include a method that recursively searches a linked-list for a specified value. Ensure that the name of your method includes your last name. The method must return a reference to the value if it is found; otherwise, it must return null. Use your method in a test program that creates a list of integers. The program must prompt the user for a value to locate in the...
Instructions Modify the Product class from the Practice Exercise 7 by adding a quantity member. Include...
Instructions Modify the Product class from the Practice Exercise 7 by adding a quantity member. Include a getter and setter, using decorators, and modify the appropriate constructor to also accept a quantity parameter. Then modify the inventory.py file from the practice exercise to include quantity values in the constructor calls with a quantity of 100 for product1 (hammers) and 3000 for product2 (nails). Add print statements to display the quantity values as shown in the Expected Output included below. Submission...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class. Use the following code to test your work. public static void testCode()         {             SavingsAccount savings = new SavingsAccount(100.00, 3.5);             SavingsAccount s = (SavingsAccount)savings.ReadAccount();...
1) Modify all methods that have used count, especially rewrite length method by traversing the list...
1) Modify all methods that have used count, especially rewrite length method by traversing the list and counting no. of elements. import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;       ...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print Make your unorderedList a list of characters instead of integers Insert ten characters into the list and print it out both ways #include <iostream> #include <string> #include <cstdlib> using namespace std; struct node { int info; node* next; }; class unorderedList { private: int length; node* listPtr; public: unorderedList() {length = 0; listPtr = NULL;} void makeEmpty(); void insertItem(int item); void printList(); bool isFull()...
Research the topic of corporate Personhood and review Chapter 21.3 Exercise 1. Do you think that...
Research the topic of corporate Personhood and review Chapter 21.3 Exercise 1. Do you think that corporations should have rights similar to natural persons? To what extent and why/why not? PLEASE DO NOT COPY AND PASTE OTHER STUDENT WORK THAT WAS SUBMITTED BEFORE. 200 words minimum
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee to represent when they joined the company. Use the Joda-Time library class LocalDate as the type for this variable. See http://www.joda.org/joda-time/index.html for details end examples of how to use this library. You may also have to download the Joda-Time library (JAR File) and include this in your CLASSPATH or IDE Project Libraries. Use a static variable in the Employee class to help automatically assign...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT