Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am doing the same project once posted by another Chegg user (he has provided the instructions) http://www.chegg.com/homework-help/questions-and-answers/need-help-write-following-code-use-bucket-sort-algorithm-sort-list-strings-write-code-file-q24983674 yet I'm still having some difficulties. The

I am doing the same project once posted by another Chegg user (he has provided the instructions)

http://www.chegg.com/homework-help/questions-and-answers/need-help-write-following-code-use-bucket-sort-algorithm-sort-list-strings-write-code-file-q24983674

yet I'm still having some difficulties. The first being is that when initializing the array of queue objects, a lightbulb insists the array is only read from and never written to. The second being when trying to creating a constructor for the queue array, I'm instantiating, which can't be done for a queue (abstract). Code was provided for Queue which uses nodes (linked list) to store data meaning should the array of queue objects be a linked list and use queue based methods instead of array methods as the solution in the link uses a linked list? Here is my meager attempt along with the Queue code, any contribution is greatly appreciated

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Queue; import java.util.Scanner;

public class Bsort { /* private int max = 27; private Queue[] queue = null;

public Bsort(int max) { this.max = max; this.queue = new Queue[this.max]; for(int i = 0;i < max; i++){ this.queue[i] = new Queue(); } } */

public static int charAt(String str, int position) { int a = 0; if (position < str.length()) { a = str.charAt(position) - 96; }

return a; }

public static void sort(ArrayList strings) { //initialize array of 27 objects Queue[] k = new Queue[27];

/*determine the greatest length of a String in the array "strings" */ // Call it 'N' int N = 0; for (String str : strings) { if (str.length() > N) { N = str.length(); } } //set index to N-1 int index = N - 1; for (String s : strings) { int charIndex = charAt(s, index); k[charIndex].add(s); //queue each String into Queue }

//set j = 0 for (int j = 0; j <= 26; j++) { while (!k[j].isEmpty()) { //dequeue an element String element = (String) k[j].remove(); /*write the element to ArrayList strings at position j */ strings.set(j, element); } } }

public static void main(String[] args) { ArrayList arr = new ArrayList<>(); try { Scanner in = new Scanner(new File(args[0])); while (in.hasNextLine()) { arr.add(in.nextLine()); } System.out.println("Unsorted List"); for (String s : arr) { System.out.println(s); } sort(arr); System.out.println(" Sorted List"); for (String s : arr) { System.out.println(s); } } catch (FileNotFoundException f) { System.out.println("File Not Found"); } }

}

//queue code

import java.util.Iterator; import java.util.NoSuchElementException;

public class Queue implements Iterable {

private int N; // number of elements on queue private Node first; // beginning of queue private Node last; // end of queue

// helper linked list class private class Node {

private Item item; private Node next; }

/** * Create an empty queue. */ public Queue() { first = null; last = null; }

/** * Is the queue empty? */ public boolean isEmpty() { return first == null; }

/** * Return the number of items in the queue. */ public int size() { return N; }

/** * Return the number of items in the queue. */ public int length() { return N; }

/** * Return the item least recently added to the queue. Throw an exception if * the queue is empty. */ public Item peek() { if (isEmpty()) { throw new RuntimeException("Queue underflow"); } return first.item; }

/** * Add the item to the queue. */ public void enqueue(Item item) { Node x = new Node(); x.item = item; if (isEmpty()) { first = x; last = x; } else { last.next = x; last = x; } N++; }

/** * Remove and return the item on the queue least recently added. Throw an * exception if the queue is empty. */ public Item dequeue() { if (isEmpty()) { throw new RuntimeException("Queue underflow"); } Item item = first.item; first = first.next; N--; if (isEmpty()) { last = null; // to avoid loitering } return item; }

/** * Return string representation. */ public String toString() { StringBuilder s = new StringBuilder(); for (Item item : this) { s.append(item + " "); } return s.toString(); }

/** * Return an iterator that iterates over the items on the queue in FIFO * order. */ public Iterator iterator() { return new ListIterator(); }

// an iterator, doesn't implement remove() since it's optional private class ListIterator implements Iterator {

private Node current = first;

public boolean hasNext() { return current != null; }

public void remove() { throw new UnsupportedOperationException(); }

public Item next() { if (!hasNext()) { throw new NoSuchElementException(); } Item item = current.item; current = current.next; return item; } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

Know the principles of effective service recovery systems.

Answered: 1 week ago

Question

Explain the service recovery paradox.

Answered: 1 week ago