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

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 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...
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.
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Create global variables Mean, Mode, Median, then create a method that takes an array of 10...
Create global variables Mean, Mode, Median, then create a method that takes an array of 10 double numbers and return three answers of the Mean, Median, Mode ( no need for implementation of the mean, median and mode, calculate them manually and return the answers), assign the answers to the global variables. In java, please!!
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
1) Question with methods use scanner: 1) Create one method that will add four numbers (return...
1) Question with methods use scanner: 1) Create one method that will add four numbers (return method or regular public static void ) 2) Create another method that will subtract four numbers (return method or regular public static void ) 3) Create another method that will multiplay four numbers (return method or regular public static void ) 4) Create another method that will divide four numbers (return method or regular public static void ) 5) Create another method that will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT