Implement a class CountingArrayList that extends ArrayList and that counts the number of calls to get and set.   import java.util.ArrayList; import java.util.Collections; public class CountingArrayListTester {    public static String smallest(ArrayList values)    {       String smallestSoFar = values.get(0);       for (int i = 1; i < values.size(); i++)       {          String value = values.get(i);          if (value.compareTo(smallestSoFar) < 0)          {             smallestSoFar = value;          }       }       return smallestSoFar;    }       public static void main(String[] args)    {       CountingArrayList words = new CountingArrayList();       words.add("Mary");       words.add("had");       words.add("a");       words.add("little");       words.add("lamb");       words.add("its");       words.add("fleece");       words.add("was");       words.add("white");       words.add("as");       words.add("snow");       String smallest = smallest(words);       System.out.println(words.count("get"));       System.out.println("Expected: 11");       System.out.println(words.count("set"));       System.out.println("Expected: 0\n");              CountingArrayListTester2 tester2 = new CountingArrayListTester2();       tester2.main(null);    } } import java.util.ArrayList; public class CountingArrayListTester2 {    public static void reverse(ArrayList words)    {       for (int i = 0; i < words.size() / 2; i++)       {          String oldValue = words.set(words.size() - i - 1,             words.get(i));          words.set(i, oldValue);       }    }    public static void main(String[] args)    {       CountingArrayList words = new CountingArrayList();       words.add("Mary");       words.add("had");       words.add("a");       words.add("little");       words.add("lamb");       words.add("its");       words.add("fleece");       words.add("was");       words.add("white");       words.add("as");       words.add("snow");       reverse(words);       System.out.println(words.count("get"));       System.out.println("Expected: 5");       System.out.println(words.count("set"));       System.out.println("Expected: 10");             System.out.println(words);       System.out.println("Expected: [snow, as, white, was, "          + "fleece, its, lamb, little, a, had, Mary]");    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Implement a class CountingArrayList that extends ArrayList<String> and that counts the number of calls to get and set.

 

import java.util.ArrayList;
import java.util.Collections;

public class CountingArrayListTester
{
   public static String smallest(ArrayList<String> values)
   {
      String smallestSoFar = values.get(0);
      for (int i = 1; i < values.size(); i++)
      {
         String value = values.get(i);
         if (value.compareTo(smallestSoFar) < 0)
         {
            smallestSoFar = value;
         }
      }
      return smallestSoFar;
   }   

   public static void main(String[] args)
   {
      CountingArrayList words = new CountingArrayList();
      words.add("Mary");
      words.add("had");
      words.add("a");
      words.add("little");
      words.add("lamb");
      words.add("its");
      words.add("fleece");
      words.add("was");
      words.add("white");
      words.add("as");
      words.add("snow");

      String smallest = smallest(words);
      System.out.println(words.count("get"));
      System.out.println("Expected: 11");
      System.out.println(words.count("set"));
      System.out.println("Expected: 0\n");
      
      CountingArrayListTester2 tester2 = new CountingArrayListTester2();
      tester2.main(null);
   }
}

import java.util.ArrayList;

public class CountingArrayListTester2
{
   public static void reverse(ArrayList<String> words)
   {
      for (int i = 0; i < words.size() / 2; i++)
      {
         String oldValue = words.set(words.size() - i - 1,
            words.get(i));
         words.set(i, oldValue);
      }
   }

   public static void main(String[] args)
   {
      CountingArrayList words = new CountingArrayList();
      words.add("Mary");
      words.add("had");
      words.add("a");
      words.add("little");
      words.add("lamb");
      words.add("its");
      words.add("fleece");
      words.add("was");
      words.add("white");
      words.add("as");
      words.add("snow");

      reverse(words);
      System.out.println(words.count("get"));
      System.out.println("Expected: 5");
      System.out.println(words.count("set"));
      System.out.println("Expected: 10");      
      System.out.println(words);
      System.out.println("Expected: [snow, as, white, was, "
         + "fleece, its, lamb, little, a, had, Mary]");
   }
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education