Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

there are test code below. // DO NOT MODIFY THIS FILE. public class FancyStringArrayTest { public static void main(String[] args) { testNoArgConstructor(); testParameterizedConstructor(); testSize(); testAdd();

image text in transcribed

there are test code below.

// DO NOT MODIFY THIS FILE. public class FancyStringArrayTest { public static void main(String[] args) { testNoArgConstructor(); testParameterizedConstructor(); testSize(); testAdd(); testGet(); testSet(); testContains(); testToString(); testEquals(); } private static void testNoArgConstructor() { System.out.println("Testing no-arg constructor"); new FancyStringArray(); System.out.println(); } private static void testParameterizedConstructor() { System.out.println("Testing parameterized constructor"); try { new FancyStringArray(0); new FancyStringArray(1); new FancyStringArray(10); new FancyStringArray(100); } catch (Exception e) { System.out.println("an exception should not have occurred here, but did"); } try { new FancyStringArray(-1); System.out.println("an exception should have occurred here, but didn't"); } catch (Exception ignored) { } System.out.println(); } private static void testSize() { System.out.println("Testing size"); FancyStringArray a = new FancyStringArray(); System.out.println(a.size()); // expected: 0 a.add("a string"); System.out.println(a.size()); // expected: 1 a.add("another string"); System.out.println(a.size()); // expected: 2 FancyStringArray b = new FancyStringArray(5); System.out.println(b.size()); // expected: 0 b.add("a string"); System.out.println(b.size()); // expected: 1 b.add("another string"); System.out.println(b.size()); // expected: 2 System.out.println(); } private static void testAdd() { System.out.println("Testing add"); FancyStringArray a = new FancyStringArray(); try { for (int i = 0; i Write a class named FancyStringArray. An object of this class should be designed to store some Strings. Internally, these Strings should be stored in an array. When creating a FancyStringArray, the user should be able to specify the capacity, which is the maximum number of elements that the FancyStringArray can hold. If the user doesn't specify the capacity, then the FancyStringArray should have a capacity of 10. Separate from the notion of capacity, there is also a notion of size. The size of a FancyStringArray is the number of elements that it is currently storing. When adding an element to a FancyStringArray, the element should be placed in the array at the next available index. For example, if we have a FancyStringArray with size 3 , the elements shou Id be stored at indexes 0 through 2 . When we add a fourth element, it should be placed at index 3. The class should have the following constructors: - public FancyStringArray() - public FancyStringArray(int capacity) The class should have the following methods: - public int size() - public void add(5tring element) - public string get (int index) // returns the element at the specified index - public String set(int index. String newElement) // replaces the old element at the specified index with the new element, and returns the old element - public boolean contains(String s)// determines whether this FancyStringArray contains s - A tostring method that returns a string representation of this FancyStringArray. The string representation should consist of a list of the collection's elements in order, enclosed in square brackets ("[" and ']"). Adjacent elements should be separated by the characters ", " (comma and space). For example, "[]" (for an empty FancyStringArray) or "[element 1, element 2, element 3]". - An equals method. Two FancyStringArrays should be considered equal if and only if they currently store the same sequence of elements in the same order. Note that the capacity is not relevant here: two FancyStringArrays can be considered equal even if they have different capacities. Throwing exceptions: - The second constructor should throw an IllegalArgumentException if the provided capacity is negative. - The add method should throw an IllegalStateException if the array is full. - The get and set methods should throw an IndexOutOfBoundsException if the provided index is negative, or if it is greater than or equal to the size. Note: do not use ArrayList (or any other kind of List or Collection) in the FancyStringArray class. Paste your solution in FancyStringArray.java, below. The other file, FancyStringArrayTest.java, contains code to test your solution. You may copy the testing code, but do not modify it. Additional Notes: Regarding your code's standard output, CodeLab will check for case errors but will ignore whitespace (tabs, spaces, newlines) altogether

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions