SlideShare a Scribd company logo
1 of 9
Download to read offline
For this lab you will complete the class MyArrayList by implementing the MyList interface
provided. The implementation you write must use an array of Object, declared like this:
private Object[] theList;
To successfully implement the interface you must provide a non-abstract version of all the
methods in the interface.
Add functionality that ensures only one type of reference can be added to your
list. In other words, make your list "type-safe'.
Create an overloaded constructor in MyArrayList public MyArrayList( Object type). The
constructor parameter will
be used to set the type of reference to be added to the list. Set up a private filed of the same type
as the parameter. You will also instantiate your Object[] in this constructor. Do not change the
default constructor.
In public boolean add(Object toAdd) you will first check the toAdd parameter to ensure it is the
same as
the type you set in the overloaded constructor. If it is then go ahead and add the reference to
your list. If
not then return false and display an error message indicating the method parameter was rejected
due to
incompatible type.
To check the "toAdd" parameter you will have to first have to get the type of class it is (HINT:
look at
Object methods). Next, you will need to check its type against the type you established in the
overloaded constructor (HINT: look at the Class methods).
Finally, provide a driver class that will:
1) create the an instance of MyList and then add references to it. Be sure to try incompatible
types.
2) Check to see if the list is empty.
3) Display the contents of the list.
4) Display the size of the list.
5) Display the first element in the list.
6) Remove the first element in the list.
7) Display the list one final time.
Modify the classes bellow to match the information above:
/**
* MyArrayList - an imlementation of an array list based on a Java array.
*
* @author Colleen
* @version 2013.11.15
*/
public class MyArrayList
{
private Object[] theList; // array of objects
/**
* Constructor - start with an empty array
*/
public MyArrayList()
{
theList = new Object[0];
}
/**
* Adds a new element at the end of the list.
* @param the object to add
* @return true if element successfully added, false if parameter is null
*/
public boolean add(Object toAdd){
return false;
}
/**
* Gets the object at the specified index.
* @param index value of object to get
* @return object at that index
*/
public Object get(int index){
return null;
}
/**
* Removes specified object from list.
* @param index value of object to remove
* @return the object removed
*/
public Object remove(int index) {
return null;
}
/**
* Returns size of the list
* @return number of elements in the list
*/
public int size(){
return 0;
}
/**
* @return true if the list has no elements, false otherwise
*/
public boolean isEmpty(){
return false;
}
}
------------------------------------
/**
* Write a description of interface List here.
*
* @author (your name)
* @version (a version number or a date)
*/
public interface MyList
{
/**
* Adds a new element at the end of the list.
* @param the object to add
* @return true if element successfully added, otherwise false
*/
boolean add(Object toAdd);
/**
* Gets the object at the specified index.
* @param index value of object to get
* @return object at that index
*/
Object get(int index);
/**
* Removes specified object from list.
* @param index value of object to remove
* @return the object removed
*/
Object remove(int index);
/**
* Returns size of the list
* @return number of elements in the list
*/
int size();
/**
* @return true if the list has no elements, false otherwise
*/
boolean isEmpty();
}
Solution
/**
* Write a description of interface List here.
*
* @author (your name)
* @version (a version number or a date)
*/
public interface MyList {
/**
* Adds a new element at the end of the list.
*
* @param the
* object to add
* @return true if element successfully added, otherwise false
*/
boolean add(Object toAdd);
/**
* Gets the object at the specified index.
*
* @param index
* value of object to get
* @return object at that index
*/
Object get(int index);
/**
* Removes specified object from list.
*
* @param index
* value of object to remove
* @return the object removed
*/
Object remove(int index);
/**
* Returns size of the list
*
* @return number of elements in the list
*/
int size();
/**
* @return true if the list has no elements, false otherwise
*/
boolean isEmpty();
}
-----------------------------------------
import java.util.Arrays;
/**
* MyArrayList - an imlementation of an array list based on a Java array.
*
* @author Colleen
* @version 2013.11.15
*/
public class MyArrayList implements MyList {
private Object[] theList; // array of objects
Class c; //type of reference
int size = 0; //size of array
/**
* Constructor - start with an empty array
*/
public MyArrayList() {
theList = new Object[0];
}
public MyArrayList(Object type) {
c = type.getClass(); //initialize the array type
theList = new Object[1]; //initialize the array with size 1
theList[0] = type; //initialize the element in the array
size = size + 1;
}
/**
* Adds a new element at the end of the list.
*
* @param the
* object to add
* @return true if element successfully added, false if parameter is null
*/
public boolean add(Object toAdd) {
if (this.c != null && !(toAdd.getClass().equals(this.c))) {
System.out.println("Incompatible Argument type " + toAdd);
return false;
} else if (this.c == null) {
this.c = toAdd.getClass();
theList = new Object[1];
} else if (toAdd.getClass().equals(this.c)) {
theList = Arrays.copyOf(theList, this.size + 1);
}
this.size = this.size + 1;
theList[this.size - 1] = toAdd;
return true;
}
/**
* Gets the object at the specified index.
*
* @param index
* value of object to get
* @return object at that index
*/
public Object get(int index) {
if (!(this.isEmpty())) {
return theList[index];
} else
return null;
}
/**
* Removes specified object from list.
*
* @param index
* value of object to remove
* @return the object removed
*/
public Object remove(int index) {
Object item = theList[index];
for (int i = index; i < this.size - 1; i++) {
theList[i] = theList[i + 1];
}
this.size = this.size - 1;
theList = Arrays.copyOf(theList, this.size);
System.out.println("Element removed successfully.");
return item;
}
/**
* Returns size of the list
*
* @return number of elements in the list
*/
public int size() {
return size;
}
/**
* @return true if the list has no elements, false otherwise
*/
public boolean isEmpty() {
if (size == 0) {
return true;
}
return false;
}
}
----------------------------------------------------
public class TestClass {
public static void main(String[] args) {
MyList obj = new MyArrayList();
obj.add(1);
obj.add(5);
obj.add(7);
obj.add("test");
obj.add(9);
obj.add(4);
System.out.println("Is this List empty: "+obj.isEmpty());
System.out.println("contents of the list :");
for (int i = 0; i < obj.size(); i++) {
System.out.println(obj.get(i));
}
System.out.println("Size of the List: " + obj.size());
System.out.println("first element in the list: "+obj.get(0));
obj.remove(0);
obj.remove(3);
System.out.println("contents of the list :");
for (int i = 0; i < obj.size(); i++) {
System.out.println(obj.get(i));
}
}
}

