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?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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<String>, 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<Point> 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<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. (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<Float> floatList = new GenericArrayList<Float>(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?

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education