Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

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

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

/* Uncomment me! 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); */

} }

Point.java

/* This is an example class for use with your generic ArrayList. * You don't have to modify anything in this class. */ public class Point implements Comparable { public float x, y;

public Point(float x, float y) { this.x = x; this.y = y; }

@Override public boolean equals(Object o) { return (o instanceof Point) && x == ((Point)o).x && y == ((Point)o).y; } @Override public int compareTo(Point p) { return Math.round(x - p.x); } public int hashCode(){ return super.hashCode(); } }

Point3D.java

/* Another example class. * Again, you don't need to modify this. */ public class Point3D extends Point { public float z;

public Point3D(float x, float y, float z) { super(x, y); this.z = z; }

@Override public boolean equals(Object o) { return (o instanceof Point3D) && x == ((Point3D)o).x && y == ((Point3D)o).y && z == ((Point3D)o).z; } public int hashCode(){ return super.hashCode(); } }

Lab 7 Code

ArrayStringList.java

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Main.java

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

public class Main {

public static void main(String[] args) { ArrayStringList list = new ArrayStringList(2); ArrayList referenceList = new ArrayList();

list.add("alpha"); referenceList.add("alpha"); list.add("beta"); referenceList.add("beta"); list.add("gamma"); referenceList.add("gamma");

// You may ask why I didn't just use JUnit instead of these honestly // ugly if statements. // The only reason is I didn't want to force people to set up JUnit for // what should be a fairly simple lab.

if (list.size() != referenceList.size()) { throw new AssertionError(String.format("Your size method produced " + "the wrong results. It should be been %d, but it was %d.", referenceList.size(), list.size())); }

for (int i = 0; i

list.remove(1); referenceList.remove(1);

if (list.size() != referenceList.size()) { throw new AssertionError(String.format("After removing an item, " + "the size of your list should be %d, but it was %d. Your " + "remove method may not properly be updating the list's size.", referenceList.size(), list.size())); }

for (int i = 0; i

if (!list.contains("alpha")) { throw new AssertionError(String.format("Your contains method " + "reported a string doesn't exist in the List, even though " + "it should.")); }

if (list.contains("beta")) { throw new AssertionError(String.format("Your contains method " + "reported a string DID exist in the List, even though it " + "should have been removed.")); }

// Fun fact: Java strings (and chars, for that matter) are // Unicode-compatible!

list.add("delta"); referenceList.add("delta"); list.add("epsilon"); referenceList.add("epsilon"); list.add("zeta"); referenceList.add("zeta"); list.add("eta"); referenceList.add("eta"); list.add("theta"); referenceList.add("theta"); list.add("iota"); referenceList.add("iota"); list.add("kappa"); referenceList.add("kappa"); list.add("lambda"); referenceList.add("lambda"); list.add("mu"); referenceList.add("mu");

if (list.size() != referenceList.size()) { throw new AssertionError(String.format("After adding a bunch of " + "new elements, the size of your list should have been %d " + "but it was %d", referenceList.size(), list.size())); }

for (int i = 0; i

System.out.println("If you're reading this, everything worked!"); } }

4.11 Lab 8 - GenericArrayList Module 4: Lab 8 - 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) FPoint3D.java (Read-only on zy Books) In Lab 7, we made a simple ArrayList that only works 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: 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? Current file: ArrayStringList.java Load default template. 1 import java.util.List; 2 import java.util.ArrayList; 3 4 * * 5 public class ArrayStringList 6 7 /* This field is really important! 8 * This is the internal array of data you're going to use to implement the 9 * ArrayStringList. This is what actually STORES the strings in your list. 10 * More information about how this should be used is in the lab writeup. 11 * Read it first! 12 */ 13 private String[] data; 14 15 /* Storing the amount of valid Strings that are in the array turns out to be 16 * fairly useful. This variable is for that. 17 */ 18 private int size; 19 20 * /* YOUR CODE HERE 21 * From this point onward, you're going to see a bunch of empty methods. It 22 * is your job to fill them out (or in fancy cs terminology, "implement" 23 * them), so they do what the comments say they should. 24 25 * Remember, the comments are not telling you how to implement a method. 26 * They are telling you WHAT the method should do. The HOW of each of these 27 * methods is going to be a combination of your own creativity and the 28 * guidelines laid out in the lab writeup. 29 */ 30 31 /* This method is mostly here for your own benefit. You may be resizing the 32 * array in several places (in both of the add methods, for instance), and 33 * whenever we are doing the same thing in multiple places, it's usually a 34 * good idea to put it into a function, so it can be easily reused. 35 36 * This method should change the size of that data array to whatever the 37 * newsize is. It should keep the original data intact as well. I recommend 38 * you start by creating a totally new String[] array of the desired size, 39 * then copying over the elements from the data array to this new array, 40 * then when that is done, replacing the data array with the new one. 41 42 private void resizeData(int newsize) { 43 String[] strings = new String[newsize]; 44 // copy the data into new array 45 for (int i = 0; i size++; } } } /* This method should add a string to a specific index in your ArrayList. * The index may not be valid. For instance, calling this with an index of * 10 on an ArrayList that only has 7 elements is not allowed. * If the index is out of bounds, stop the method without doing anything. */ public void add(int index, string str) { if(index=0) { data[index] = str; size++; } } } /* This method should return the string stored at a certain index. * Like the method above, the index may not be valid. Return null if the * index given is out of bounds. */ public string get(int index) { if(index=0) return data[index]; return null; } } 1* This method should take the string at a given index out of your * ArrayList. * If the index isn't valid, then stop the method without doing anything. */ public void remove(int index) { if(index=0) { for (int i = index; i 0; list.add("alpha"); referenceList.add("alpha"); list.add("beta"); referenceList.add("beta"); list.add("gamma"); referenceList.add("gamma"); ...I Emma 161 162 // You may ask why I didn't just use JUnit instead of these honestly 163 // ugly if statements. 164 // The only reason is I didn't want to force people to set up Junit for 165 // what should be a fairly simple lab. 166 167 if (list.size() != referenceList.size() { 168 throw new AssertionError(String.format("Your size method produced 169 + "the wrong results. It should be been d, but it was xd.", 178 referenceList.size(), list.size()); 171 } 172 173 for (int i = 0; i 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: 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? Current file: ArrayStringList.java Load default template. 1 import java.util.List; 2 import java.util.ArrayList; 3 4 * * 5 public class ArrayStringList 6 7 /* This field is really important! 8 * This is the internal array of data you're going to use to implement the 9 * ArrayStringList. This is what actually STORES the strings in your list. 10 * More information about how this should be used is in the lab writeup. 11 * Read it first! 12 */ 13 private String[] data; 14 15 /* Storing the amount of valid Strings that are in the array turns out to be 16 * fairly useful. This variable is for that. 17 */ 18 private int size; 19 20 * /* YOUR CODE HERE 21 * From this point onward, you're going to see a bunch of empty methods. It 22 * is your job to fill them out (or in fancy cs terminology, "implement" 23 * them), so they do what the comments say they should. 24 25 * Remember, the comments are not telling you how to implement a method. 26 * They are telling you WHAT the method should do. The HOW of each of these 27 * methods is going to be a combination of your own creativity and the 28 * guidelines laid out in the lab writeup. 29 */ 30 31 /* This method is mostly here for your own benefit. You may be resizing the 32 * array in several places (in both of the add methods, for instance), and 33 * whenever we are doing the same thing in multiple places, it's usually a 34 * good idea to put it into a function, so it can be easily reused. 35 36 * This method should change the size of that data array to whatever the 37 * newsize is. It should keep the original data intact as well. I recommend 38 * you start by creating a totally new String[] array of the desired size, 39 * then copying over the elements from the data array to this new array, 40 * then when that is done, replacing the data array with the new one. 41 42 private void resizeData(int newsize) { 43 String[] strings = new String[newsize]; 44 // copy the data into new array 45 for (int i = 0; i size++; } } } /* This method should add a string to a specific index in your ArrayList. * The index may not be valid. For instance, calling this with an index of * 10 on an ArrayList that only has 7 elements is not allowed. * If the index is out of bounds, stop the method without doing anything. */ public void add(int index, string str) { if(index=0) { data[index] = str; size++; } } } /* This method should return the string stored at a certain index. * Like the method above, the index may not be valid. Return null if the * index given is out of bounds. */ public string get(int index) { if(index=0) return data[index]; return null; } } 1* This method should take the string at a given index out of your * ArrayList. * If the index isn't valid, then stop the method without doing anything. */ public void remove(int index) { if(index=0) { for (int i = index; i 0; list.add("alpha"); referenceList.add("alpha"); list.add("beta"); referenceList.add("beta"); list.add("gamma"); referenceList.add("gamma"); ...I Emma 161 162 // You may ask why I didn't just use JUnit instead of these honestly 163 // ugly if statements. 164 // The only reason is I didn't want to force people to set up Junit for 165 // what should be a fairly simple lab. 166 167 if (list.size() != referenceList.size() { 168 throw new AssertionError(String.format("Your size method produced 169 + "the wrong results. It should be been d, but it was xd.", 178 referenceList.size(), list.size()); 171 } 172 173 for (int i = 0; i

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

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

=+2 How does the preparation and support for each type of IE vary?

Answered: 1 week ago

Question

=+What is the extent of the use of each type of IE?

Answered: 1 week ago