Complete only the following methods of the ArrayWrapper class:
add(E element)
add(int index, E element)
clear()
contains(E o)
get(int index)
isEmpty()
remove(int index)
set(int index, E element)
size()
You need only to demonstrate this to me to receive credit; you do not have to submit it.
To test your implementation, use the following program:
import java.util.List; public class HW2App { public static void main(String[] args) { List<Person> x = new ArrayWrapper<Person>(); System.out.println("IsEmpty: " + x.isEmpty()); // should print "true" Person ann = new Person("Ann", 18, 100); x.add(ann); Person bob = new Person("Bob", 19, 101); x.add(bob); Person carl = new Person("Carl", 18, 102); x.add(carl); for(int i=0; i<x.size() ; i++) { System.out.println(x.get(i).toString() ); } System.out.println("IsEmpty: " + x.isEmpty()); // should print "false" Person dave = new Person("Dave", 19, 103); x.add(1, dave); for(int i=0; i<x.size() ; i++) { System.out.println(x.get(i).toString() ); } System.out.println("Contains dave: " + x.contains(dave)); // should print "true" Person ed = new Person("Ed", 19, 104); System.out.println("Contains ed: " + x.contains(ed)); // should print "false" x.remove(2); // remove bob for(int i=0; i<x.size() ; i++) { System.out.println(x.get(i).toString() ); } System.out.println("Size="+ x.size() ); x.clear(); System.out.println("IsEmpty: " + x.isEmpty()); // should print "true" System.out.println("Size="+ x.size() ); } }
The test program should generate the output shown below:
IsEmpty: true Ann, age=18, id=100 Bob, age=19, id=101 Carl, age=18, id=102 IsEmpty: false Ann, age=18, id=100 Dave, age=19, id=103 Bob, age=19, id=101 Carl, age=18, id=102 Contains dave: true Contains ed: false Ann, age=18, id=100 Dave, age=19, id=103 Carl, age=18, id=102 Size=3 IsEmpty: true Size=0