Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java. Please implement in the code I have below and please add comments so I can understand what is being done. Code I have

In Java. Please implement in the code I have below and please add comments so I can understand what is being done.

Code I have so far:

import java.io.*; import java.util.*; public class Lab4 { /** * Problem: Sort multi-digit integers (with n total digits) in O(n) time. * (Technically, it is O(n * b) time. However, since our base b = 128 is constant, it is O(n).) */ private static void problem(byte[][] arr) { // Implement me! } // --------------------------------------------------------------------- // Do not change any of the code below! private static final int LabNo = 4; private static final Random rng = new Random(654321); private static boolean testProblem(byte[][] testCase) { byte[][] numbersCopy = new byte[testCase.length][]; // Create copy. for (int i = 0; i { numbersCopy[i] = testCase[i].clone(); } // Sort problem(testCase); Arrays.sort(numbersCopy, new numberComparator()); // Compare if both equal if (testCase.length != numbersCopy.length) { return false; } for (int i = 0; i { if (testCase[i].length != numbersCopy[i].length) { return false; } for (int j = 0; j { if (testCase[i][j] != numbersCopy[i][j]) { return false; } } }

return true; } // Very bad way of sorting. private static class numberComparator implements Comparator { @Override public int compare(byte[] n1, byte[] n2) { // Ensure equal length if (n1.length { byte[] tmp = new byte[n2.length]; for (int i = 0; i { tmp[i] = n1[i]; } n1 = tmp; } if (n1.length > n2.length) { byte[] tmp = new byte[n1.length]; for (int i = 0; i { tmp[i] = n2[i]; } n2 = tmp; } // Compare digit by digit. for (int i = n1.length - 1; i >=0; i--) { if (n1[i] if (n1[i] > n2[i]) return 1; } return 0; } } public static void main(String args[]) { System.out.println(\"CS 302 -- Lab \" + LabNo); testProblems(); } private static void testProblems() { int noOfLines = 10000; System.out.println(\"-- -- -- -- --\"); System.out.println(noOfLines + \" test cases.\"); boolean passedAll = true; for (int i = 1; i => { byte[][] testCase = createTestCase(i);[]>

boolean passed = false; boolean exce = false; try { passed = testProblem(testCase); } catch (Exception ex) { passed = false; exce = true; } if (!passed) { System.out.println(\"Test \" + i + \" failed!\" + (exce ? \" (Exception)\" : \"\")); passedAll = false; break; } } if (passedAll) { System.out.println(\"All test passed.\"); } } private static byte[][] createTestCase(int testNo) { int maxSize = Math.min(100, testNo) + 5; int size = rng.nextInt(maxSize) + 5; byte[][] numbers = new byte[size][]; for (int i = 0; i { int digits = rng.nextInt(maxSize) + 1; numbers[i] = new byte[digits]; for (int j = 0; j { numbers[i][j] = (byte)rng.nextInt(128); } // Ensures that the most significant digit is not 0. numbers[i][digits - 1] = (byte)(rng.nextInt(127) + 1); } return numbers; } }

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

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students also viewed these Programming questions

Question

What fonts does Java guarantee you have?

Answered: 1 week ago