More Related Content

Similar to For this lab you will complete the class MyArrayList by implementing.pdf

Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdfImplement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdfrishabjain5053
 
A popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdfA popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdfarsarees
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxalanfhall8953
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdffirstchoiceajmer
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfbabitasingh698417
 
In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfEvanpZjSandersony
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docxKomlin1
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docxajoy21
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfinfo430661
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdfsaradashata
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfConint29
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdfarshin9
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfmayorothenguyenhob69
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdfgudduraza28
 
Engineering lecture ppt by venay magen
Engineering lecture ppt by venay magenEngineering lecture ppt by venay magen
Engineering lecture ppt by venay magenvenaymagen19
 
import java.util.ArrayList; import java.util.List;public class S.pdf
import java.util.ArrayList; import java.util.List;public class S.pdfimport java.util.ArrayList; import java.util.List;public class S.pdf
import java.util.ArrayList; import java.util.List;public class S.pdfanupamele
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfaggarwalopticalsco
 
package ADTs public interface CollectionADTltTgt .pdf
package ADTs public interface CollectionADTltTgt      .pdfpackage ADTs public interface CollectionADTltTgt      .pdf
package ADTs public interface CollectionADTltTgt .pdfsyedabdul78662
 

Similar to For this lab you will complete the class MyArrayList by implementing.pdf (20)

Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdfImplement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
 
A popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdfA popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdf
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
 
In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docx
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdf
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
 
Java collections
Java collectionsJava collections
Java collections
 
Engineering lecture ppt by venay magen
Engineering lecture ppt by venay magenEngineering lecture ppt by venay magen
Engineering lecture ppt by venay magen
 
import java.util.ArrayList; import java.util.List;public class S.pdf
import java.util.ArrayList; import java.util.List;public class S.pdfimport java.util.ArrayList; import java.util.List;public class S.pdf
import java.util.ArrayList; import java.util.List;public class S.pdf
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
 
