Question

In: Computer Science

Due 2/14 by end of day Your first assignment is to create the barebones outline of...

Due 2/14 by end of day

Your first assignment is to create the barebones outline of your first data structure - The ArrayList - with minimal functionality.

1) Open up your IDE and create a new Project. Call it "PDSSpring2019".
2) Inside your project create a new package.  Call it "adt"
3) Inside the adt package create an interface.  Call it "List"
4) 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 - 

boolean add(E e)
void    add(int index, E element)
boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)
void    clear()
boolean contains(Object o)
boolean containsAll(Collection<?> c)
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)
boolean removeAll(Collection<?> c)
E       set(int index, E element)
int     size()
List<E>   subList(int fromIndex, int toIndex)
Object[]        toArray()

You also need the appropriate Javadoc.  To get the outline of the functions please visit the Javadoc at

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

NOTE - It's OK to leave off the Exceptions for now because you aren't sure which Exceptions you're going to need to throw yet.

5) You need to create a new file, ArrayList.java that implements the List interface.  For most of your methods you can create stubs but you will
implement the following

Constructors: All 3 located here
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Methods:
boolean add(E e)
void    add(int index, E element)
int     size()

NOTE * You will probably need the add method to perform one of the constructors.



Solutions

Expert Solution

Solution(s):

//Interface implementation
interface List
{
boolean add(E e);
void add(int index E element);
boolean addAll(Collection<? extends E> c);
boolean addAll(int index,Collection<? extends E> c);
void clear();
boolean contains(Object o);
boolean containsAll(Collection<? extends E> c);
boolean equals(Object o);
E get (int index);
int indexOf(Object o);
boolean isEmpty();
E remove(int index);
boolean remove(Object o);
boolean removeAll(Collection<? extends E> c);
E set(int index,Element e);
int size();
List<E> sublist(int fromIndex, int toIndex);
Object[] toArray();
  
}

