Question: Objectives - Design a data structure that behaves like a dynamically allocated vector - Implement a list interface - Extend an abstract class - Convert
Objectives
- Design a data structure that behaves like a dynamically allocated vector
- Implement a list interface
- Extend an abstract class
- Convert a generic Object type class to a parameterized data type
Your MyVector class where you will implement MyList begins like this:
public class MyVector extends MyAbstractList implements MyList
After the functionality is complete and the bugs are worked out, the next step is to make MyVector a generic typed class by adding parameterized data type. Of course, MyAbstractList and MyList will also need the conversion applied to them. For grading, the class header will be:
public class MyVector extends MyAbstractList implements MyList
An example was given of converting Couple.java to a parameterized data type class. In general, the steps are:
Add syntax to the class, and possibly some method headers (your IDE will help you know)
Change Object to E in definitions of parameters, return types, arrays, and variables
In addition to implementing MyList interface, provide three constructors:
one with no parameters that initializes the vector to a capacity of 10 elements,
one getting the initial capacity of the vector from the parameter, and
another that take the initial capacity of the vector and the capacity increment.
Last, add three more methods:
getId() which returns a String "Program 6, FullNameHere", and
getCapacity() which returns the capacity (not the size) of the internal array structure
getIncrement() which returns the capacity increment of the vector.
Develop the code incrementally. That is, do one method at a time, testing as you go. A test driver is provided. You will need to modify it or write a new driver for MyVector, but it will be fairly similar.
MyList.java
public interface MyList {
/** THE STUDENT MUST WRITE THE METHOD HEADERS. THE FIRST ONE IS DONE FOR YOU **/
/**
* Appends the specified element to the end of this list
* @param data
* @return boolean
*/
public boolean add(E data);
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any subsequent
* elements by adding one to their indices.
* @param index - index at which the specified element is to be inserted
* @param data - element to be inserted
* @return boolean
* @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
*/
add
/**
* Removes all of the elements from this list
*/
clear
/**
* Returns true if this list contains the specified element
* @param data
* @return boolean
*/
contains
/**
* Returns the element at the specified position in this list
* @param index
* @return E
*/
get
/**
* Returns the index of the first occurrence of the specified element in this list
* Return, or -1 if this list does not
* contain the element
* @param data
* @return int
*/
indexOf
/**
* Returns the index of the last matching of the element in this list
* Return -1 if no match
* @param data
* @return int
*/
lastIndexOf
/**
* Returns true if this list contains no elements
* @return boolean
*/
isEmpty
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements by subtracting one from their indices.
* @param index - index of the element to be removed
* @return E - the element that was removed from the list
* IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
*/
remove
/**
* Trims the capacity of this MyVector instance to be the list's current size. An application
* can use this operation to minimize the storage of a MyVector instance.
*/
trimToSize
/**
* Returns the number of elements in this list
* @return int
*/
size
}
MyAbstractList.java
public abstract class MyAbstractList implements MyList {
protected int size;
public MyAbstractList() {
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public int size() {
return this.size;
}
}
MyVector.java will be the driver class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