package ADTs public interface CollectionADTltTgt .pdf
package ADTs public interface CollectionADTltTgt      .pdfpackage ADTs public interface CollectionADTltTgt      .pdf
package ADTs public interface CollectionADTltTgt .pdf
 

More from fashiongallery1

Discuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdfDiscuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdffashiongallery1
 
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdfDiscrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdffashiongallery1
 
Describe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdfDescribe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdffashiongallery1
 
Describe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdfDescribe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdffashiongallery1
 
Can someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdfCan someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdffashiongallery1
 
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdfAssume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdffashiongallery1
 
All computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdfAll computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdffashiongallery1
 
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdffashiongallery1
 
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdfBriefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdffashiongallery1
 
Write a class that implements the BagInterface. BagInterface should .pdf
Write a class that implements the BagInterface.  BagInterface should .pdfWrite a class that implements the BagInterface.  BagInterface should .pdf
Write a class that implements the BagInterface. BagInterface should .pdffashiongallery1
 
write an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdfwrite an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdffashiongallery1
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdfWhy are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdffashiongallery1
 
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdfWhich of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdffashiongallery1
 
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdfWhat made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdffashiongallery1
 
What data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdfWhat data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdffashiongallery1
 
1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdffashiongallery1
 
VERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdfVERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdffashiongallery1
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdffashiongallery1
 
The __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdfThe __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdffashiongallery1
 

More from fashiongallery1 (20)

Discuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdfDiscuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdf
 
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdfDiscrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
 
Describe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdfDescribe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdf
 
Describe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdfDescribe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdf
 
Can someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdfCan someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdf
 
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdfAssume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
 
All computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdfAll computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdf
 
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
 
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdfBriefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
 
Write a class that implements the BagInterface. BagInterface should .pdf
Write a class that implements the BagInterface.  BagInterface should .pdfWrite a class that implements the BagInterface.  BagInterface should .pdf
Write a class that implements the BagInterface. BagInterface should .pdf
 
write an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdfwrite an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdfWhy are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdf
 
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdfWhich of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
 
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdfWhat made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
 
What data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdfWhat data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdf
 
1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf
 
VERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdfVERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdf
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
 
The __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdfThe __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdf
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 

