Related questions
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;
}
}
Step by stepSolved in 3 steps
- JAVAarrow_forwardJAVA programming I have to finish the "remove" method which is supposed to remove element e at index ind and returns it. 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...arrow_forwardWrite a program in JAVA to input the number of elements in an array, also input the values in the array from the console. Replace all those elements which are even and greater than 5, with -99. Print the final array. Constraints: The array should initially not contain -99. The value of the number of elements should not be greater than 50.arrow_forward
- write in java pleasearrow_forwardWrite a program named GradeStats.java then implement the following method: public static int[] gradeStats(int[] arr) It accepts int array representing test grades for CISY432 then calculates and returns the following grade statictics: highest, lowest, average, number of grades that are greater than or equal to 70, the number of grades that are less than 70 It returns these five numbers as an int array, and the 1st element is for highest, 2nd for lowest, 3rd for average 4th element for number of grades that are greater than or equal to 70 5th for the number of grades that are less than 70 For example. O Type here to search DELLarrow_forwardIn java i have some code for a method that finds the index of an integer that is passed in thet int is (target) the method also has the array called (array) and the first and last element of the array. also for this method to work the array has to be in descending order "static int find(int [] array, int first, int last, int target)" thats the method with all the variables that are passed to it my guess is that the first if statement just will have a short line that is "return -1;" because since the array has to be in descending order the first element cant be greater then the last element in the array but im confused on the rest because i cant find a way to make them one line codes with the conditions that i have to meet in the comments any help would be aprreaciatedarrow_forward
- This is needed in Java Given that an ArrayList of Strings named nameList has already been created and names have already been inserted, write the statement necessary to delete the name at index 15.arrow_forwardCan you please help me with this problem because I don't understand how to do this problem. Can you please help me with this problem. Can you please explain this step by step.arrow_forwardcan you plz write it in java.util.scanner formarrow_forward
- Please answer in python Write a method called add_racer which takes in a Boat object and adds it to the end of the racers list. The function does not return anything. Write a method called print_racers which loops through racers and prints the Boat objects. This function takes in no parameters (other than self) and returns nothing. Write a method called count that returns the number of racers. Write a method called race. The race function calls the move function for all of the racers in the BoatRace. Once all the racers have moved, call the print_racers method to display information about the progress of each boat. Then, check if any of the racer’s current_progress is greater than or equal to the race’s distance. If so, then return a list of all of the racers whose current_progress is greater than or equal to distance. If no racer has finished the race then repeat the calls to move and check until at least one racer has finished the race. Examples: Copy the following if...arrow_forwardJAVAarrow_forwardPlease write a java code using the photo below. The second photo is the output of my program that I am writingarrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education