programming help please. Main and other class of code is below. Thank you so so much. i have to Write a program that does the following: You have an array of integers with positive and negative values. using one ourArray object (will post code of "ourArrary" below) , and in two loops maximum, How can you reorganize the content of the array so that all negative numbers are on one side and the positive numbers are on the other side. [-3, 2, -4, 5, -6, -8,-9, 7, 13] The output should be [-3, -4, -6, -8, -9, 2, 5, 7, 13] Our array code: public class ourArray implements iQueue{ private T[] Ar;
Java programming help please. Main and other class of code is below. Thank you so so much.
i have to Write a program that does the following:
You have an array of integers with positive and negative values. using one ourArray object (will post code of "ourArrary" below) , and in two loops maximum, How can you reorganize the content of the array so that all negative numbers are on one side and the positive numbers are on the other side.
[-3, 2, -4, 5, -6, -8,-9, 7, 13] The output should be [-3, -4, -6, -8, -9, 2, 5, 7, 13]
Our array code:
public class ourArray<T> implements iQueue{
private T[] Ar;
private int size = 0;
private int increment = 100;
//default constructor
@SuppressWarnings("unchecked")
public ourArray () {
Ar = (T[]) new Object[100];
size = 0;
}
//constructor that accepts the size
@SuppressWarnings("unchecked")
public ourArray (int n) {
Ar = (T[]) new Object[n];
size = 0;
}
//constructor that specifies the initial capacity and the increment
@SuppressWarnings("unchecked")
public ourArray (int n, int incr) {
Ar = (T[]) new Object[n];
size = 0;
increment = incr;
}
public int getSize() {
return size;
}
public int size() {
return size;
}
public int getCapacity() {
return Ar.length;
}
//isEmpty
public boolean isEmpty() {
return (size == 0);
}
public String toString() {
if(this.isEmpty())
return "[]";
String st = "[";
for(int i = 0; i < size - 1; i++) {
st += Ar[i].toString() + ", ";
}
//the last element index size -1 needs to be dealt with differently
st += Ar[size -1].toString() + "]";
return st;
}
//T elementAt(intind)
public T elementAt(int ind) {
if(ind < 0 || ind > size -1) {
return null;
}
return Ar[ind];
}
//addBasck adds a to the back of the array
public void addBack(T a) {
this.add(a, this.getSize());
}
public void addFront(T a) {
this.add(a, 0);
}
public T removeBack( ) {
return this.remove(size-1);
}
public T removeFront() {
return this.remove(0);
}
private void resize() {
//create a new array with capacity of the old plus increment
@SuppressWarnings("unchecked")
T[] temp = (T[])new Object[Ar.length + increment];
//copy all elements of the old array into the new one
for(int i =0 ; i < size; i++) {
temp[i] = Ar[i];
}
//assign new array to old array.
Ar = temp;
}
public void add(T a, int ind) {
if(size == Ar.length) {
resize();
}
//is the index a valid one
if(ind < 0 || ind > size) {
System.out.println("Invalid index");
return;
}
for(int i= size-1; i>=ind; i--) {
Ar[i+1]=Ar[i];
}
Ar[ind]=a;
size++;
}
public T remove(int ind){
if(this.isEmpty()) {
System.out.println("The array is emtpy");
return null;
}
if(ind < 0 || ind > size-1) {
System.out.println("Invalid index");
return null;
}
//remember the element at ind
T temp = Ar[ind];
//bring all elements down by one until ind
for(int i =ind; i <size-1; i++) {
Ar[i] = Ar[i+1];
}
size--;
returntemp;
}
@Override
public void add(Object e) {
this.addBack((T)e);
}
@Override
public T remove() {
// TODO Auto-generated method stub
return this.removeFront();
}
@Override
public T peek() {
return this.elementAt(0);
}
}
MAIN CODE:
public class Tester {
public static void main(String[] args) {
//create an object ourArray
ourArray<Integer> A = new ourArray<Integer>(5);
//add elements to the back
A.addBack(-3);
A.addBack(-7);
A.addBack(-9);
System.out.println("A: " + A.toString());
A.addFront(-11);
A.addFront(-13);
System.out.println("A: " + A.toString());
System.out.println("removing the back: " + A.removeBack());
System.out.println("A: " + A.toString());
System.out.println("removing the front: " + A.removeFront());
System.out.println("A: " + A.toString());
A.add(-15, 2);
System.out.println("size: " + A.getSize() + " capacity: " + A.getCapacity() + " A: " + A.toString());
A.add(-18, 4);
System.out.println("size: " + A.getSize() + " capacity: " + A.getCapacity() + " A: " + A.toString());
A.add(-21, 1);
System.out.println("size: " + A.getSize() + " capacity: " + A.getCapacity() + " A: " + A.toString());
ourArray<String> S = new ourArray<String>(3, 5);
S.addFront("Hi");
S.add("There", 1);
S.addBack("you");
System.out.println("size: " + S.getSize() + " capacity: " + S.getCapacity() + " S: " + S.toString());
S.add("?", 3);
System.out.println("size: " + S.getSize() + " capacity: " + S.getCapacity() + " S: " + S.toString());
}
}

Step by step
Solved in 2 steps with 1 images









