Question

In: Computer Science

Create a method evenOdd() that sorts an array into even numbers on one side and the...

Create a method evenOdd() that sorts an array into even numbers on one side and the odd numbers on the other side

public class ourArray {
int[] Ar;
int size;

public int getSize() {
   return size;
}
public ourArray() {
   Ar = new int[100];
   size = 0;
}
public void add(int e) {
   Ar[size]= e;
   size++;
}
public int remove() {
   //save the last item
          int save = Ar[size -1];
          //decrease the size
          size--;
          //return the saved item
          return save;
}
/*
   * Design a method int indexSmallest() that returns the index of the smallest
   * element in the array.
   */
public int indexSmallest(int start) {
   int ind = start;
   for(int i = 1; i < size; i++) {
       if(Ar[ind] > Ar[i]) {
           ind = i;
       }
   }
   return ind;
}
/*
   * Design a method search that looks for a specific integer
   * and returns its index. The method should return -1 if the number
   * is not found
   */
   public int search(int n){
       for (int i = 0; i < size; i++){
           if(Ar[i] == n)
               return i;
       }
       return -1;
   }
   /*
   * Design method mySort() that will sort the array
   */
   public void mySort() {
       //loop through the array size -2
       for(int i = 0; i < size - 1; i++) {
           //find the index of the smallest starting at i
   int ind = indexSmallest(i);
   //swap element at ind with element at i
           int temp = Ar[i];
           Ar[i] = Ar[ind];
           Ar[ind]= temp;
       }
   }
   /*
   * Design a method flip() that will flip the content of the array
   * ex. your array is: -2 -3 4 -5 will be -5 4 -3 -2 after invoking flip
   */
   public void flip() {
       int i = 0 , j = size-1;
       while(i < j) {
           int temp = Ar[i];
           Ar[i] = Ar[j];
           Ar[j]= temp;
           i++;
           j--;
       }
   }
   /*
   * Design a method that would put all
   */
   public void evenOdd() {
       for(int i = 0; i < size - 1; i++) {
             
       }
   }
public String toString() {
   String st = "[";
   for (int i=0; i<size-1; i++) {
       st += Ar[i] + " , ";
   }
   st += Ar[size-1] + "]";
   return st;
}
}

Solutions

Expert Solution

code:

import java.util.*;
public class ourArray {
int[] Ar;
int size;

public int getSize() {
return size;
}
public ourArray() {
Ar = new int[100];
size = 0;
}
public void add(int e) {
Ar[size]= e;
size++;
}
public int remove() {
//save the last item
int save = Ar[size -1];
//decrease the size
size--;
//return the saved item
return save;
}
/*
* Design a method int indexSmallest() that returns the index of the smallest
* element in the array.
*/
public int indexSmallest(int start) {
int ind = start;
for(int i = 1; i < size; i++) {
if(Ar[ind] > Ar[i]) {
ind = i;
}
}
return ind;
}
/*
* Design a method search that looks for a specific integer
* and returns its index. The method should return -1 if the number
* is not found
*/
public int search(int n){
for (int i = 0; i < size; i++){
if(Ar[i] == n)
return i;
}
return -1;
}
/*
* Design method mySort() that will sort the array
*/
public void mySort() {
//loop through the array size -2
for(int i = 0; i < size - 1; i++) {
//find the index of the smallest starting at i
int ind = indexSmallest(i);
//swap element at ind with element at i
int temp = Ar[i];
Ar[i] = Ar[ind];
Ar[ind]= temp;
}
}
/*
* Design a method flip() that will flip the content of the array
* ex. your array is: -2 -3 4 -5 will be -5 4 -3 -2 after invoking flip
*/
public void flip() {
int i = 0 , j = size-1;
while(i < j) {
int temp = Ar[i];
Ar[i] = Ar[j];
Ar[j]= temp;
i++;
j--;
}
}
/*
* Design a method that would put all
*/
public void evenOdd() {
int j = -1;
for(int i = 0; i < size - 1; i++) {
if (Ar[i]%2==0)
{
j++;
int temp = Ar[i];
Ar[i] = Ar[j];
Ar[j] = temp;
}   
}
}
public String toString() {
String st = "[";
for (int i=0; i<size-1; i++) {
st += Ar[i] + " , ";
}
st += Ar[size-1] + "]";
return st;
}
/*
comment this main method if needed as just given to verify the functioning
*/
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of elements in the array:");
int n=sc.nextInt();
ourArray o=new ourArray();
System.out.println("Enter the array elements:");
for(int i=0;i<n;i++)
{
o.add(sc.nextInt());
}
System.out.println("Before calling evenOdd");
System.out.println(o);
o.evenOdd();
System.out.println("Before calling evenOdd");
System.out.println(o);
}
}
Sample i/o:

Explanation:

The method evenOdd separates or sorts the array element in such a way that first half contains even elements and the next half contains odd elements. Sample i/o is presented for you understanding.
**please upvote if you like the answer. Thank you


Related Solutions

Draw a flowchart that asks the user to enter an array of random numbers, then sorts...
Draw a flowchart that asks the user to enter an array of random numbers, then sorts the numbers (ascending order), then prints the new array, after that asks the user for two new numbers and adds them to the same array and keeps the array organization.
7/Draw a flowchart that asks the user to enter an array of random numbers, then sorts...
7/Draw a flowchart that asks the user to enter an array of random numbers, then sorts the numbers (descending order), after that reverses it (the first element will be the last...).
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
WITH using methods create an array with numbers
IN JAVA  Prompt 3:WITH using methods create an array with numbersint [] number = { 35, 68, 80, 34, 45, 79, 80};Display the numbers in the array using for loopDisplay the sumFind biggest element in the arrayDisplay the numers in the array with index numbersDisplay the numers using for loop in ascending order (sort)
Write a complete program called EvenNums that uses a method to create arrays of even numbers...
Write a complete program called EvenNums that uses a method to create arrays of even numbers starting 1 of the given length: public static int[] getEvenNums(int length) {} EX: int[] nums = getEvenNumbers(6); System.out.println(Arrays.toString(nums)); expected output: [2, 4, 6, 8, 10, 12]
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
9) Create a java programming where you will enter two numbers and create only one method,...
9) Create a java programming where you will enter two numbers and create only one method, which will return or display addition, substraction, multiplication, division, average and check which number is greater than the other. Everything in one method and call it in main method.
Write a function that removes all even numbers from an array. The function should take the...
Write a function that removes all even numbers from an array. The function should take the array, length of the array, and a pointer for number of odd numbers found as arguments and return a pointer to the new array. If there are no odd numbers found in the array, your code should print "No odds found." and return NULL. Use the function header: int *removeEvens(int *a, int length, int *oddsFound); Example: Input array a[ ] = {3, 4, 5,...
In MATLAB Write a function to create an array of N numbers with a normal distribution....
In MATLAB Write a function to create an array of N numbers with a normal distribution. Call that from a script to create 1000 numbers, with a mean of 50 and a sigma of 10.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT