Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Students also viewed these Databases questions

Question

1. Who should participate and how will participants be recruited?

Answered: 1 week ago

Question

3. How would this philosophy fit in your organization?

Answered: 1 week ago

Question

How would you assess the value of an approach like this?

Answered: 1 week ago