Question
Can someone please review my Java code? I keep getting errors when I try to run it, but I don't know what is wrong. import
Can someone please review my Java code? I keep getting errors when I try to run it, but I don't know what is wrong.
import java.util.*;
public class BaseMerging { /** * Entry point for sample output. * * @param args the command line arguments */ public static void main(String[] args) { Queue q1 = new ListQueue(); q1.enqueue("T"); q1.enqueue("R"); q1.enqueue("O"); q1.enqueue("L"); q1.enqueue("E"); Queue q2 = new ListQueue(); q2.enqueue("X"); q2.enqueue("S"); q2.enqueue("P"); q2.enqueue("M"); q2.enqueue("E"); q2.enqueue("A"); Queue q3 = new ListQueue(); q3.enqueue(20); q3.enqueue(17); q3.enqueue(15); q3.enqueue(12); q3.enqueue(5); Queue q4 = new ListQueue(); q4.enqueue(18); q4.enqueue(16); q4.enqueue(13); q4.enqueue(12); q4.enqueue(4); q4.enqueue(1); //Q1 - sample test cases Queue merged1 = mergeQueues(q1, q2); System.out.println(merged1.toString()); Queue merged2 = mergeQueues(q3, q4); System.out.println(merged2.toString()); //Q2 - sample test cases String[] a = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"}; sort(a); assert isSorted(a); show(a); //Q3 - sample test cases String[] b = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"}; shuffle(b); show(b); shuffle(b); show(b); } public static Queue mergeQueues(Queue
while(!q1.isEmpty()) { a.enqueue(q1.dequeue()); }
while(!q2.isEmpty()) { a.enqueue(q2.dequeue()); }
return a; } public static void sort(Comparable[] a) {
Comparable b = mergesort(a);
for(int i = 0; i
a[i] = b[i];
} }
public static Comparable[] mergesort(Comparable[] a) {
if (a.length
return a;
int center = a.length / 2; Comparable[] a1 = new Comparable[center]; Comparable[] a2 = new Comparable[a.length - center]; for(int i = 0; i
for(int i = center, j = 0; i
a1 = mergesort(a1); a2 = mergesort(a2);
a = merge(a1, a2);
return a; }
public static Comparable[] merge(Comparable[] a, Comparable[] b) {
Comparable[] merge = new Comparable[a.length + b.length];
int i = 0; int j = 0; int k = 0;
while(i 0) { merge[k] = b[j]; k++; j++; } else { merge[k] = a[i]; k++; i++; merge[k] = b[j]; k++; j++; } } else if(i = b.length) { merge[k] = a[i]; k++; i++; } else if(j = a.length) { merge[k] = b[j]; k++; j++; } } return merge; }
public static void shuffle(Object[] a) { shuffle(a, 0, a.length - 1); }
private static void shuffle(Object[] a, int first, int last) { if(first > last) return; int mid = (first + last) / 2; int rand = (new Random()).nextInt(a.length); Object midEl = a[mid]; a[mid] = a[rand]; a[rand] = midEl; shuffle(a, first, mid - 1); shuffle(a, mid + 1, last); }
//sorting helper from text private static boolean less(Comparable v, Comparable w) { return v.compareTo(w)
//sorting helper from text private static void show(Comparable[] a) { for (Comparable a1 : a) System.out.print(a1 + " ");
System.out.println(); } //sorting helper from text public static boolean isSorted(Comparable[] a) { for (int i = 1; i public static void main(String[] args) { Queue q1 = new ListQueue(); 91.enqueue("T"); q1. enqueue("R"); 21. enqueue("0"); q1.enqueue("L"); 41. enqueue("E"); Queue q2 = new ListQueue (); 92.enqueue("X"); q2.enqueue("S"); 92. enqueue("P"); q2.enqueue ("M"); 92. enqueue("E"); q2.enqueue("A"); Queue q3 = new ListQueue(); q3.enqueue (20); q3.enqueue(17); q3.enqueue (15); 43.enqueue (12); q3.enqueue(5); Queue 94 = new ListQueue(); 44.enqueue (18); 44.enqueue(16); 44.enqueue (13); 44. enqueue(12); 44.enqueue (4); 94.enqueue (1); //01 - sample test cases Queue merged1 = mergeQueues (q1, 92); System.out.println(merged1.toString()); Queue merged2 = mergeQueues (q3, 4); System.out.println(merged2.toString()); //Q2 - sample test cases String[] a = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"}; sort(a); assert issorted(a); show(a); //Q3 - sample test cases String [] b = {"S", "0", "R", "T", "E", "X", "A", "M", "P", "L", "E"); shuffle(b); show(b); shuffle(b); show(b); } 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 public static Queue mergeQueues (Queue
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started