Question

In: Computer Science

java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1....

java programming

Concepts

ArrayList - Collections

Sorting

Enhanced For Loop

Collections Class

Auto-boxing

Programming Assignment

1. 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 names to the collection. "Bob" "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 name 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.

Submission.

Please submit .java file(s)

Solutions

Expert Solution


import java.util.*;
public class Main
{
   public static void main(String[] args) {
       System.out.println("Hello World");
       //1
       //auto-boxing and un-boxing are used for auto-typcasting from primitive type to object and object to primitive type
      
       //auto-boxing
       Integer n = 24;
       //un-boxing
       int m = n;
       System.out.println(n+" "+m);
      
       //2
       ArrayList<String> a1= new ArrayList<String>();
       //adding 5 names
a1.add("Bob");
a1.add("Susan");
a1.add("John");
a1.add("Sherlock");
a1.add("Bobby");
  
//outputing names through loop
for(int i=0;i<a1.size();i++)
System.out.println(a1.get(i));
//3
//sorting
Collections.sort(a1);
//printing list
System.out.println("Sorted list :"+a1);
//shuffling list
Collections.shuffle(a1);
System.out.println("Shuffled list :"+a1);
  
//4
//searching for Susan
int k=-1;
for(int i=0;i<a1.size();i++)
if(a1.get(i).equals("Susan"))//here equals is used to compare two string objects
k=i;//if found
if(k<=0)System.out.println("Susan not found");
else System.out.println("Susan found at index : "+k);
  
//searching for string that is not in the list
k=-1;
for(int i=0;i<a1.size();i++)
if(a1.get(i).equals("Fisher"))//here equals is used to compare two string objects
k=i;//if found

if(k<=0)System.out.println("Fisher not found");
else System.out.println("Fisher found at index : "+k);
  
//5 //here equals is used to compare two string objects
  
  
//6
//converting to array
  
Object a[] = a1.toArray();
//printing Array
for(int i=0;i<a.length;i++)
System.out.println(a);
  
  
  
}
}


Related Solutions

Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList are the same data structure but implement data storage in different ways. Classes, that implement Map interface in Java collections framework are used for storing what type of data? Declare and instantiate a list of elements of type String. Name this list myArray. what type of data structure is Stack? LinkedList data structure in Java collections is implemented as doubly linked lists. In PriorityQueue...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye”...
Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display...
Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept an array...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1 to 1000. Then remove every even number. Then remove every multiple of 3 remaining Then remove every multiple of 5 Then remove every multiple of 7 Then sum the array, and print.
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
This program focuses on programming with Java Collections classes. You will implement a module that finds...
This program focuses on programming with Java Collections classes. You will implement a module that finds a simplified Levenshtein distance between two words represented by strings. Your program will need support files LevenDistanceFinder.Java, dictionary.txt, while you will be implementing your code in the LevenDistanceFinder.java file. INSTRUCTIONS The Levenshtein distance, named for it's creator Vladimir Levenshtein, is a measure of the distance between two words. The edit distance between two strings is the minimum number of operations that are needed to...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing a list of integers. The following public methods are provided: ? IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0. ? void randomize()—fills the list with random integers between 1 and 100, inclusive. ? void print()—prints the array elements and indices ? int search(int target)—looks for value target in the list using a linear (also called sequential) search algorithm. Returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT