JAVA programming I have to finish the "remove" method which is supposed to remove element e at index ind and returns it.  index here is the place where the numbers are placed. space 0,1,2,3. for example, if i run this program, i will get "-13,-3,-1" as the output. so the index (ind) is place 0 is -13, 1 is -3 and the 2nd place or index 2 is -1. so I have to remove a number from whatever index I want to and return it.  I want to remove an element (number) from the 2nd spot in the array, that's index (ind) 2, so whatever number is at index 2, i have to remove and return that number.  Ill paste the entire code below, but i only have to complete the "remove" method.   CODE: package com.mac286.arrays;   public class ourArray { //define an array of integers  private int[] Ar; //Ar is a reference  //declare  private int Capacity = 100; private int size = 0; private int Increment = 10;   //Default Constructor public ourArray() { Ar = new int[100]; Capacity = 100; size = 0; Increment = 10; }   //Constructor that accepts the capacity (C). It creates an array of C integersm, sets capacity to C //  Increment to 10 public ourArray(int C) { //create an array of C integers Ar = new int[C]; size = 0; Capacity = C; Increment = 10; }   //Constructor that accepts two integers (c, i) c for capacity and i for increment public ourArray(int C, int I) { //create an array of C integers Ar = new int[C]; size = 0; Capacity = C; Increment = I; }   //setter for Increment public void setIncrement(int I) { Increment = I; }   //getter for size, capacity and increment.  public int getSize() { return size; } public int getCapacity() { return Capacity; } public int getIncrement() { return Increment; }   //- A method void add(int e); it adds e to the end of the array.  // A.add(-1); A.add(-5), A.add(-9) [-1, -5, -9] public void add(int e) { //add only if there is space  if(size >= Capacity) { System.out.println("The array is full"); //get out  System.exit(1); } //if you are here it is not full Ar[size] = e; size++;   } //isEmpty returns true if it is empty and false it it is not.  public boolean isEmpty() { return (size == 0); } public String toString() { if(this.isEmpty()) return "[]"; String st = "["; //build your string [-1, -3, -9] for(int i = 0; i < size-1; i++) { //in the loop your are always dealing with element i //v element i is Ar[i] //append Ar[i], to the string st += Ar[i] + ", "; } //close the bracket  st += Ar[size-1] + "]"; return st; }   public void addFront(int e) { //check there is space  if (size >= this.Capacity) { System.out.println("Array is full"); System.exit(1); } //bring all values up by one  for(int i = size; i > 0; i--){ Ar[i] = Ar[i-1]; } //add e to index 0 Ar[0] = e; size++; } //remove removes the last item and returns it public int remove() { if(this.isEmpty()) { System.out.println("Error: the array is empty"); return -1; } int save = Ar[size-1]; //save the last element to be returned  //delete by just reduing the size  size--; //return the removed item return save;   } //removeFront() removes the first item and returns it //removeFront() removes the first item and returns it public int removeFront() { if(this.isEmpty()) { System.out.println("Error: the array is empty"); return -1; } int save = Ar[0]; //bring the rest down by one (size-1)( element down) for(int i = 0; i < size-1; i++) { Ar[i] = Ar[i+1];//if youy use size instead of size-1 you may get  //outofbounds exception } size--; return save; } //This method will add element e at index ind public void add(int ind, int e) { if (size >= this.Capacity) { System.out.println("Array is full"); System.exit(1); } if(ind == 0) this.addFront(e); else if (ind == size) this.add(e); else if(ind < 0 || ind > size) { System.out.println("Error: cannot add at those indices"); return; } //bring all values up by one  for(int i = size; i > ind; i--){ Ar[i] = Ar[i-1]; } //add e to index 0 Ar[ind] = e; size++;   } //int remove(intind) removes element e at index ind and returns it.  public int remove(int ind) { return 0;    }   }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question

JAVA programming

I have to finish the "remove" method which is supposed to remove element e at index ind and returns it. 

index here is the place where the numbers are placed. space 0,1,2,3. for example, if i run this program, i will get "-13,-3,-1" as the output. so the index (ind) is place 0 is -13, 1 is -3 and the 2nd place or index 2 is -1. so I have to remove a number from whatever index I want to and return it.  I want to remove an element (number) from the 2nd spot in the array, that's index (ind) 2, so whatever number is at index 2, i have to remove and return that number. 

Ill paste the entire code below, but i only have to complete the "remove" method.

 

CODE:

package com.mac286.arrays;

 

