Preliminaries Download the driver and the Lorem Ipsum text file and place them i
ID: 668277 • Letter: P
Question
Preliminaries
Download the driver and the Lorem Ipsum text file and place them in your project directory.
Synopsis
Specification
Create a class ArrayListUtils with the following (static) methods:
removeDuplicates Removes duplicates from an ArrayList of Characters. The remaining character from the duplicates is always the first occurrenceparams
data The ArrayList to remove duplicates from.
return An ArrayList with no duplicate entries remaining.
getCommonStrings Finds the strings common to both given ArrayLists. If both lists contain the same entry multiple times, the resulting list should contain the entry exactly once.params
list1 An ArrayList of Strings
list2 An ArrayList of Strings
return An ArrayList of Strings common to both list1 and list2
wordRecall Reads in lines from a text file, then asks the user for a line number and a word number. Then, does one of the following:
If the number/word choice is invalid, displays an appropriate error message
If the user enters 'quit', exits the loop (but not the program)
Otherwise, prints the word at the given position
param
fname The name of the file to read in.
Your class should not have a main method
Note
You are not allowed the use of any arrays, even if you do not directly create them. For example, the split method on strings returns an array, and therefore is not allowed.
Example Dialog:
The driver:
Explanation / Answer
class ArrayListUtils {
public static removeDuplicates(ArrayList<Character> array)
{
//This new list created is calling the Hashset which do not allow the duplicate element in to the list
List newList = new ArrayList(new HashSet( array));
//Iterating through each element and printing the non duplicate values.
Iterator it = newList.iterator();
while (it.hasNext())
{
System.out.println(it.next());
}
}
public static getCommonStrings(ArrayList<Character> array1,ArrayList<Character> array2)
{
/*We created the two arraylist as the function called from the main method will have two arguments of the
arraylist type.
The retainAll() method is used to remove it's elements from a list that are not contained in the specified
collection.
array1.retainAll(array2);
array2.retainAll(array1);
System.out.println(array2); //Printing the elements which are in common.
}
//As the passes paramenter is of the type text file,hence we have declared it as File variable
public static wordRecall (File f)
{
int count=0,word=0;
// This will add the lines to the string as the read of line continiues.
List<String> total = new ArrayList<String>();
try
{
//Opening a file
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null)
{
count++;
total.add(line); //adding lines to arraylist of string
if(count==x) //matching the user input line number with the current one.
{
for(String s:line) //Loopinf to find the word number
{
words += s.split(" ").length; //Splitting the word on space till end of line is reached.
if(words==y) //Check the word number with the current looped number.
System.out.println(s); //Printing the given matched criteria word.
}
else
System.out.println("The line number not found");
}
}
br.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}