Question

In the attached FlexArray Java class, implement a method public int delete (int location) { }...

In the attached FlexArray Java class, implement a method

public int delete (int location) {

}

that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n.

*****************************************************************************************************************************

public class FlexArray
{
    int [] array;
    private int size;
    private int capacity;
    public FlexArray()
    {
       capacity=10;
       size=0;
       array=new int[10];
    }
    public FlexArray(int maxEntries) {
       capacity=maxEntries;
       size=0;
       array=new int[capacity];
    }
    public void add(int val) {
        if (size<capacity) {
        array[size]=val;
        size++;
    }
    else System.out.println("Array is full, cannot add any more values");
    }

// add your code
public int delete (int location) {




}
        public int set(int val, int location) {
        if (location>=0 && location<size){
        int num= get(location);
        array[location]=val;
        return num;
    }
    else throw new ArrayIndexOutOfBoundsException();
        
    }
        public int get(int location) {
        if (location>=0 && location<size){
        int num= array[location];
        return num;
    }
    else throw new ArrayIndexOutOfBoundsException();        
    }
}

Homework Answers

Answer #1
public int delete (int location) {
    if(size == 0){
        return -1; //returns -1 as the array is empty
    }
    else if(location >= size){
        return -1; //returns -1 as the location is invalid with respect to size of array
    }
    else{
        int value = array[location];
        for(int i=location+1;i<size;i++){
            array[i-1] = array[i];
        }
        size--;
        return value; //returns value at location in array
    }
}

Time Complexity is O(n).

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
Here is a basic Counter class. class Counter { private int num = 0; // Not...
Here is a basic Counter class. class Counter { private int num = 0; // Not needed for this question public void increment() { num++; } public boolean equals(Counter other) { return this.num == other.num; } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Counter[] counters, int numCounters, Counter counter) The goal of the method is to...
Code a valid copy constructor for the GrabBag class below: class GrabBag { private: int* values;...
Code a valid copy constructor for the GrabBag class below: class GrabBag { private: int* values; int size; public: GrabBag(); }; #include "GrabBag.h" GrabBag::GrabBag() { size = rand() % 100 + 1;   values = new int[size]; //random array size   for (int i = 0; i < size; i++)   values[i] = rand()% 10 + 1 ;// random array contents   }   
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....