Question

In: Computer Science

Create a List Create a class that holds an ordered list of items. This list should...

Create a List

Create a class that holds an ordered list of items. This list should have a variable size, most importantly this class should implement the interface SimpleArrayList. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other words, you have to write the code for each of the functions specified in the SimpleArrayList interface.

You are not allowed to use any 3rd party data structures or libraries such as Java.Utils.ArrayList or Java.awt.ArrayList

Please use the class SimpleArrayListUnitTest.java to test your code, it is a JUnit test that contains 49 different tests that test all the functions of your class.

(using Eclipse.. I don't have any idea :()

Solutions

Expert Solution

/**********************SimpleArrayList.java**************/


/**
* The Interface SimpleArrayList.
*/
public interface SimpleArrayList {


   /**
   * Size.
   *
   * @return the int
   */
   public int size();

   /**
   * Checks if is empty.
   *
   * @return true, if is empty
   */
   public boolean isEmpty();

   /**
   * Index of.
   *
   * @param obj the obj
   * @return the int
   */
   public int indexOf(Object obj);

   /**
   * Contains.
   *
   * @param obj the obj
   * @return true, if successful
   */
   public boolean contains(Object obj);

   /**
   * Adds the.
   *
   * @param obj the obj
   * @return true, if successful
   */
   public boolean add(Object obj);

   /**
   * Clear.
   */
   public void clear();

   /**
   * Gets the.
   *
   * @param pos the pos
   * @return the object
   */
   public Object get(int pos);

   /**
   * Sets the.
   *
   * @param pos the pos
   * @param obj the obj
   * @return the object
   */
   public Object set(int pos, Object obj);

   /**
   * Removes the.
   *
   * @param pos the pos
   * @return the object
   */
   public Object remove(int pos);

   /**
   * Equals.
   *
   * @param obj the obj
   * @return true, if successful
   */
   public boolean equals(Object obj);

}

/*************************MyArrayList.java*********************/


/**
* The Class MyArrayList.
*/
public class MyArrayList implements SimpleArrayList {

   /** The items. */
   private Object[] items;

   /** The size. */
   private int size;

   /** The Constant CAPACITY. */
   private static final int CAPACITY = 5;

   /**
   * Instantiates a new my array list.
   */
   public MyArrayList() {

       this(CAPACITY);
   }

   /**
   * Instantiates a new my array list.
   *
   * @param capacity2 the capacity 2
   */
   public MyArrayList(int capacity2) {
       items = new Object[capacity2];
       size = 0;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#size()
   */
   @Override
   public int size() {
       return size;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#isEmpty()
   */
   @Override
   public boolean isEmpty() {
       return size() == 0;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#indexOf(java.lang.Object)
   */
   @Override
   public int indexOf(Object obj) {
       for (int i = 0; i < size(); i++) {
           if (items[i].equals(obj)) {
               return i;
           }
       }
       return -1;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#contains(java.lang.Object)
   */
   @Override
   public boolean contains(Object obj) {
       return indexOf(obj) != -1;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#add(java.lang.Object)
   */
   @Override
   public boolean add(Object obj) {
       if (obj == null) {
           throw new IllegalArgumentException();
       }
       ensureSpareCapacity(1);
       items[size] = obj;
       size++;
       return true;
   }

   /**
   * Ensure spare capacity.
   *
   * @param i the i
   */
   private void ensureSpareCapacity(int i) {
       if (size() + i <= items.length) {
           return;
       }
       Object[] newItems = new Object[items.length * 2 + i];
       for (int k = 0; k < size(); k++) {
           newItems[k] = items[k];
       }
       items = newItems;

   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#clear()
   */
   @Override
   public void clear() {
       for (int k = 0; k < size; k++) {
           items[k] = null;
       }
       size = 0;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#get(int)
   */
   @Override
   public Object get(int pos) {
       checkPosition(pos);
       return items[pos];
   }

   /**
   * Check position.
   *
   * @param pos the pos
   */
   private void checkPosition(int pos) {
       if (pos < 0 || pos >= size()) {
           throw new IndexOutOfBoundsException();
       }

   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#set(int, java.lang.Object)
   */
   @Override
   public Object set(int pos, Object obj) {
       checkPosition(pos);
       Object result = items[pos];
       items[pos] = obj;
       return result;
   }

   /*
   * (non-Javadoc)
   *
   * @see SimpleArrayList#remove(int)
   */
   @Override
   public Object remove(int pos) {
       checkPosition(pos);
       Object result = items[pos];
       size--;
       // shift to the left
       for (int k = pos; k < size; k++) {
           items[k] = items[k + 1];
       }
       items[size] = null;
       return result;
   }

   public boolean equals(Object obj) {
   if (!(obj instanceof SimpleArrayList)) {
   return false;
   }
   MyArrayList other = (MyArrayList) obj;
   if (this.size() != other.size()) {
   return false;
   }
   for (int k = 0; k < size(); k++) {
   if (!this.items[k].equals(other.items[k])) {
   return false;
   }
   }
   return true;
   }

@Override
   public String toString() {
       return Arrays.toString(items);
   }

}
/*********************TestMyArrayList.java************/


public class TestMyArrayList {

   public static void main(String[] args) {
      
       SimpleArrayList ar = new MyArrayList();
      
       ar.add("JKS");
       ar.add(12);
       ar.add(new String("Virat"));
       ar.add(56);
       ar.remove(2);
       ar.add(67);
       ar.add("MS");
       System.out.println(ar.size());
       System.out.println(ar.indexOf(56));
       System.out.println(ar.toString());
      
   }
}

/**************output****************/

5
2
[JKS, 12, 56, 67, MS]

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Assignment: The Ordered List In this assignment, you will create an ordered list of movies. The...
Assignment: The Ordered List In this assignment, you will create an ordered list of movies. The list will always be maintained in sorted order (ascending order, by movie title) by assuring that all new movies are inserted in the correct location in the list. Create a new project to hold your implementation of an ordered singly-linked list. You will need a main.cppto use as a test driver, as well as header and implementation files as described below. Movie To represent...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
Create an unsorted LIST class. Each list should be able to store 100 names.
Create an unsorted LIST class. Each list should be able to store 100 names.
Create an ADT class that creates a friend contact list. The program should be able to...
Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++
Trying to create a Class of Employees that holds every Employee created from a class called...
Trying to create a Class of Employees that holds every Employee created from a class called Employee in c++
java For this assignment, you will create a Time class that holds an hour value and...
java For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 AM is 0001 and 1 PM is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 AM to 11:59 PM. In previous assignments, we had a requirement that your class be named Main. In this assignment, the...
Create a class Team to hold data about a college sports team. The Team class holds...
Create a class Team to hold data about a college sports team. The Team class holds data fields for college name (such as Hampton College), sport (such as Soccer), and team name (such as Tigers). Include a constructor that takes parameters for each field, and get methods that return the values of the fields. Also include a public final static String named MOTTO and initialize it to Sportsmanship! Save the class in Team.java. Create a UML class diagram as well....
Create a class Team to hold data about a college sports team. The Team class holds...
Create a class Team to hold data about a college sports team. The Team class holds data fields for college name (such as Hampton College), sport (such as Soccer), and team name (such as Tigers). Include a constructor that takes parameters for each field, and get methods that return the values of the fields. Also include a public final static String named MOTTO and initialize it to Sportsmanship! Save the class in Team.java. Write an application named TestTeam with the...
a. Design a class named ItemForSale that holds data about items placed for sale on Carlos's...
a. Design a class named ItemForSale that holds data about items placed for sale on Carlos's List, a classified advertising website. Fields include an ad number, item description, asking price, and phone number. Include get and set methods for each field. Include a static method that displays the website's motto ("Sell Stuff Locally!"). Include two overloaded constructors as follows: A default constructor that sets the ad number to 101, the asking price to $1, and the item description and phone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT