* 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
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
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"
Step by step
Solved in 4 steps with 4 images