Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribedimage text in transcribedimage text in transcribed

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 q1, Queue q2) { Queue a = new ListQueue(); while(!q1.isEmpty() && !q2.isEmpty()) { if(!less((Comparable) q1.front(), (Comparable) q2.front())) { a.enqueue(q1.dequeue()); } else { a.enqueue(q2.dequeue()); } }

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 a1, Queue q2) { Queue a = new ListQueue(); while(!ql.isEmpty() && !42. isEmpty()) { if(!less((Comparable) q1. front(), (Comparable) 22. front())) { a.enqueue (91.dequeue()); } else { a.enqueue (22.dequeue()); } } 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 0) { result[k] = b[j]; else { result[k] a[i]; i++; result[k] = b[j]; } 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 else if(i = b. length) { result[k] = a[i]; i++; } else if(j >= a. length) { result[k] = b[j]; j++; } } return result; } public static void shuffle(object[] a) { shufflela, 0, a. length - 1); } private static void shuffle(Object[] a, int first, int last) { if(first > last) return; return; 170 171 172 173 174 175 176 int middle = (first + last) / 2; int randIndex (new Random()).nextInt(a. length); Object middleElement = a (middle); a[middle] = a[randIndex]; a[randIndex] = middleElement; 177 178 179 // recursive calls shuffle(a, first, middle - 1); shuffle(a, middle + 1, last); } 180 181 182 183 184 185 186 187 188 189 190 191 //sorting helper from text private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) a1, Queue q2) { Queue a = new ListQueue(); while(!ql.isEmpty() && !42. isEmpty()) { if(!less((Comparable) q1. front(), (Comparable) 22. front())) { a.enqueue (91.dequeue()); } else { a.enqueue (22.dequeue()); } } 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 0) { result[k] = b[j]; else { result[k] a[i]; i++; result[k] = b[j]; } 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 else if(i = b. length) { result[k] = a[i]; i++; } else if(j >= a. length) { result[k] = b[j]; j++; } } return result; } public static void shuffle(object[] a) { shufflela, 0, a. length - 1); } private static void shuffle(Object[] a, int first, int last) { if(first > last) return; return; 170 171 172 173 174 175 176 int middle = (first + last) / 2; int randIndex (new Random()).nextInt(a. length); Object middleElement = a (middle); a[middle] = a[randIndex]; a[randIndex] = middleElement; 177 178 179 // recursive calls shuffle(a, first, middle - 1); shuffle(a, middle + 1, last); } 180 181 182 183 184 185 186 187 188 189 190 191 //sorting helper from text private static boolean less(Comparable v, Comparable w) { return v.compareTo(w)

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_2

Step: 3

blur-text-image_3

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions