Java programming help please.  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 1 "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]

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java programming help please.  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 1 "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] 

MAIN class:

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

 

 

 

 

 

}

}

this.add(a, 0);
}
public T removeBack(O{
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+l]=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+l];
}
size--;
return temp;
}
@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);
}
}
Transcribed Image Text:this.add(a, 0); } public T removeBack(O{ 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+l]=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+l]; } size--; return temp; } @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); } }
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(int ind)
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);
}
Transcribed Image Text: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(int ind) 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); }
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education