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
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() { AccessCountArrayListacad = 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started