In: Computer Science
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;
}
}
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