Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your assignment is to create a class called CharList in a file called CharList.j

ID: 3698932 • Letter: Y

Question

Your assignment is to create a class called CharList in a file called CharList.java. (there is no main method in this class). The class CharList has two instance variables, an array of characters and an integer variable, which keeps track of the number of characters in the array. Note: the number of elements is different from the size of the array.

The class CharList must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.)

Method

Description of the Method

The constructor instantiates an array of characters using the given size.

Fills array with random characters between ‘a’ to ‘z’

Adds a character (newChar) to the arry at the specified index and shifts all the elements (after passed index) to the right by one index. If the array is full, its size will be doubled first.

This method is private and it doubles the size of the array.

Removes the first occurrence of newChar from the array and shifts all the elements (after the removed one) to the left by one index.

this method returns an array of twenty-six int values, each of which stores the number of occurrence of each letter contained in the "charList". For example, the first element in the array stores the number of occurrences for letter 'a'.

Displays the array of characters. You should print 10 characters per line.

Save the CharList class in a file called CharList.java and use the following program stored in Assignment7.java, which has the main method to test your class.

The main function prints the menu:

Menu

----------------------------
a: Create a new list (** do this first!! **)
b: Print the list of characters
c: Add a character to the list at specified index d: Remove a character from the list
e: Count the number of letters in the list
q. Quit

Here is the description for each option:

a: Creates a new list with specific size from the user and fill the list with random cahracters between ‘a’ to ‘z’ lowercase
b: It prints the characters using the toString method
c: Adds a new character to the list by asking the user for the character and the index where the character will be added

d: It deletes the first occurrence of the character entered by the user
e: It counts the number of occurrence of each letter ‘a’ to ‘z’ in the array q: quit

Note that you will NOT be removing or modifying the existing codes in the Assignment7.java. After compiling CharList.java file and Assignment7.java file, you need to execute Assignment7 class.

Menu ---------------

a: Create a new list (** do this first!! **) b: Print the list of characters
c: Add a character to the list
d: Remove a character from the list

e: Count the number of letters in the list q: Quit

Please enter a command or type? a Enter an integer for the array size? 10

Please enter a command or type? b bmbudokosm

Please enter a command or type? c
Enter the new character to add to the list: i
Enter the index where you want to add the new character: 0 Please enter a command or type? c
Enter the new character to add to the list: t
Enter the index where you want to add the new character: 1 Please enter a command or type? d

Enter the character to delete: b Please enter a command or type? b

itmbudokosm

Please enter a command or type? d

Enter the character to delete: t

Please enter a command or type? b i mb u d o ko s m

Please enter a command or type? e a0
b1
c0

d1 e0 f0 g0 h0 i1 j0 k1 l0 m2 n0 o2 p0 q0 r0 s1 t0 u1 v0 w0 x0 y0 z0

Please enter a command or type? q Press any key to continue . . .

Helpful hints for doing this assignment:

Work on it in steps – write one method, test it with a test driver and make sure it works before going on to the next method

Always make sure your code compiles before you add another method

Your methods should be able to be called in any order

Method

Description of the Method

  public CharList (int size)  

The constructor instantiates an array of characters using the given size.

  public void randomize ()  

Fills array with random characters between ‘a’ to ‘z’

  public void addChar (char newChar,  int index)  

Adds a character (newChar) to the arry at the specified index and shifts all the elements (after passed index) to the right by one index. If the array is full, its size will be doubled first.

  private void increaseSize()  

This method is private and it doubles the size of the array.

  public void removeFirst (int newChar)  

Removes the first occurrence of newChar from the array and shifts all the elements (after the removed one) to the left by one index.

  public int[] countLetters()  

this method returns an array of twenty-six int values, each of which stores the number of occurrence of each letter contained in the "charList". For example, the first element in the array stores the number of occurrences for letter 'a'.

  public String toString()  

Displays the array of characters. You should print 10 characters per line.

Explanation / Answer

Please find my implementation:

##############

import java.util.Arrays;

import java.util.Random;

public class CharList {

   //attributes

   private char[] array;

   private int numChars;

   //constructor to initialize the array

   public CharList(int size) {

       array = new char[size];

       numChars = 0;

   }

   /**

   * method to generate random values to the array

   */

   public void randomize() {

       numChars = 0;

       Random random = new Random();

       for (int i = 0; i < array.length; i++) {

           //generating a random char between a and z

           char randomChar = (char) ('a' + random.nextInt(26));

           array[i] = randomChar;

           numChars++;

       }

   }

   /**

   * method to add a character to a specified array index

   */

   public void addChar(char newChar, int index) {

       if (index < 0 || index > numChars) {

           System.out.println("Invalid index");

       } else {

           if (numChars == array.length) {

               increaseSize();

           }

           numChars++;

           //shifting elements to the right

           for (int i = numChars - 1; i > index; i--) {

               array[i] = array[i - 1];

           }

           array[index] = newChar;

           System.out.println(newChar + " is added to index " + index);

       }

   }

