Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA ARRAY ASSIGNMENT HELP PLEASE This week, it is time to practice arrays so that our methods can process millions of items of data, giving

JAVA ARRAY ASSIGNMENT HELP PLEASE

This week, it is time to practice arrays so that our methods can process millions of items of data, giving our loops and conditions something useful to do. Create a class ArrayProblems and write the following array methods in it. Again, you must use the JUnit test class TestArrayProblems.java to test out your methods.

You have to start the code with "void squeezeLeft(int[] a)" that modifies the contents of its parameter array a by moving all its nonzero elements to the left as far as it can, writing over the zero elements that precede them. For example, if called with the parameter array {0, 1, 0, 0, 3, -2, 0, 7}, the contents of this array after the call would be {1, 3, -2, 7, 0, 0, 0, 0}. (For an extra challenge, you can try make your method work in place so that it doesnt create another array to use as temporary workspace. For an even harder challenge for the truly hardcore among you, make your method work in one pass through the array.)

TESTER CODE HAS TO COMPILE ALONG SIDE WITH THE ANS CODE FOR ABOVE

import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.*; public class TestArrayProblems { private static final int SEED = 12345; private static final int RUNS = 100; private static final int SIZE = 100; private ArrayProblems ap = new ArrayProblems(); 
 @Test public void testSqueezeLeft() { Random rng = new Random(SEED); ArrayList al1 = new ArrayList(10 * RUNS); ArrayList al2 = new ArrayList(10 * RUNS); for(int i = 0; i < RUNS; i++) { al1.clear(); al2.clear(); for(int j = 0; j < RUNS; j++) { int d = rng.nextInt(100000) - 50000; if(d == 0) { d = 1; } al1.add(d); int zeros = rng.nextInt(20); for(int k = 0; k < zeros; k++) { al2.add(0); } al2.add(d); } while(al1.size() < al2.size()) { al1.add(0); } int[] a1 = new int[al1.size()]; int[] a2 = new int[al2.size()]; int loc = 0; for(Integer e: al1) { a1[loc++] = e; } loc = 0; for(Integer e: al2) { a2[loc++] = e; } ap.squeezeLeft(a2); assertArrayEquals(a1, a2); } } 

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

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

Students also viewed these Databases questions

Question

1. Write down two or three of your greatest strengths.

Answered: 1 week ago