Question: package com.mac286.arrays; /* //HW Write a program that does the following. You have an array of integers //with positive and negative values. //using one ourArray
package com.mac286.arrays;
/*
//HW Write a program that does the following. You have an array of integers //with positive and negative values. //using one ourArray object, 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. - A reference to an array of integers - A variable size that will keep track of the number of elements in the array.
- Default constructor that creates an array of 100 integers. - A constructor that accepts the capacity of the array and creates an array of that capacity. Size should be set to 0 - Method size that returns the size of the array - method isEmpty that returns true if the array is empty and false if not. - method toString that returns the array as a string in the form
- A method void addBack(int a) that adds a to the back of the array (index size). - A method int removeFront() that removes the first element of the array (index 0) and returns it - A method void addFront(int a) that would add a to the front of the array - A method int removeBack() that removes the last element and returns it - A method void add(int a, int ind) that would add a to the index ind - A method int remove(int ind) that woudl remove the element at ind and returns it.
// [-3, 2, -4, 5, -6, -8,-9, 7, 13] The output should be // [-3, -4, -6, -8, -9, 2, 5, 7, 13]
*/
public class ourArray { private T[] Ar; private int size = 0; private int increment = 100; //default constructor public ourArray () { Ar = (T[]) new Object[100]; size = 0; } //constructor that accepts the size public ourArray (int n) { Ar = (T[]) new Object[n]; size = 0; } //constructor that specifies the initial capacity and the increment public ourArray (int n, int incr) { Ar = (T[]) new Object[n]; size = 0; increment = incr; } public int getSize() { 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; } //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 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 Ar[i] = Ar[i+1]; } size--; return temp; } }
package com.mac286.arrays;
public class Tester {
public static void main(String[] args) { //create an object ourArray ourArray A = new ourArray(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 S = new ourArray(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 Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
