Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

L8 GenericArrayList.java (Main class in zyBooks) GenericArrayListPt2.java Point.java (Read-only on zyBooks) Point3D.java (Read-only on zyBooks) GenericArrayList For the first part of this lab, copy your

L8 GenericArrayList.java(Main class in zyBooks) GenericArrayListPt2.java Point.java (Read-only on zyBooks) Point3D.java (Read-only on zyBooks)

GenericArrayList

For thefirst partof this lab, copy your working ArrayStringList code into the GenericArrayList class. Then, modify the class so that it can storeanytype 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 thesecond partof 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 theonlyclasses 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:

  1. Why can't you write something like the following in GenericArrayListPt2? GenericArrayList floatList = new GenericArrayList(2);
  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?

GenericArrayList.java:

import java.util.List; import java.util.ArrayList;

public class GenericArrayList { /* YOUR CODE HERE * Copy your code from your ArrayStringList class, and place it within * this class. * */

// Place code here

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! */

/* Uncomment me when you're done GenericArrayList pointList = new GenericArrayList(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 floatList = new GenericArrayList(2);

for (float f = 0.0f; f floatList.add(f); }

float f = floatList.get(19);

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

} }

********************* GenericArrayListPt2.Java:

import java.util.List; import java.util.ArrayList;

public class GenericArrayListPt2 { /* YOUR CODE HERE * Copy your code from your ArrayStringList class, and place it within * this class. * */ // Place code here

public static void main(String[] args) {

/* 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. */

/* GenericArrayListPt2 pointList = new GenericArrayListPt2(2); GenericArrayListPt2 pointList3D = new GenericArrayListPt2(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! - Why? GenericArrayListPt2 floatList = new GenericArrayList(2); */

} }

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

How do extended families differ from nuclear families?

Answered: 1 week ago