Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives: -Design a data structure that behaves like a dynamically allocated array -Implement a list interface -Extend an abstract class -Convert a generic Object type

Objectives:

-Design a data structure that behaves like a dynamically allocated array

-Implement a list interface

-Extend an abstract class

-Convert a generic Object type class to a parameterized data type

Instructions for MyArrayList:

Hopefully you were able to code along with the video to implement your own ArrayList. We will begin with that code and complete the class. Most of the methods were written in the video. The code is also available on Blackboard/Week 4 in MyArrayList.zip.

Your MyArrayList class where you will implement MyList begins like this:

public class MyArrayList extends MyAbstractList implements MyList 

After the functionality is complete and the bugs are worked out, the next step is to make MyArrayList 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 MyArrayList 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

Take care when creating new arrays. Though the code below has replaced Object with E, we cannot create an array this way:

E[] mylist = new E[10]; // cannot do this 

Instead, we must create an array of Object references and then cast the array to E:

E[] mylist = (E[]) new Object[10]; // can do this 

But now the compiler warns us that this conversion may be problematic. Yet we blaze ahead because we know we need to do this. To turn off the warning, we add a compiler directive. Like this example of a no-arg constructor:

 @SuppressWarnings("unchecked") public MyArrayList() { array = (E[]) new Object[10]; size = 0; } 

In addition to implementing MyList interface, provide two constructors:

one with no parameters that initializes the array to a capacity of 10 elements, and

one getting the initial capacity of the array list from the parameter. Last, add two more methods:

getIdentificationString() which returns a String "Program 6, FullNameHere", and

getCapacity() which returns the capacity (not the size) of the internal array structure

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 MyArrayList, but it will be fairly similar.

Turning in your program

Upload to ZyBooks: (1) MyArrayList.java (2) MyAbstractList.java (3) MyList.java [ outline follows ]

Your program will be graded automatically against the requirements.

You may submit as many times as necessary.

The automatic grading program is very specific. If you feel you have the correct solution but are not receiving full credit, please

Carefully review the output -- you might need to scroll all the way to the right to find what is wrong with a particular output.

Verify you have the correct names for the program itself and all methods.

Check your calculations by hand: was there a logic error?

Review the requirements: did you miss a step? misinterpret a requirement?

If all these check out, contact the T.A. for assistance.

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 ArrayList instance to be the list's current size. An application can use this * operation to minimize the storage of an ArrayList 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; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 Tsukuba Japan April 2010 Proceedings Part 1 Lncs 5981

Authors: Hiroyuki Kitagawa ,Yoshiharu Ishikawa ,Wenjie Li ,Chiemi Watanabe

2010th Edition

3642120253, 978-3642120251

More Books

Students also viewed these Databases questions

Question

fscanf retums a special value EOF that stands for...

Answered: 1 week ago

Question

What is human nature?

Answered: 1 week ago