   /**

   * method to double the array size

   */

   private void increaseSize() {

       array = Arrays.copyOf(array, array.length * 2);

   }

   /**

   * method to remove the first occurrence of a char

   */

   public void removeFirst(char c) {

       boolean found = false;

       for (int i = 0; i < numChars; i++) {

           if (array[i] == c) {

               found = true;

               //shifting remaining elements to the left

               for (int j = i; j < numChars - 1; j++) {

                   array[j] = array[j + 1];

               }

               System.out.println(c + " is removed from index " + i);

               numChars--;

               break;

           }

       }

       if (!found) {

           //not found

           System.out.println(c + " is not found in the list");

       }

   }

   /**

   * method to count and return the number of occurrence of each characters

   */

   public int[] countLetters() {

       int counts[] = new int[26];

       for (int i = 0; i < numChars; i++) {

           int index = array[i] - 'a';

           counts[index]++;

       }

       return counts;

   }

   @Override

   public String toString() {

       String data = "";

       for (int i = 0; i < numChars; i++) {

           data += array[i] + " ";

           if ((i + 1) % 10 == 0) {

               // putting a line break after 10 chars

               data += " ";

           }

       }

       return data;

   }

}

###############

import java.util.Scanner;

public class Assignment{
   //-------------------------------------------------------
   // Create a list, then repeatedly print the menu and do what the
   // user asks until they quit
   //-------------------------------------------------------
   public static void main(String[] args)
   {
       CharList list = null;
       char newChar, oldChar;
       Scanner scan = new Scanner (System.in);

       printMenu();
       // ask a user to choose a command
       System.out.print(" Please enter a command or type? ");
       String choice = scan.next().toLowerCase();
       char command = choice.charAt(0);

       while (command != 'q')
       {

           switch(command)
           {
           case 'a':// This option calls the constructor and initilaizes the array with random characters
               // it needs to be called first
               System.out.print(" Enter an integer for the array size? ");
               int size = scan.nextInt();
               list = new CharList(size);
               list.randomize();
               break;
           case 'b'://this option prints the list of chracters
               System.out.println(list.toString());
               break;
           case 'c': //adds a new character to the list
               System.out.print(" Enter the new character to add to the list: ");
               newChar = scan.next().charAt(0);
               System.out.print(" Enter the index where you want to add the new character: ");
               int index = scan.nextInt();
               list.addChar(newChar, index);
               break;
           case 'd'://remove the character from the list
               System.out.print(" Enter the character to delete: ");
               oldChar = scan.next().charAt(0);
               list.removeFirst(oldChar);
               break;
           case 'e'://prints the count of the letters in the list
               int[] temp = list.countLetters();
               for (int i=0; i<temp.length; i++)
                   System.out.println((char)(i + 'a')+ " " + temp[i]);

               break;
           case '?':
               printMenu();
               break;
           case 'q':
               break;

           default:
               System.out.println("Invalid input");

           }
           System.out.print(" Please enter a command or type? ");
           choice = scan.next().toLowerCase();
           command = choice.charAt(0);
       }
   }

   //-------------------------------------------------------
   // Print the user's choices
   //-------------------------------------------------------
   public static void printMenu()
   {

       System.out.println("    Menu   ");
       System.out.println("   ---------------");

       System.out.println("a: Create a new list (** do this first!! **)");
       System.out.println("b: Print the list of characters");
       System.out.println("c: Add a character to the list");
       System.out.println("d: Remove a character from the list");
       System.out.println("e: Count the number of letters in the list");
       System.out.println("q: Quit");
   }


}

/*OUTPUT*/

   Menu  

   ---------------

a: Create a new list (** do this first!! **)

b: Print the list of characters

c: Add a character to the list

d: Remove a character from the list

e: Count the number of letters in the list

q: Quit

Please enter a command or type? a

Enter an integer for the array size? 25

Please enter a command or type? b

p y b d j h p w z q

m o a a f e p n o p

h m u w x

Please enter a command or type? c

Enter the new character to add to the list: w

Enter the index where you want to add the new character: 3

w is added to index 3

Please enter a command or type? b

p y b w d j h p w z

q m o a a f e p n o

p h m u w x

Please enter a command or type? d

Enter the character to delete: b

b is removed from index 2

Please enter a command or type? e

a 2

b 0

c 0

d 1

e 1

f 1

g 0

h 2

i 0

j 1

k 0

l 0

m 2

n 1

o 2

p 4

q 1

r 0

s 0

t 0

u 1

v 0

w 3

x 1

y 1

z 1

Please enter a command or type? q

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote