I need part 4 of this assignment done. I have already done parts 1-3 and the cod
ID: 3912896 • Letter: I
Question
I need part 4 of this assignment done. I have already done parts 1-3 and the code for that is given below. Feel free to change anything. Thank you, will upvote.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class Bucket {
// start char of the bucket
private char minInitial;
// end char of the bucket
private char maxInitial;
// list holds all the words in one particular bucket
private LinkedList<String> list;
// outputfile to store the results
private static File outfile = null;
// Bucket constructor.
public Bucket(char min, char max) {
this.maxInitial = max;
this.minInitial = min;
// initializes a new LinkedList to hold words for that bucket
list = new LinkedList<String>();
}
public static void main(String args[]) {
// test sorting of Integer array
sortIntegerList();
Scanner sc = new Scanner(System.in);
System.out.println("Enter file name to read data from");
String inputFileName = sc.nextLine();
System.out.println("Enter file name to write data to");
String outputFileName = sc.nextLine();
createOutputFile(outputFileName);
String[] wordsArray = null;
ArrayList<Bucket> buckets = new ArrayList<Bucket>();
buckets.add(new Bucket('a', 'b'));
buckets.add(new Bucket('c', 'c'));
buckets.add(new Bucket('d', 'f'));
buckets.add(new Bucket('g', 'k'));
buckets.add(new Bucket('l', 'o'));
buckets.add(new Bucket('p', 'r'));
buckets.add(new Bucket('s', 's'));
buckets.add(new Bucket('t', 'z'));
try {
BufferedReader br = new BufferedReader(new FileReader(inputFileName));
String line = "";
String demlimiter = ",";
while ((line = br.readLine()) != null) {
wordsArray = line.trim().replaceAll("\s", "").toLowerCase().split(demlimiter);
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("Input file not found" + e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
// add words to the correct bucket for sorting
addToBucket(wordsArray, buckets);
// sort the strings in the bucket
sortStringBuckets(outputFileName, buckets);
sc.close();
}
// Creates an integer array with some values and sorts them with Arrays.sort
// Prints the value of array before and after the sorting
private static void sortIntegerList() {
int[] intList = { 23, 455, 66, 2, 56, 1 };
System.out.println("Integer list before sorting");
for (int i : intList) {
System.out.println(i);
}
Arrays.sort(intList);
System.out.println("Integer list after sorting");
for (int i : intList) {
System.out.println(i);
}
}
// Creates an output file for storing the result. FOr each inputfile a new file
// is created.Before starting it checks if the file already exists it delets the
// old one
// and creates a new file (for clean start)
private static void createOutputFile(String outputFileName) {
outfile = new File(outputFileName);
// If already exists delete and create a new
if (outfile.exists()) {
outfile.delete();
try {
outfile.createNewFile();
} catch (IOException e) {
System.out.println("Output file not found" + e.getMessage());
}
}
}
// For every bucket it takes the linkedList<String> one by one and sort each
// list individually
private static void sortStringBuckets(String outputFileName, ArrayList<Bucket> buckets) {
for (int i = 0; i < buckets.size(); i++) {
// get list for this bucket
LinkedList<String> list = buckets.get(i).getList();
// create array from linkedlist
String[] array = list.toArray(new String[list.size()]);
// sort the array
Arrays.sort(array);
// write to the output file
writeToFile(outputFileName, buckets, i, array);
}
System.out.println("Writting to the file is done!!");
}
// Write sorted List to the output file .
private static void writeToFile(String outputFileName, ArrayList<Bucket> buckets, int i, String[] array) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(outfile, true));
for (int j = 0; j < array.length; j++) {
// if this is the last element of all the buckets then no need to add comma in
// the end
if (i == buckets.size() - 1 && j == array.length - 1) {
out.write(array[j]);
} else {
out.write(array[j] + ",");
}
}
} catch (IOException e1) {
System.out.println("Writting to file failed." + e1.getMessage());
} finally {
try {
// close outputBuffer
out.close();
} catch (IOException e) {
System.out.println("Output buffer can't close." + e.getMessage());
}
}
}
// Get all the Strings obtained from the input file and put them in the correct
// bucket
private static void addToBucket(String[] wordsArray, ArrayList<Bucket> buckets) {
for (int i = 0; i < wordsArray.length; i++) {
// take one word at a time and find the correct bucket for it
String str = wordsArray[i];
char letter = str.toLowerCase().charAt(0);
if (letter == 'a' || letter == 'b') {
buckets.get(0).list.add(str);
} else if (letter == 'b') {
buckets.get(1).list.add(str);
} else if (letter >= 'd' && letter <= 'f') {
buckets.get(2).list.add(str);
} else if (letter >= 'g' && letter <= 'k') {
buckets.get(3).list.add(str);
} else if (letter >= 'l' && letter <= 'o') {
buckets.get(4).list.add(str);
} else if (letter >= 'p' && letter <= 'r') {
buckets.get(5).list.add(str);
} else if (letter == 's') {
buckets.get(6).list.add(str);
} else if (letter >= 't' && letter <= 'z') {
buckets.get(7).list.add(str);
}
}
System.out.println("Adding to the bucket is done!!");
}
// return the linkedList
public LinkedList<String> getList() {
return list;
}
}
Explanation / Answer
Array implementation: public class ResizingArrayQueue { private String[] q; private int N = 0, head = 0, tail = 0; public ResizingArrayQueue() { q = new String[1]; } public void enqueue(String item) { // double size of array if (N == q.length) resize(2*q.length); q[tail++] = item; N++; if (tail == q.length) tail = 0; } public String dequeue() { String item = q[head]; q[head++] = null; N--; if (head == q.length) head = 0; // shrink size of array if (N > 0 && N == q.length/4) resize(q.length/2); return item; } }