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

I need part 4 of this assignment done. I have already done parts 1-3 and the cod

ID: 3912674 • 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.

Code that I have completed for parts 1-3:

////////////////////////////////////////////Bucket.java/////////////////////////////////////////////////////////////////////////////////////

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;

}

}

//////////////////////////////////////////////////End Bucket.java//////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////input.txt/////////////////////////////////////////////////////////////////////////////////////////////

In, Java ,we ,can, append, a ,string, in, an ,existing ,file, using ,FileWriter, which ,has, an, option ,to ,open, file, in, append, mode

////////////////////////////////end input.txt/////////////////////////////////////////////////////////////////////////////

In this assignment, you are asked to write a program that reads a series of strings sepa- ratod with comma from a given file. Your program needs to alphabetically sort the strings using bucket sort and write the result in the output file. The strings in the output must be separáted with comma. The names and addresses of input and output files are given by the user through console at the beginning of the program. 1 Bucket Sort Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements into a number of buckets. Each bucket is then sorted individually. To sort each bucket in this assignment, you need to use the sort() method in jana util Arrags class. 0-9 10-19 20-29 30-39 40-4 29 25 3 49 9 37 21 43 37 43 49 29 25 3749 21 10-1, 20-29 x -19 404,-> 3 9 21 25 29 37 43 49 Figure I: Exumple of bucket sort for sorting mumbers 29, 25, 3, 19, 9, 37, 21, 13. Numbers distributod aumong bins (left image). Then, elements are sorted within each bin (right are image)

Explanation / Answer

I am giving answer to part 4 in which queue is to be implemented in 2 ways i.e. circular and using 2 stacks

1. circular queue giving options to insert delete JAVA code

import java.io.*;

class CircularQ

{

int Q[] = new int[100]; // defining capacity of our queue

int n, front, rear;

static BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

public CircularQ(int nn)

{

n=nn;

front = rear = 0; // intialising positions

}

public void add(int v)

{

if((rear+1) % n != front)

{

rear = (rear+1)%n;

Q[rear] = v;

}

else

System.out.println("Queue is full !");

}

public int del()

{

int v;

if(front!=rear)

{

front = (front+1)%n;

v = Q[front];

return v;

}

else

return -9999;

}

public void disp()

{

int i;

if(front != rear)

{

i = (front +1) %n;

while(i!=rear)

{

System.out.println(Q[i]);

i = (i+1) % n;

}

}

else

System.out.println("Queue is empty !");

}

public static void main(String args[]) throws IOException

{

System.out.print("Enter the size of the queue : ");

int size = Integer.parseInt(br.readLine());

CircularQ call = new CircularQ(size);

int choice;

boolean exit = false;

while(!exit)

{

System.out.print(" 1 : Add 2 : Delete 3 : Display 4 : Exit Your Choice : ");

choice = Integer.parseInt(br.readLine());

switch(choice)

{

case 1 :

System.out.print(" Enter number to be added : ");

int num = Integer.parseInt(br.readLine());

call.add(num);

break;

case 2 :

int popped = call.del();

if(popped != -9999)

System.out.println(" Deleted : " +popped);

else

System.out.println(" Queue is empty !");

break;

case 3 :

call.disp();

break;

case 4 :

exit = true;

break;

default :

System.out.println(" Wrong Choice !");

break;

}

}}}

2. using 2 stacks Iam implementing a queue in JAVA