Answered step by step
Verified Expert Solution
Link Copied!

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

5.12 Lab 9- GenericArrayList
Module 4: Lab 9- 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:
L8.jar.
The following .java files are included in this lab:
L8
GenericArrayList.java (Main class in zyBooks)
GenericArrayListPt2.java
Point.java (Read-only on zyBooks)
Point3D.java (Read-only on zyBooks)
In Lab 7, 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 Point3D. 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 GenericArrayListPt2 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 (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[] newData =(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 create an error in the Java compiler. How amazing!
You will likely still get warnings depending on how you implement this, however. See question #2 below. You will want to know what these warnings mean.
GenericArrayListPt2
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. (Note: only the main in GenericArrayList will run in zyBooks.)
Questions to think about:
Why can't you write something like the following in GenericArrayListPt2?
GenericArrayList floatList = new GenericArrayList(2);
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?
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Why is trust so important to the customerprovider relationship?

Answered: 1 week ago