* Design a class ourVector that mimics the javaVector. The class has the following   * properties:  - A private variable size that keeps track of how many elements in the array  - A reference to an array of integers   - An increment by how much to increase wjen it's full   - A default constructor that creates an array of 5 with increment 10;  - A constructor that accepts the initial capacity and creates an array of that capacity   and an increment of twice the capacity   - A constructor that accepts two integers, the first for the capacity and the second for   increment.   - Capacity, the length of the array  - int size(); A method that returns the size of the array   - boolean isEmpty(); returns true if it's empty and false if not.  - void addBack(int e); adds e to the back of the array  - void addFront(int e); adds e to the front of the array.  - Override String toString(); to return the content of the array in the form    [-1, -4, -6]  - int elementAt(int ind); returns the elements at index ind, if valid.   - int removeBack(); removes the element in the back of the array and returns it (update size)  - int removeFront(); removes the element in the front of the array and returns it (update size)  - void add(int ind, int e); adds e to the index ind.   - int remove(int ind); removes element at index ind and returns it.  */ public class ourVector { //private variables private int size; //Let's use .length fro capacity no need for another variable private int increment; private int[] V; //V is a reference  //constructors  public ourVector() { V = new int[5]; size = 0; increment = 10; } public ourVector(int capacity) { V = new int[capacity]; size = 0; increment = 2*capacity; } public ourVector(int capacity, int incr) { V = new int[capacity]; size = 0; increment = incr; } //methods  public int size() { return size; } public boolean isEmpty() { return (size == 0); //if size is 0 return true else return false } //WH TODO: Finish the resize method and test it private void resize() { //create a temporary array temp with capacity V.length+increment //copy all elements from V to temp //assign temp to V.  return; }   public int capacity() { return V.length; }   public void addBack(int e) { //test first if it's not full if(size == V.length) { System.out.println("The vector is full resizing"); this.resize(); } //insert e at index size  V[size] = e; size++; } public void add(int e) { this.addBack(e); }   //HW TODO: fix toString so that the last comma does not appear   //toString to return the array as a string in the form [-1, -5, -7] public String toString() { if(this.isEmpty()) { return "[]"; } String st = "["; for(int i =0; i

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
* Design a class ourVector that mimics the javaVector. The class has the following 
 * properties:
 - A private variable size that keeps track of how many elements in the array
 - A reference to an array of integers 
 - An increment by how much to increase wjen it's full 
 - A default constructor that creates an array of 5 with increment 10;
 - A constructor that accepts the initial capacity and creates an array of that capacity 
 and an increment of twice the capacity 
 - A constructor that accepts two integers, the first for the capacity and the second for 
 increment. 
 - Capacity, the length of the array
 - int size(); A method that returns the size of the array 
 - boolean isEmpty(); returns true if it's empty and false if not.
 - void addBack(int e); adds e to the back of the array
 - void addFront(int e); adds e to the front of the array.
 - Override String toString(); to return the content of the array in the form 
  [-1, -4, -6]
 - int elementAt(int ind); returns the elements at index ind, if valid. 
 - int removeBack(); removes the element in the back of the array and returns it (update size)
 - int removeFront(); removes the element in the front of the array and returns it (update size)
 - void add(int ind, int e); adds e to the index ind. 
 - int remove(int ind); removes element at index ind and returns it.
 */
public class ourVector {
//private variables
private int size;
//Let's use .length fro capacity no need for another variable
private int increment;
private int[] V; //V is a reference 
//constructors 
public ourVector() {
V = new int[5];
size = 0;
increment = 10;
}
public ourVector(int capacity) {
V = new int[capacity];
size = 0;
increment = 2*capacity;
}
public ourVector(int capacity, int incr) {
V = new int[capacity];
size = 0;
increment = incr;
}
//methods 
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0); //if size is 0 return true else return false
}
//WH TODO: Finish the resize method and test it
private void resize() {
//create a temporary array temp with capacity V.length+increment
//copy all elements from V to temp
//assign temp to V. 
return;
}
 
public int capacity() {
return V.length;
}
 
public void addBack(int e) {
//test first if it's not full
if(size == V.length) {
System.out.println("The vector is full resizing");
this.resize();
}
//insert e at index size 
V[size] = e;
size++;
}
public void add(int e) {
this.addBack(e);
}
 
//HW TODO: fix toString so that the last comma does not appear
 
//toString to return the array as a string in the form [-1, -5, -7]
public String toString() {
if(this.isEmpty()) {
return "[]";
}
String st = "[";
for(int i =0; i<size; i++ ) {
st += V[i] + ", ";
}
//close it with a closing bracket 
st += "]";
return st;
}
 
}
Expert Solution
Step 1: Algorithm :

Class: ourVector

Properties:
- private int size
- private int increment
- private int[] V

Constructor 1:
- ourVector()
  - Initialize V with an array of size 5
  - Initialize size to 0
  - Initialize increment to 10

Constructor 2:
- ourVector(int capacity)
  - Initialize V with an array of size capacity
  - Initialize size to 0
  - Initialize increment to 2 times capacity

Constructor 3:
- ourVector(int capacity, int incr)
  - Initialize V with an array of size capacity
  - Initialize size to 0
  - Initialize increment to incr

Methods:
- int size()
  - Return the current size of the vector

- boolean isEmpty()
  - Return true if the vector is empty (size == 0), otherwise false

- void resize()
  - Create a new temporary array "temp" with a capacity of V.length + increment
  - Copy all elements from V to temp
  - Assign temp to V

- int capacity()
  - Return the current capacity of the vector (length of V)

- void addBack(int e)
  - Check if the vector is full (size == V.length)
    - If full, call resize() to increase the capacity
  - Add element e to the end of the vector
  - Increment size

- void addFront(int e)
  - Check if the vector is full (size == V.length)
    - If full, call resize() to increase the capacity
  - Shift all elements to the right to make space for the new element
  - Add element e to the front of the vector
  - Increment size

- int elementAt(int ind)
  - Check if the given index "ind" is valid (0 <= ind < size)
  - Return the element at index "ind"

- int removeBack()
  - Check if the vector is empty (size == 0)
    - If empty, throw an exception (IllegalStateException)
  - Remove and return the element at the back of the vector
  - Decrement size

- int removeFront()
  - Check if the vector is empty (size == 0)
    - If empty, throw an exception (IllegalStateException)
  - Remove and return the element at the front of the vector
  - Decrement size

- void add(int ind, int e)
  - Check if the given index "ind" is valid (0 <= ind <= size)
  - Check if the vector is full (size == V.length)
    - If full, call resize() to increase the capacity
  - Shift elements to the right to make space for the new element at index "ind"
  - Insert element e at index "ind"
  - Increment size

- int remove(int ind)
  - Check if the given index "ind" is valid (0 <= ind < size)
  - Remove and return the element at index "ind"
  - Shift elements to the left to fill the gap
  - Decrement size

- String toString()
  - If the vector is empty, return "[]"
  - Create a StringBuilder "sb"
  - Append "[" to "sb"
  - Iterate over the elements in the vector and append them to "sb" separated by commas
  - Append "]" to "sb"
  - Return the string representation of "sb"

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Class
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