Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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.

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.

import java.util.Scanner; public class Assignment7{ //------------------------------------------------------- // 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; iSample Output: User input is in RED. Because characters are generated randomly, the characters in your array will look different from the sample output. Please check your result according to the operations of methods but not match the sample output directly.

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

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

Advanced Project Management A Structured Approach

Authors: Frederick Harrison, Dennis Lock

4th Edition

1138270636, 978-1138270633

Students also viewed these Databases questions

Question

4. Avoid pointing or gesturing.

Answered: 1 week ago

Question

Draw a schematic diagram of I.C. engines and name the parts.

Answered: 1 week ago