SlideShare a Scribd company logo
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.pdf
annaelctronics
 
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
rishabjain5053
 
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
arsarees
 
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
alanfhall8953
 
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
firstchoiceajmer
 
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
babitasingh698417
 
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
EvanpZjSandersony
 
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
Komlin1
 
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
ajoy21
 
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
info430661
 
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
saradashata
 
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
Conint29
 
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
arshin9
 
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
mayorothenguyenhob69
 
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
gudduraza28
 
Java collections
Java collectionsJava collections
Java collections
adamborisoff
 
Engineering lecture ppt by venay magen
Engineering lecture ppt by venay magenEngineering lecture ppt by venay magen
Engineering lecture ppt by venay magen
venaymagen19
 
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
anupamele
 
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
aggarwalopticalsco
 
package ADTs public interface CollectionADTltTgt .pdf
package ADTs public interface CollectionADTltTgt      .pdfpackage ADTs public interface CollectionADTltTgt      .pdf
package ADTs public interface CollectionADTltTgt .pdf
syedabdul78662
 

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.pdf
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 
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
fashiongallery1
 

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

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 

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)); } } }