Question: In the third lab, you get to practice using class inheritance to create your own custom subclass versions of existing classes in Java with new

In the third lab, you get to practice using class inheritance to create your own custom subclass versions of existing classes in Java with new functionality that did not exist in the original superclass. Your third task in this course is to use inheritance to create your own custom subclass AccessCountArrayList that extends the good old ArrayList from the Java Collection Framework. This subclass should keep an integer count of how many times the methods get and set have been called. (One counter keeps the simultaneous count for both methods.)

You should override the get and set methods so that both of them first increment the access count and then call the superclass version of that same method (use the prefix super in the method call to force this), returning whatever result the superclass version returned. In addition to these overridden methods inherited from the superclass, your class should define the following two brand new methods:

public int getAccessCount()

Returns the current count of how many times the get and set methods have been called for this object.

public void resetCount()

Resets the access count field of this object back to zero.

Test Code:

import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Random; import java.util.*; import java.util.zip.CRC32; public strictfp class AccessCountArrayListTest { private static int TRIALS = 10000; private static int SEED = 87654; private void updateCheck(double x, CRC32 check) { long y = Double.doubleToRawLongBits(x); check.update((int)(y & 0x0000FFFF)); check.update((int)(y >> 32)); } @Test public void massTest() { AccessCountArrayList acad = new AccessCountArrayList(); Random rng = new Random(SEED); CRC32 check = new CRC32(); int idx; for(int i = 0; i < TRIALS; i++) { acad.clear(); acad.resetCount(); int len = rng.nextInt(1000) + 50; for(int j = 0; j < len; j++) { acad.add(rng.nextDouble()); } for(int j = 0; j < len; j++) { if(rng.nextBoolean()) { idx = rng.nextInt(len); updateCheck(acad.set(idx, rng.nextDouble()), check); } if(rng.nextBoolean()) { idx = rng.nextInt(len); updateCheck(acad.get(idx), check); } check.update(acad.getAccessCount()); } } assertEquals(3163640888L, check.getValue()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!