Question: JAVA Problem: ReOrder.java ******Starter Code: including what I've tried so far***** public class ReOrder { /* * TODO : You are asked to complete the

JAVA Problem: ReOrder.java

JAVA Problem: ReOrder.java ******Starter Code: including what I've tried so far***** public

class ReOrder { /* * TODO: You are asked to complete the

method reorder. This method takes in as input an array of *

******Starter Code: including what I've tried so far*****

public class ReOrder {

/*

* TODO: You are asked to complete the method reorder. This method takes in as input an array of

* ints and returns back nothing (void). This method must call the method swap. You cannot change

* the function signature of this method.

*/

public static void reorder(int[] items) {

}

/*

* TODO: You are asked to complete the method swap. This method takes in as input two ints and an

* array of ints. The int i and int j are the index i and index j in the array items. You are

* asked to swap the value at the index i in items with the value at the index j. You cannot

* change the function signature of this method.

*/

private static void swap(int i,int j,int[] items)

{

i = 0;

j = items.length / 2;

int k = items.length - 1;

while (j

{

if (items[j] == 0)

j++;

if (items[j] > 0)

{

int temp = items [j];

items[j] = items[k-1];

items[k-1] = temp;

}

k--;

if (items[j]

{

int temp = items[j];

items[j] = items[i];

items[i] = temp;

}

j++;

i++;

}

}

public static void main(String[] args) {

/*

* You can modify the main function in any way you like. We will not mark your main function.

*/

int[] items = {7, -3, 0, 0, 8, -2};

/*

* printing the values in the items before calling the method reorder.

*/

for (int item : items) {

System.out.println(item);

}

// calling the reorder method

ReOrder.reorder(items);

/*

* printing the values in the items after calling the method reorder

*/

for (int item : items) {

System.out.println(item);

}

}

}

In this problem, we provide you with a Java file called ReOrder.java. Suppose that there is an array and let us call this array, items. The array items contains n integers. These n integers are in a complete random order in the array. What you are asked to do is to reorder the items in the array such that all the negative integers come before all the zeros and all the positive integers appear at the end. Note, this question is not asking you to sort the numbers in the array. It is asking you to reorder them. Here are some examples, to help you understand the problem and get started 2.1 Example 1 Figure 1: In this first example, you can see that the array initially contains the integers 7,-3,0, 0,8 and -2. Now this is in some random order. What you must do to this array, is to reorder the numbers such that the array has three distinct pieces (also called constraints) represented by o. You can see that in the first region, we place all the numbers that are less than 0, followed by all zeros and then followed by any numbers that are greater than 0. The numbers within each region can be in any order, as long as they happen to fulfill the constraint In this problem, we provide you with a Java file called ReOrder.java. Suppose that there is an array and let us call this array, items. The array items contains n integers. These n integers are in a complete random order in the array. What you are asked to do is to reorder the items in the array such that all the negative integers come before all the zeros and all the positive integers appear at the end. Note, this question is not asking you to sort the numbers in the array. It is asking you to reorder them. Here are some examples, to help you understand the problem and get started 2.1 Example 1 Figure 1: In this first example, you can see that the array initially contains the integers 7,-3,0, 0,8 and -2. Now this is in some random order. What you must do to this array, is to reorder the numbers such that the array has three distinct pieces (also called constraints) represented by o. You can see that in the first region, we place all the numbers that are less than 0, followed by all zeros and then followed by any numbers that are greater than 0. The numbers within each region can be in any order, as long as they happen to fulfill the constraint

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!