For this lab you will complete the class MyArrayList by implementing.pdf

  • 1. For this lab you will complete the class MyArrayList by implementing the MyList interface provided. The implementation you write must use an array of Object, declared like this: private Object[] theList; To successfully implement the interface you must provide a non-abstract version of all the methods in the interface. Add functionality that ensures only one type of reference can be added to your list. In other words, make your list "type-safe'. Create an overloaded constructor in MyArrayList public MyArrayList( Object type). The constructor parameter will be used to set the type of reference to be added to the list. Set up a private filed of the same type as the parameter. You will also instantiate your Object[] in this constructor. Do not change the default constructor. In public boolean add(Object toAdd) you will first check the toAdd parameter to ensure it is the same as the type you set in the overloaded constructor. If it is then go ahead and add the reference to your list. If not then return false and display an error message indicating the method parameter was rejected due to incompatible type. To check the "toAdd" parameter you will have to first have to get the type of class it is (HINT: look at Object methods). Next, you will need to check its type against the type you established in the overloaded constructor (HINT: look at the Class methods). Finally, provide a driver class that will: 1) create the an instance of MyList and then add references to it. Be sure to try incompatible types. 2) Check to see if the list is empty. 3) Display the contents of the list. 4) Display the size of the list. 5) Display the first element in the list.
  • 2. 6) Remove the first element in the list. 7) Display the list one final time. Modify the classes bellow to match the information above: /** * MyArrayList - an imlementation of an array list based on a Java array. * * @author Colleen * @version 2013.11.15 */ public class MyArrayList { private Object[] theList; // array of objects /** * Constructor - start with an empty array */ public MyArrayList() { theList = new Object[0]; } /** * Adds a new element at the end of the list. * @param the object to add * @return true if element successfully added, false if parameter is null */ public boolean add(Object toAdd){ return false; } /** * Gets the object at the specified index. * @param index value of object to get * @return object at that index */ public Object get(int index){
  • 3. return null; } /** * Removes specified object from list. * @param index value of object to remove * @return the object removed */ public Object remove(int index) { return null; } /** * Returns size of the list * @return number of elements in the list */ public int size(){ return 0; } /** * @return true if the list has no elements, false otherwise */ public boolean isEmpty(){ return false; } } ------------------------------------ /** * Write a description of interface List here. * * @author (your name)
  • 4. * @version (a version number or a date) */ public interface MyList { /** * Adds a new element at the end of the list. * @param the object to add * @return true if element successfully added, otherwise false */ boolean add(Object toAdd); /** * Gets the object at the specified index. * @param index value of object to get * @return object at that index */ Object get(int index); /** * Removes specified object from list. * @param index value of object to remove * @return the object removed */ Object remove(int index); /** * Returns size of the list * @return number of elements in the list */ int size(); /** * @return true if the list has no elements, false otherwise */ boolean isEmpty();
  • 5. } Solution /** * Write a description of interface List here. * * @author (your name) * @version (a version number or a date) */ public interface MyList { /** * Adds a new element at the end of the list. * * @param the * object to add * @return true if element successfully added, otherwise false */ boolean add(Object toAdd); /** * Gets the object at the specified index. * * @param index * value of object to get * @return object at that index */ Object get(int index); /** * Removes specified object from list. * * @param index * value of object to remove * @return the object removed */ Object remove(int index); /**
  • 6. * Returns size of the list * * @return number of elements in the list */ int size(); /** * @return true if the list has no elements, false otherwise */ boolean isEmpty(); } ----------------------------------------- import java.util.Arrays; /** * MyArrayList - an imlementation of an array list based on a Java array. * * @author Colleen * @version 2013.11.15 */ public class MyArrayList implements MyList { private Object[] theList; // array of objects Class c; //type of reference int size = 0; //size of array /** * Constructor - start with an empty array */ public MyArrayList() { theList = new Object[0]; } public MyArrayList(Object type) { c = type.getClass(); //initialize the array type theList = new Object[1]; //initialize the array with size 1 theList[0] = type; //initialize the element in the array size = size + 1; } /** * Adds a new element at the end of the list.
  • 7. * * @param the * object to add * @return true if element successfully added, false if parameter is null */ public boolean add(Object toAdd) { if (this.c != null && !(toAdd.getClass().equals(this.c))) { System.out.println("Incompatible Argument type " + toAdd); return false; } else if (this.c == null) { this.c = toAdd.getClass(); theList = new Object[1]; } else if (toAdd.getClass().equals(this.c)) { theList = Arrays.copyOf(theList, this.size + 1); } this.size = this.size + 1; theList[this.size - 1] = toAdd; return true; } /** * Gets the object at the specified index. * * @param index * value of object to get * @return object at that index */ public Object get(int index) { if (!(this.isEmpty())) { return theList[index]; } else return null; } /** * Removes specified object from list. * * @param index
  • 8. * value of object to remove * @return the object removed */ public Object remove(int index) { Object item = theList[index]; for (int i = index; i < this.size - 1; i++) { theList[i] = theList[i + 1]; } this.size = this.size - 1; theList = Arrays.copyOf(theList, this.size); System.out.println("Element removed successfully."); return item; } /** * Returns size of the list * * @return number of elements in the list */ public int size() { return size; } /** * @return true if the list has no elements, false otherwise */ public boolean isEmpty() { if (size == 0) { return true; } return false; } } ---------------------------------------------------- public class TestClass { public static void main(String[] args) { MyList obj = new MyArrayList(); obj.add(1);
  • 9. obj.add(5); obj.add(7); obj.add("test"); obj.add(9); obj.add(4); System.out.println("Is this List empty: "+obj.isEmpty()); System.out.println("contents of the list :"); for (int i = 0; i < obj.size(); i++) { System.out.println(obj.get(i)); } System.out.println("Size of the List: " + obj.size()); System.out.println("first element in the list: "+obj.get(0)); obj.remove(0); obj.remove(3); System.out.println("contents of the list :"); for (int i = 0; i < obj.size(); i++) { System.out.println(obj.get(i)); } } }