public class ourArray {

//define an array of integers 

private int[] Ar; //Ar is a reference 

//declare 

private int Capacity = 100;

private int size = 0;

private int Increment = 10;

 

//Default Constructor

public ourArray() {

Ar = new int[100];

Capacity = 100;

size = 0;

Increment = 10;

}

 

//Constructor that accepts the capacity (C). It creates an array of C integersm, sets capacity to C

//  Increment to 10

public ourArray(int C) {

//create an array of C integers

Ar = new int[C];

size = 0;

Capacity = C;

Increment = 10;

}

 

//Constructor that accepts two integers (c, i) c for capacity and i for increment

public ourArray(int C, int I) {

//create an array of C integers

Ar = new int[C];

size = 0;

Capacity = C;

Increment = I;

}

 

//setter for Increment

public void setIncrement(int I) {

Increment = I;

}

 

//getter for size, capacity and increment. 

public int getSize() {

return size;

}

public int getCapacity() {

return Capacity;

}

public int getIncrement() {

return Increment;

}

 

//- A method void add(int e); it adds e to the end of the array. 

// A.add(-1); A.add(-5), A.add(-9) [-1, -5, -9]

public void add(int e) {

//add only if there is space 

if(size >= Capacity) {

System.out.println("The array is full");

//get out 

System.exit(1);

}

//if you are here it is not full

Ar[size] = e;

size++;

 

}

//isEmpty returns true if it is empty and false it it is not. 

public boolean isEmpty() {

return (size == 0);

}

public String toString() {

if(this.isEmpty())

return "[]";

String st = "[";

//build your string [-1, -3, -9]

for(int i = 0; i < size-1; i++) {

//in the loop your are always dealing with element i

//v element i is Ar[i]

//append Ar[i], to the string

st += Ar[i] + ", ";

}

//close the bracket 

st += Ar[size-1] + "]";

return st;

}

 

public void addFront(int e) {

//check there is space 

if (size >= this.Capacity) {

System.out.println("Array is full");

System.exit(1);

}

//bring all values up by one 

for(int i = size; i > 0; i--){

Ar[i] = Ar[i-1];

}

//add e to index 0

Ar[0] = e;

size++;

}

//remove removes the last item and returns it

public int remove() {

if(this.isEmpty()) {

System.out.println("Error: the array is empty");

return -1;

}

int save = Ar[size-1]; //save the last element to be returned 

//delete by just reduing the size 

size--;

//return the removed item

return save;

 

}

//removeFront() removes the first item and returns it

//removeFront() removes the first item and returns it

public int removeFront() {

if(this.isEmpty()) {

System.out.println("Error: the array is empty");

return -1;

}

int save = Ar[0];

//bring the rest down by one (size-1)( element down)

for(int i = 0; i < size-1; i++) {

Ar[i] = Ar[i+1];//if youy use size instead of size-1 you may get 

//outofbounds exception

}

size--;

return save;

}

//This method will add element e at index ind

public void add(int ind, int e) {

if (size >= this.Capacity) {

System.out.println("Array is full");

System.exit(1);

}

if(ind == 0)

this.addFront(e);

else if (ind == size)

this.add(e);

else if(ind < 0 || ind > size) {

System.out.println("Error: cannot add at those indices");

return;

}

//bring all values up by one 

for(int i = size; i > ind; i--){

Ar[i] = Ar[i-1];

}

//add e to index 0

Ar[ind] = e;

size++;

 

}

//int remove(intind) removes element e at index ind and returns it. 

public int remove(int ind) {

return 0; 

 

}

 

}

//int remove(int ind) removes element e at index ind and returns it.
public int remove(int ind) {
return 0;
}
Transcribed Image Text://int remove(int ind) removes element e at index ind and returns it. public int remove(int ind) { return 0; }
package com.mac286.arrays;
public class Tester {
public static void main(String [] args) {
//create an object ourArray
//className objectName = new className();
ourArray A = new ourArray();
+ A.getSize() +
+ A.getIncrement ());
System.out.println("size:
//display while empty
System.out.println("size:
//add -3, -1, -9
A.add(-3);
A.add (-1);
A.add(-9);
Increment:
+ A.getSize() +
+ A.toString(0);
%3D
:
//show the content
System.out.println("size:
A.addFront (-13);
System.out.println("size:
A.addFront(-25);
System.out.println("size:
System.out.println("removing:
System.out.println("size:
System.out.println("removing:
System.out.println("size:
+ A.getSize() + "
" + A.toString());
:
+ A.getSize() +
+ A.tostring(0);
:
+ A.getSize() +
+ A. remove();
+ A.getSize(0 +
+ A. removeFront());
+ A.getSize() + " :
+ A.toString(0);
:
+ A. toString 0);
+ A.toString(0);
%3D
}
}
Transcribed Image Text:package com.mac286.arrays; public class Tester { public static void main(String [] args) { //create an object ourArray //className objectName = new className(); ourArray A = new ourArray(); + A.getSize() + + A.getIncrement ()); System.out.println("size: //display while empty System.out.println("size: //add -3, -1, -9 A.add(-3); A.add (-1); A.add(-9); Increment: + A.getSize() + + A.toString(0); %3D : //show the content System.out.println("size: A.addFront (-13); System.out.println("size: A.addFront(-25); System.out.println("size: System.out.println("removing: System.out.println("size: System.out.println("removing: System.out.println("size: + A.getSize() + " " + A.toString()); : + A.getSize() + + A.tostring(0); : + A.getSize() + + A. remove(); + A.getSize(0 + + A. removeFront()); + A.getSize() + " : + A.toString(0); : + A. toString 0); + A.toString(0); %3D } }
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Arrays
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT