For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already in the code) Then, modify the class so that it can store any type someone asks for, instead of only Strings. You shouldn't have to change any of the actual logic in your class to accomplish this, only type declarations (i.e. the types of parameters, return types, etc.) Note: In doing so, you may end up needing to write something like this (where T is a generic type): T[] newData = new T[capacity]; ...and you will find this causes a compiler error. This is because Java dislikes creating new objects of a generic type. In order to get around this error, you can write the line like this instead: T[] new Data = (T[]) new Object[capacity] This creates an array of regular Objects which are then cast to the generic type. It works and it doesn't anger the Java compiler. How amazing! Once you're done, screenshot or save your code for checkin later. For the second part of the lab, modify your GenericArrayList so that it can store any type that is comparable to a Point. Remember the Point and Point3D classes? Both of those implement the Comparable interface, so they both can compared to a Point. In fact, they are the only classes that can be compared to a Point, so after modifying your GenericArrayList, it should only be able to contain these two classes. In both parts, test your classes by following the directions in the comments. They will ask you to uncomment some code and look for a specific result.     public class GenericArrayList { /* YOUR CODE HERE    * Copy your code from your ArrayStringList class, and place it within    * this class.    *    * Only copy the code you filled out! Don't copy the main method.    */      // Place code here public class ArrayStringList { private String[] data; private int size;     private void resizeData(int newSize) { String[] str = new String[newSize]; for(int i = 0; i < size; i++) { str[i] = data[i]; } data=str; }       public ArrayStringList(int initialCapacity) { data = new String[initialCapacity]; size = 0; }   public void add(String str) { if(size < data.length) { data[size] = str; size++; } else { resizeData(2 * data.length); data[size] = str; size++; }   }   public void add(int index, String str) { if(index < data.length && index >= 0) { data[index] = str; size++; }   } public String get(int index) { if(index < data.length && index >= 0) { return data[index]; } return null; } public void remove(int index) { if(index < data.length && index >= 0) { for(int i = index; i < data.length; i++) { if((i + 1) < size) { data[i] = data[i + 1]; } } size--; }   }     public int size() { return size; }   public boolean contains(String str) { for(int i = 0; i < data.length; i++) { if(str.equals(data[i])) { return true; } } return false; }               public static void main(String[] args) { /* PART 1:    * Modify the GenericArrayList above so that it can store *any* class,    * not just strings.    * When you've done that, uncomment the block of code below, and see if    * it compiles. If it does, run it. If there are no errors, you did    * it right!    */     GenericArrayList pointList = new GenericArrayList<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already in the code) Then, modify the class so that it can store any type someone asks for, instead of only Strings. You shouldn't have to change any of the actual logic in your class to accomplish this, only type declarations (i.e. the types of parameters, return types, etc.)

Note:

In doing so, you may end up needing to write something like this (where T is a generic type):

T[] newData = new T[capacity];

...and you will find this causes a compiler error. This is because Java dislikes creating new objects of a generic type. In order to get around this error, you can write the line like this instead:

T[] new Data = (T[]) new Object[capacity]

This creates an array of regular Objects which are then cast to the generic type. It works and it doesn't anger the Java compiler. How amazing!

Once you're done, screenshot or save your code for checkin later.

For the second part of the lab, modify your GenericArrayList so that it can store any type that is comparable to a Point. Remember the Point and Point3D classes? Both of those implement the Comparable<Point> interface, so they both can compared to a Point. In fact, they are the only classes that can be compared to a Point, so after modifying your GenericArrayList, it should only be able to contain these two classes.

In both parts, test your classes by following the directions in the comments. They will ask you to uncomment some code and look for a specific result.

 

 

public class GenericArrayList {

/* YOUR CODE HERE

   * Copy your code from your ArrayStringList class, and place it within

   * this class.

   *

   * Only copy the code you filled out! Don't copy the main method.

   */

 

   // Place code here

public class ArrayStringList {

private String[] data;

private int size;

 

 

private void resizeData(int newSize) {

String[] str = new String[newSize];

for(int i = 0; i < size; i++) {

str[i] = data[i];

}

data=str;

}

 

 

 

public ArrayStringList(int initialCapacity) {

data = new String[initialCapacity];

size = 0;

}

 

public void add(String str) {

if(size < data.length) {

data[size] = str;

size++;

} else {

resizeData(2 * data.length);

data[size] = str;

size++;

}

 

}

 

public void add(int index, String str) {

if(index < data.length && index >= 0) {

data[index] = str;

size++;

}

 

}

public String get(int index) {

if(index < data.length && index >= 0) {

return data[index];

}

return null;

}

public void remove(int index) {

if(index < data.length && index >= 0) {

for(int i = index; i < data.length; i++) {

if((i + 1) < size) {

data[i] = data[i + 1];

}

}

size--;

}

 

}

 

 

public int size() {

return size;

}

 

public boolean contains(String str) {

for(int i = 0; i < data.length; i++) {

if(str.equals(data[i])) {

return true;

}

}

return false;

}

 

 

 

 

 

 

 

public static void main(String[] args) {

/* PART 1:

   * Modify the GenericArrayList above so that it can store *any* class,

   * not just strings.

   * When you've done that, uncomment the block of code below, and see if

   * it compiles. If it does, run it. If there are no errors, you did

   * it right!

   */

 

 

GenericArrayList<Point> pointList = new GenericArrayList<Point>(2);

 

pointList.add(new Point(0, 0));

pointList.add(new Point(2, 2));

pointList.add(new Point(7, 0));

pointList.add(new Point(19.16f, 22.32f));

 

pointList.remove(0);

Point p = pointList.get(2);

 

if (p.x != 19.16f && p.y != 22.32f) {

throw new AssertionError("Your GenericArrayList compiled properly "

+ "but is not correctly storing things. Make sure you didn't "

+ "accidentally change any of your ArrayStringList code, aside "

+ "from changing types.");

}

 

GenericArrayList<Float> floatList = new GenericArrayList<Float>(2);

 

for (float f = 0.0f; f < 100.0f; f += 4.3f) {

floatList.add(f);

}

 

float f = floatList.get(19);

 

System.out.println("Hurray, everything worked!");

  

 

 

/* PART 2:

   * Now, modify your GenericArrayList again so that it can only store

   * things that are comparable to a Point.

   *

   * If you don't know how to do this, reference zybooks and your textbook

   * for help.

   *

   * When you are ready to test it, uncomment the code above and run the

   * code below.

   */

 

/*

GenericArrayList<Point> pointList = new GenericArrayList<Point>(2);

GenericArrayList<Point3D> pointList3D = new GenericArrayList<Point3D>(3);

 

pointList.add(new Point(0, 0));

pointList.add(new Point(2, 2));

pointList.add(new Point(7, 0));

pointList.add(new Point(19.16f, 22.32f));

 

pointList3D.add(new Point3D(1.0f, 2.0f, 3.0f));

pointList3D.add(new Point3D(7.3f, 4, 0));

 

Point p = pointList.get(2);

Point3D p3 = pointList3D.get(0);

 

// You should get a compilation error on this line!

GenericArrayList<Float> floatList = new GenericArrayList<Float>(2);

*/

}

 

}

}

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY