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

Please i need some help with my programing assigment in Java. Know this inherita

ID: 3863907 • Letter: P

Question

Please i need some help with my programing assigment in Java.

Know this inheritance hierarchy these are interfaces (not classes It is possible for an interface to extend an interface. This allows the designers to define methods that are in common to all of the sub-interfaces. Collection Map List Set Queue Deque SortedMap SortedSet parent interface is Collection sub-interfaces Set, List, Queue Degue. Seit has a sub-interface Sorted Set The Map interface has a sub-interface SortedMap Programming Exercises: Part 1. l. Describe auto-boxing, including why it is useful. (Google for this one) Write a few lines of code that auto-box an int into an Integer, and un-box an Integer to an int. 2. Declare an ArrayList of Strings. Add 5 es to the collection. "B "Susan" Output the Strings onto the console using the enhanced for loop 3. Sort the list using the method Collections,sort, Output the sorted List. Shuffle the list, and output the shuffled list. Note that Collections (with an s) is a class, while Collection is an interface. The Collections class has many useful static methods for processing interfaces, including the sort method. 4. Search for the name "Susan" in the list. What location was it found? Search for a names that is not in the list. What location is reported? 5. Describe why an equals method and a compareTo method are required to achieve searching and sorting of the elements of a list. 6. Convert the list above to an array using toArray, Output the elements of the array. Convert the array back into a list using asList, output the elements of the list 7. Describe the differences between a HashSet, TreeSet, and a LinkedHashSet, When might you choose each one of these? 8. Describe the differences between a HashMap, TreeMap, and LinkedHashMap, Describe the situation when might you use each of these?

Explanation / Answer

1)
Auto boxing is a process by which the java compiler will convert the primitive data types into its corresponding wrapper classes. One of its main advantage is that we cannot handle primitive data types as objects, thus auto boxing can help us to use the int value as an object and thus we can handle it as a normal java object.

Examples:

       int n = 3;
      
       Integer anInteger = n;   //auto boxing

       int i = anInteger;   //auto un-boxing

2)

       ArrayList<String> names = new ArrayList<String>();
      
       names.add("Bob");
       names.add("Susan");
       names.add("Alice");
       names.add("John");
       names.add("Zac");
      
       for(String name : names){   //enhanced for loop
           System.out.println(name);
       }


3)

import java.util.ArrayList;
import java.util.Collections;

class Test {
  
   public static void main(String args[]){
      
       ArrayList<String> names = new ArrayList<String>();
      
       names.add("Bob");
       names.add("Susan");
       names.add("Alice");
       names.add("John");
       names.add("Zac");
      
       //List elements before sorting
       System.out.println(" List elements before sorting:");
       for(String name : names){
           System.out.println(name);
       }

       Collections.sort(names);

       //List elements after sorting
       System.out.println(" List elements after sorting:");
       for(String name : names){
           System.out.println(name);
       }

       Collections.shuffle(names);

       //List elements after shuffling
       System.out.println(" List elements after shuffling:");
       for(String name : names){
           System.out.println(name);
       }

   }  
}

OUTPUT:


List elements before sorting:
Bob
Susan
Alice
John
Zac

List elements after sorting:
Alice
Bob
John
Susan
Zac

List elements after shuffling:
Alice
John
Bob
Zac
Susan


4)

If the searched element is not found in the list, the indexOf function will return -1, else it will return the corresponding position of the element in the list(starting with 0)

import java.util.ArrayList;
import java.util.Collections;

class Test {
  
   public static void main(String args[]){
      
       ArrayList<String> names = new ArrayList<String>();
      
       names.add("Bob");
       names.add("Susan");
       names.add("Alice");
       names.add("John");
       names.add("Zac");

       System.out.println("Susan location in list : "+names.indexOf("Susan"));
       System.out.println("Finn location in list : "+names.indexOf("Finn"));

   }  
}

OUTPUT:
Susan location in list : 1
Finn location in list : -1