class ArrayList implements List
{
private Object[] arrayList;
private int size = 0;
//initializes empty arrayList of default size 10
public ArrayList()
{
arrayList=new Object[10];
}
//initializes empty arrayList of given size
public ArrayList(int capacity)
{
arrayList=new Object[capacity];
}
//add object to arrayList
public boolean add(E e)
{
if(arrayList.length-size <= 5){
increaseListSize();
}
arrayList[size++] = e;
return true;
}
//adds all Objects to arrayList
public boolean addAll(Collection<? extends E> c) {
//converting Collection to array
Object[] a = c.toArray();
int newlength = a.length;
increaseListSize(size + newlength);
System.arraycopy(a, 0, arrayList, size, newlength);
size += newlength;
return newlength != 0;
}
//Uses indexOf() to find the index of the object
public boolean contains(Object o)
{
//it ll return -1 if the object is not in the arrayList
if(arrayList.indexOf(o)>0)
return true;
  
return false;
}
//use equals() to check if all the objects are in the arrayList
boolean containsAll(Collection<? extends E> c)
{
Object[] a = c.toArray();
int check=0;
int newlength=a.length;
if(a!=size)
return false;
for(int i=0;i<size;i++)
{
if(arrayList.equals(a[i]))
check++;
}
if(check==size)
return true;
return false;

}
boolean equals(Object o)
{
for(int i=0;i<size;i++)
{
if(arrayList[i]==o)
return true;
}
return false;
  
}
public Object get(int index){
if(index < size){
return arrayList[index];
} else {
return null;
}
}
int indexOf(Object o)
{
for(int i=0;i<size;i++)
{
if(arrayList[i].equals(o))
return i;
}
return -1;
}
public void clear()
{   
//setting arrayList objects to null
for(int i=0;i<size;i++)
arrayList[i]=null;
size=0;
}
boolean isEmpty()
{
//returns false if size !=0
return(size==0);
}
//calls remove(int index) to delete the object
public boolean remove(Object o)
{
if(arrayList.contains(o))
{
int s=size;
int index=indexOf(o)
Object o=remove(index);
if(s>size)
return true;
}
return false;
}
public Object remove(int index){
if(index < size){
Object obj = arrayList[index];
arrayList[index] = null;
int temp = index;
//shifting the array
while(temp < size){
arrayList[temp] = arrayList[temp+1];
arrayList[temp+1] = null;
temp++;
}
size--;
return obj;
} else {
return null;
}

}
public boolean removeAll()
{
boolean removed=false;
Iterator<?> e = iterator();
while (e.hasNext()) {
if (arrayList.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
  
}
public Object set(int index,Element e)
{
arrayList[index]=e;
return e;
}
//to control growth of arrayList
private void increaseListSize(){
arrayList = Arrays.copyOf(arrayList, arrayList.length*2);
}
  

int size()
{
return size;
}
  
List<Object> sublist(int fromIndex, int toIndex)
{
//new ArrayList
ArrayList<Object> newList=new ArrayList<>();
for(int i=fromIndex;i<toIndex;i++)
{
newList.add(arrayList[i]);
}
return newList;
}
public Object[] toArray()
{
Object[] myArray = new Object[ObjectList.size()];
ObjectList.toArray(newArray);
}
  
}

Hope this helps. Thanks!


Related Solutions

On your first day on a new IT job, your boss gives you this assignment: Because...
On your first day on a new IT job, your boss gives you this assignment: Because you completed coursework on usability, you are responsible for evaluating the usability of the company website. Include hard data in your report. You are required to use this definition of usability: The usability of a system or any product is a key factor for all the organizations determining that everyone is having a good user experience and easy to use. In the cutting-edge technology,...
They say that your first day of internship is essentially like your first day of school:...
They say that your first day of internship is essentially like your first day of school: part two. I see it as a 180 hour job interview, but what exactly is internship? Using your own words, explain the following discussion topics. 1. What is internship? 2. What is the value or benefits of internship? 3. What can you do today to start preparing for internship?
Your assignment this week has two parts: First, you will create a timeline or infographic to...
Your assignment this week has two parts: First, you will create a timeline or infographic to address the following questions: Research and provide examples or comparisons that demonstrate these changes over time from each of the categories: Print, Radio/Music, Cinema, Television, and the Internet. Include images of each artifact. How have these various forms of communication have evolved since they were first introduced? (Give a “before” and “after”) Second, you will write a mini-statement answering the following question. In 300-500...
14,       A residential property is acquired on the first day of the tax year for a...
14,       A residential property is acquired on the first day of the tax year for a purchase price of $300,000 plus acquisition costs of $15,000. The property is held for five years and sold on the last day of the tax year. Tax Assessment Allocation Percentage Basis Allocation Land $ 60,000 30% $94,500 Improvements +    $140,000 70% $220,500 TOTAL Assessments $200,000 a. What is the cost-recovery deduction for each full year of acquisition?                                       b. What is the annual cost-recovery...
This will be your first collaborative assignment with your team. Your team will submit one 1-2...
This will be your first collaborative assignment with your team. Your team will submit one 1-2 page plan outlining the methods your team will use to work together. Topics such as communications, expectations of team members, and technology use should be covered.
For this assignment, you will create three 5-slide presentations for your local Planned Parenthood. The first...
For this assignment, you will create three 5-slide presentations for your local Planned Parenthood. The first presentation is for a monthly information session on puberty and development. They have asked you to come in as a health professional and provide a guest lecturer on the topic of male and female development. The second presentation is for women who are in their first trimester and their partners. They have asked you to explain prenatal development, parturition, and lactation. The final presentation...
Task Create an outline (plan to write your message – sample outline provided below) and an...
Task Create an outline (plan to write your message – sample outline provided below) and an email message based on case details identified below. Situation You work as an assistant office manager for Diamond Clear, a family-run window installation company and a scheduling issue has arisen in the office. You are writing on behalf of Lucia to her brother Scott regarding the situation (includes the problem) and your request (main idea). In your email message you will inform Scott that...
Create an outline of your project (1 page). The outline should only contain topic headings and...
Create an outline of your project (1 page). The outline should only contain topic headings and a brief description of what it will cover. See the link below for an APA formatted outline. Remember that your outline will be a guide as you write your paper to keep you on topic and organized. Required paragraph topics include but are not limited to: the role nutrition plays in the prevention of the disease, etiology, progression, treatment, recommended diets, nursing assessment, nursing...
1. For your first assignment, management has provided the following revenue and cost information: High-end set...
1. For your first assignment, management has provided the following revenue and cost information: High-end set economical set Sales price $3,500 price unit $1,000 Per unit Labor $875 per unit $250 per unit materials $1,400 per unit $300 per unit direct fixed costs $25,000 per month $16,500 per month allocated fixed cost $85,000 per month $85,000 per month They want a better understanding of their business to make budgeting and sales goals decisions and have asked you to determine their:For...
14) Outline the dispersion of nanoparticles in nanocomposites filled with 2-D nanoparticles. In short
14) Outline the dispersion of nanoparticles in nanocomposites filled with 2-D nanoparticles. In short
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT