Question

You are to fully program the List interface using Java generics and Javadoc. Please note -...

You are to fully program the List interface using Java generics and Javadoc.  Please
note - an interface doesn't account for implementation.  It is just the
function definitions and Javadoc.  The following functions need to be present - 
please post screenshot of the code with these functions implemented 

boolean add(E e)
void    add(int index, E element)
void    clear()
boolean contains(Object o)
boolean equals(Object o)
E       get(int index)
int     indexOf(Object o)
boolean isEmpty()
int     lastIndexOf(Object o)
E       remove(int index)
boolean remove(Object o)
E       set(int index, E element)
int     size()
Object[]        toArray()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

public class MyArrayList<E> {

// YOUR CODE ANYWHERE IN THIS CLASS

private Object[] array;

public static final int CAPACITY = 4;

public int size;

public int capacity;

// there could be more instance variables!

/*

* initialise array with capacity. If capacity is less than 1, use CAPACITY.

*

*/

@SuppressWarnings("unchecked")

public MyArrayList() {

array = new Object[CAPACITY];

this.capacity = 4;

size = 0;

}

public MyArrayList(int capacity) {

array = new Object[capacity];

this.capacity = capacity;

size = 0;

}

public void add(int i, E val) {

if (i >= capacity)

return;

else

{

size++;

for (int x = size - 1; x > i; x--) {

array[x] = array[x - 1];

}

array[i] = val;

}

}

public void add(E x) {

if (size >= capacity){

Object[] temp = new Object[2*(array.length)];

for(int i=0;i<size;++i){

temp[i] = array[i];

}

this.capacity = 2*array.length;

array = temp;

array[size++] = x;

return;

}

else

{

array[size++] = x;

}

}

public void set(int i, E val) {

// YOUR CODE ANYWHERE IN THIS CLASS

if (i >= capacity)

return;

else

{

size++;

for (int x = size - 1; x > i; x--) {

array[x] = array[x - 1];

}

array[i] = val;

}

}

/*

* if index is outside range of array. return null

*

*/

public Object get(int i) {

// YOUR CODE ANYWHERE IN THIS CLASS

if (i >= capacity)

throw new ArrayIndexOutOfBoundsException();

else

return array[i];

}

/*

* if index is outside range of array. return null

*

*/

public void remove(int i) {

if (i >= size || size == 0)

return;

else {

Object e = array[i];

for (int x = i; x < this.array.length - 1; x++) {

array[x] = array[x + 1];

}

size--;

}

}

public void remove(E ob) {

// YOUR CODE ANYWHERE IN THIS CLASS

if (size == 0)

return;

else {

int i;

for (i = 0; i < size; ++i) {

if (ob.equals(array[i]))

break;

}

if (i == size)

return;

Object e = array[i];

for (int x = i; x < this.array.length - 1; x++) {

array[x] = array[x + 1];

}

size--;

}

}

public int size() {

return size;

}

public boolean isEmpty() {

return size == 0;

}

public boolean contains(E ob) {

for (int i = 0; i < size; ++i) {

if (ob.equals(array[i]))

return true;

}

return false;

}

public int search(Object ob) {

for (int i = 0; i < size; ++i) {

if (ob.equals(array[i]))

return i;

}

return -1;

}

public String toString() {

String res = "";

for (int i = 0; i < size; ++i) {

res = res + array[i] + " ";

}

return res;

}

@Override

public boolean equals(Object other) {

if (other == null)

return false;

System.out.println(this);

System.out.println((Integer) other);

if (other == this)

return true;

return false;

}

public static void main(String args[]){

MyArrayList<String> myArrayList = new MyArrayList<String>();

myArrayList.add("Rahul");

myArrayList.add("Chegg");

System.out.println(myArrayList.toString());

System.out.println("Size of String arrayList is: " + myArrayList.size );

myArrayList.add("Hello");

System.out.println("Size of String arrayList is: " + myArrayList.size );

for(int i=0;i<myArrayList.size;++i){

System.out.print(myArrayList.array[i] + " ");

}

System.out.println("\n");

MyArrayList<Integer> myArrayList1 = new MyArrayList<Integer>();

myArrayList1.add(1);

myArrayList1.add(2);

System.out.println(myArrayList1.toString());

System.out.println("Size of Integer arrayList is: " + myArrayList1.size );

}

}

============================
SEE OUTPUT

PLEASE COMMENT if there is any concern.

======================

Add a comment
Know the answer?
Add Answer to:
You are to fully program the List interface using Java generics and Javadoc. Please note -...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Please write in Java Recall that the ADT list class methods are;  void List() ...

    Please write in Java Recall that the ADT list class methods are;  void List()  bool isEmpty()  int size()  void add(int item, int pos)//inserts item at specified position (first postion is 1)  void remove(int index)//removes item from specified position  void removeAll()  int indexOf(int item)//returns the index of item  int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...

  • In Java, the List interface provides the following methods (including those inherited from Collection): add, addAll,...

    In Java, the List interface provides the following methods (including those inherited from Collection): add, addAll, clear, contains, containsAll, equals, isEmpty, remove, removeAll, retain, size, toArray, get, indexOf, lastIndexOf, listIterator, set. Of these, which would you select to implement if you were going to implement your own ordered list? Specifically, the list will be used to insert, delete and search for items as well as being able to traverse the list to output the entire thing. Only make reference to...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT