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

PDF
Everything needs to be according to the instructions- thank you! SUPPO.pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
PDF
Java ArrayList Tutorial | Edureka
PPTX
Java collections
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
PDF
I need help with this code working Create another project and add yo.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
A2003822018_21789_17_2018_09. ArrayList.ppt
Java ArrayList Tutorial | Edureka
Java collections
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
I need help with this code working Create another project and add yo.pdf

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

PPT
L11 array list
DOCX
ArrayList.docx
PPTX
ArrayList class and useful methods.pptx
PPTX
collection framework.pptx
PDF
Question 1 MiniList collection 20 marks In this question .pdf
DOCX
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
PPTX
Collections - Lists & sets
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PPT
12_-_Collections_Framework
PDF
Java collections
DOCX
Please complete all the code as per instructions in Java programming.docx
DOCX
Array list
PDF
Prompt Your task is to create a connected list implementation and .pdf
PDF
Write a java class LIST that outputsmainpublic class Ass.pdf
PPTX
arraylist in java a comparison of the array and arraylist
PPTX
arraylistinjava.pptx
PDF
A popular implementation of List is ArrayList- Look up how to instanti.pdf
PPT
Collection Framework.power point presentation.......
DOCX
Java collections notes
PPTX
Collection implementation classes - Arraylist, linkedlist, Stack
L11 array list
ArrayList.docx
ArrayList class and useful methods.pptx
collection framework.pptx
Question 1 MiniList collection 20 marks In this question .pdf
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Collections - Lists & sets
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
12_-_Collections_Framework
Java collections
Please complete all the code as per instructions in Java programming.docx
Array list
Prompt Your task is to create a connected list implementation and .pdf
Write a java class LIST that outputsmainpublic class Ass.pdf
arraylist in java a comparison of the array and arraylist
arraylistinjava.pptx
A popular implementation of List is ArrayList- Look up how to instanti.pdf
Collection Framework.power point presentation.......
Java collections notes
Collection implementation classes - Arraylist, linkedlist, Stack
Ad

More from fashiongallery1 (20)

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

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
Classroom Observation Tools for Teachers
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
Cell Types and Its function , kingdom of life
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
human mycosis Human fungal infections are called human mycosis..pptx
Presentation on HIE in infants and its manifestations
Classroom Observation Tools for Teachers
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
Chinmaya Tiranga quiz Grand Finale.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Microbial disease of the cardiovascular and lymphatic systems
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GDM (1) (1).pptx small presentation for students
RMMM.pdf make it easy to upload and study

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