Answered step by step
Verified Expert Solution
Question
1 Approved Answer
5 . 1 2 Lab 9 - GenericArrayList Module 4 : Lab 9 - GenericArrayList How Generic! This lab requires you have a working ArrayStringList
Lab GenericArrayList
Module : Lab GenericArrayList
How Generic!
This lab requires you have a working ArrayStringList from the last lab. Finish that lab before you tackle this one.
Download the lab materials here:
Ljar.
The following java files are included in this lab:
L
GenericArrayList.java Main class in zyBooks
GenericArrayListPtjava
Point.java Readonly on zyBooks
PointDjava Readonly on zyBooks
In Lab we made a simple ArrayList that only works with Strings. But what if we want to store something else, like Integers, or arrays, or our own custom classes? The solution is generics!
In this lab, you will be giving your old ArrayStringList a type parameter, so that anyone who makes an object of that class can choose what type they want to store. This is what Java's standard ArrayList does whenever you type something like ArrayList, you are basically asking Java to create a version of ArrayList that only works for Strings. The ArrayList class, then, becomes a sort of template for other, more specific classes.
Using the code
You'll notice two unfamiliar classes in the jar file we provide; Point and PointD These create a simple inheritance hierarchy. You'll also notice both implement the Comparable interface, which is to say they can both be compared to a Point to get some ordering.
You won't be writing code in either of these classes, and the implementations of their methods isn't very important. The important thing to understand for now is how they relate to one another.
You will be working in the GenericArrayList and GenericArrayListPt classes for this lab. Follow the instructions below.
GenericArrayList
For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class. 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 ie 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 Tcapacity;
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 newData T new Objectcapacity;
This creates an array of regular Objects which are then cast to the generic type. It works, and it doesn't create an error in the Java compiler. How amazing!
You will likely still get warnings depending on how you implement this, however. See question # below. You will want to know what these warnings mean.
GenericArrayListPt
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 PointD 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. Note: only the main in GenericArrayList will run in zyBooks.
Questions to think about:
Why can't you write something like the following in GenericArrayListPt
GenericArrayList floatList new GenericArrayList;
Why might there be unchecked and raw type warnings when you run your code? What do these warnings tell you and why is it important to pay attention to them?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started