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;

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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());

 

 

 

 

}

}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY