Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming: Question 1: Write Java code using the List interface to implem

ID: 671597 • Letter: J

Question

Java Programming:

Question 1:

Write Java code using the List interface to implement the following function

//returns true if a is a subset of b

//returns false otherwise

public static <E> boolean subset(List<E> a, List<E>, b){

Question 2:

Implement the following method of the list interface using the ArrayList<E>

implementation of List<E>. The method should return the object that is removed from the List.

E remove(int index){

Question 3:

Implement the following method of the list interface using the LinkedList<E>

implementation of List<E>. The method should return the object that is removed from the List.

E remove(int index){

Explanation / Answer

Question 1 (Answer):

import java.util.ArrayList;
import java.util.List;


public class SubsetInList {

   public static void main(String a[]){
   List<String> list1 = new ArrayList<String>();
   //adding elements to the end
   list1.add("First");
   list1.add("Second");
   list1.add("Third");
   list1.add("Random");
   list1.add("Click");
   System.out.println("Actual ArrayList:"+list1);
   List<String> list = list1.subList(2, 4);
   System.out.println("Sub List: "+list);
   System.out.println("Is the first list is subset of another list " + isSubset(list1, list));
  
   List<String> list3 = new ArrayList<String>();
   //adding elements to the end
   list3.add("Lavanya");
   list3.add("Nivetha");
   list3.add("Keerthana");
   list3.add("Sam");
   list3.add("Lawrence");
   System.out.println("Is the first list is subset of another list " + isSubset(list1, list3));
  
  
   }
   private static boolean isSubset(List<String> list1, List<String> list2) {
       if (list1.containsAll(list2))
           return true;
       return false;
   }

}

Question 2:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class ListDelete {

   public static void main(String[] args) {
      
       List<String> list1 = new ArrayList<String>();
//adding elements to the end
list1.add("First");
list1.add("Second");
       list1.add("Third");
       list1.add("Random");
       list1.add("Click");
       do {
           try {
               System.out.println(" Elements in the list are : " + list1);
               int range = list1.size() - 1;
               System.out
                       .println("Enter the index of number which you want to delete. But the range should be (0, "
                               + range + "): ");
               Scanner in = new Scanner(System.in);
               Integer index = Integer.parseInt(in.nextLine());
               if (index.intValue() >= 0 || index.intValue() < list1.size()) {
                   System.out.println("This is the nuber going to delete: "
                           + list1.remove(index.intValue()));
               } else {
                   System.out
                           .println("Sorry ! your have entered wrong value.");
               }
           } catch (IndexOutOfBoundsException e) {
               System.out.println("Sorry ! your have entered wrong value.");
           }

       } while (list1.size() > 0);
      
       System.out.println("Deleted all the elements in the list. Thank You!");
   }

}

Question 3:

import java.util.LinkedList;
import java.util.Scanner;


public class LinkedlistRemove {

   public static void main(String[] args) {
       // create a linked list
   LinkedList<String> linkedlist = new LinkedList<String>();
   // add elements to the linked list
   linkedlist.add("F");
   linkedlist.add("B");
   linkedlist.add("D");
   linkedlist.add("E");
   linkedlist.add("C");
   linkedlist.addLast("Z");
   linkedlist.addFirst("A");
   linkedlist.add(1, "A2");
   do {
               try {
                   System.out.println(" Elements in the list are : " + linkedlist);
                   int range = linkedlist.size() - 1;
                   System.out.println("Enter the index of number which you want to delete. But the range should be (0, " + range + "): ");
                   Scanner in = new Scanner(System.in);
                   Integer index = Integer.parseInt(in.nextLine());
                  
                   // Deleting the element based on the indexvalue
                   if (index.intValue() >= 0 || index.intValue() < linkedlist.size()) {
                       //To delete first element in the list
                       if (index.intValue() == 0 ) {
                           System.out.println("to delete the first element in linked list We can use 'removeFirst()' method");
                           System.out.println("This is the nuber going to delete: "+ linkedlist.removeFirst());
                       //To delete last element in the list
                       } else if (index.intValue() == linkedlist.size() - 1) {
                           System.out.println("to delete the last element in linked list We can use 'removeLast()' method");
                           System.out.println("This is the nuber going to delete: " + linkedlist.removeLast());
                       } else {
                           System.out.println("This is the nuber going to delete: "+ linkedlist.remove(index.intValue()));
                       }
                      
                   } else {
                       System.out.println("Sorry ! your have entered wrong value.");
                   }
               } catch (IndexOutOfBoundsException e) {
                   //If the user entered index value out of the range
                   System.out.println("Sorry ! your have entered wrong value.");
               }

           } while (linkedlist.size() > 0);
          
           System.out.println("Deleted all the elements in the list. Thank You!");

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote