C211 Problem Solving and Programming II

C211 Lab 9

Due Date: Monday, December 4, 2023.

Ex. 1. Using Generic Classes

In this lab, we will implement a project that uses generic classes.

a. Generic Class

For this lab, you can use either Eclipse or NetBeans. Create a project called Lab7 and a package inside called lab7.

Create a generic class called MyArray depending on one type parameter T. In the class, declare an attribute store containing an object of type ArrayList containing elements of type T. Write a constructor for this class that takes an array of type T and makes a copy of it in the attribute.

In this class, declare a method called distance calculating the Hamiltonian distance between two such objects and returning an integer. The first should be the target object this, and the second one a parameter called other. The function should first initialize the distance with the difference between the sizes of the two arrays. Then it should go through the arrays and increment the distance for each pair of elements at the same index in the two arrays that are different.

Write another method called sample that returns a random element from the array stored in the object. The method should return an element of type T and not take any parameters. The function should generate a random value between 0 and the size of the array minus 1, then return the element at that index.

b. Test Class

Declare a class called MyArrayTest, containing a main function. In the main, create an array of String objects called words containing 10 words of your choosing. Then declare an object of type MyArray called wordsArray and assign to it a new object of this class with this array as a parameter. Declare a simple String object called firstWord and assign to it the result of calling the method sample on the object wordsArray.

c. Word Game

We will use the generic class to implement a word game. The user receives a word, and must find as many other words as possible that are at a Hamiltonian distance of 1 from the first one. This means that it must differ from it by one letter only, or have one less or one more letter at the end of the word.

For this, continuing in the main, create another object of type MyArray called first, and assign to it an object created from converting the string firstWord to a Character array. For this, you can use the following syntax:

Character[] firstArray = firstWord.chars().mapToObj(ch -> (char) ch)
                .toArray(Character[]::new);
MyArray first = new MyArray(firstArray);

Also add an integer counter, initialized as 0, and an integer dist initialized as 1.

Then declare a second String object called secondWord. Add a while loop where you keep going while the distance is equal to 1. In the loop, input the second word from the user, convert it to a MyArray object called second the same way as the first one, and then call the function distance with the two objects first and second and assign the result to the variable dist.

After the loop, output the score for the user.

Lab Submission

Upload the files MyArray.java and MyArrayTest.java to Canvas in Assignments - Lab 9.