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

I am posting this again because I haven\'t had an answer yet! Any help would be

ID: 3852990 • Letter: I

Question

I am posting this again because I haven't had an answer yet! Any help would be greatly appreciated!!

Your assignment is to create a class called CharacterList in a file called CharacterList.java. (there is no main method in this class). A class CharacterList has the following instance variables (they must be declared using private):

charArray - an array of characters

count - an integer
The variable count keeps track how many characters are stored in the array. The variable name for the array of characters is charArray.

Note: You need to distinguish the array size (capacity) and "count" that keeps track of characters added to this array so far.

The class CharacterList 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 CharacterList (int arraySize)

A Constructor to construct a CharacterList object with an array capacity specified by the integer parameter
"arraySize". charArray needs to be instantiated and count needs to be initialized to 0. Every character in the array needs to be initialized to a space ' '.

private int
indexOf
(char searchingChar)

It searches the character specified by the parameter to see where it is located and returns the index of its location (use a loop for searching the specified character). If the character is not found, it returns -1. It is a helper method.

public boolean addCharacter (charcharToAdd)

The method checks if the character specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If the character doesn’t exist in the array and the array has not reached its capacity, the character is added to the array at the smallest available index (count keeps track how many characters are added so far). If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the character. If the character is added successfully, then count is incremented and the method returns true. If the character already exists in the array, the new character will not be added, and the method returns false.

public boolean removeCharacter (charcharToRemove)

The method checks if the character specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and if it does, it moves the character stored in the last index (count-1) to where charToRemove was found, and changes the content at the last index (count-1) to a space character ' ', decrements count, and return true. Otherwise, it returns false.

private void doubleArrayCapacity()

It is a helper method and doubles the capacity of the charArray. To do this, you will need to create another array with the doubled size first, then copy the content of charArray to this temporary array. Then the address of the temporary array can be copied to charArray.

public char findLargest()

It finds the character with the largest unicode among the characters stored so far (at the time when this method is called), and returns it. If the array is empty, return the space character ' '.

public char findSmallest()

It finds the character with the smallest unicode among the characters stored so far (at the time when this method is called), and returns it. If the array is empty, return the space character ' '.

public int computeSumOfUnicode()

It computes and returns the sum of unicode of all characters stored in the charArray so far (at the time when this method is called.) If the array is empty, return 0. The unicode (integer) of each character can be obtained by casting each character to an integer.

public String toString( )

Returns a String containing a list of characters stored in the charArray. An example of such string can be:

('a', 'd', 'H', 'L', 'W', 'c', 'k')

The string should start with a '(' and end with a ')'.

Save the CharacterList class in a file called CharacterList.java and use the following program stored in Assignment6.java, which has the main method to create new CharacterList objects and to test your class. You need to modify Assignment6.java by adding your code for the cases '1', '2', '3', '4', '5', '6', '7', and '8'.

The program will ask a user to enter a size for the array. Then it will show the following menu to a user:

Command Options -----------------------------------
1: add a character in the array
2: remove a character from the array
3: display the array
4: compute and display the largest character 5: compute and display the smallest character 6: compute and display the sum of unicode 7: display the menu again
8: quit this program

Then it will ask a user to enter one of the above commands. Based on the user's choice, the program needs to perform corresponding operation. This will be done by using a method you define in the CharacterList class. The program will terminate when a user enters '8'.

Note that you will NOT be removing or modifying the existing code in the Assignment6.java. All you need to do is to add more code.

After compiling CharacterList.java file and Assignment6.java file, you need to execute Assignment6 class. Sample Output: (the inputs entered by a user are shown in bold). Make sure that your program works at least with this scenario.

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

For this and all subsequent assignments, provide a heading (in comments) which includes:

The assignment number.

Its author (your name).

A description of what this program is doing.

Grading Criteria for the part 2:

05 pts: it contains header information

10 pts: adequate comment to explain every method
05 pts: consistent indentation and spacing
10 pts: it compiles
05 pts total (0.5 pt each): The instance variables are declared correctly (charArray and count) 05 pts: The Constructor of CharacterList is correct

05 pts: The indexOf method is correct
05 pts: The addCharacter method is correct
05 pts: The removeCharacter method is correct
05 pts: The doubleArrayCapacity method is correct 05 pts: The findLargest method is correct
05 pts: The findSmallest method is correct
05 pts: The computeSumOfUnicode method is correct 05 pts: The toString method is correct

Please enter a size for the array.

3

Command Options

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

1: add a character in the array

2: remove a character from the array

3: display the array

4: compute and display the largest character

5: compute and display the smallest character

6: compute and display the sum of unicode

7: display the menu again

8: quit this program

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

1 a 1 y 1 L

1

Entered command: 1

Entered command: 1

Please enter a character to add.

a was added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 1

Please enter a character to add.

y was added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 1

Please enter a character to add.

L was added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Please enter a character to add.

p

p was added
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

1 a 1 K 1 Y 1 S 3

4

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

5

6

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

2 a

  

Entered command: 1

Please enter a character to add.

a was not added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 1

Please enter a character to add.

K was added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 1

Please enter a character to add.

Y was added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 1

Please enter a character to add.

S was added

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 3

('a', 'y', 'L', 'p', 'K', 'Y', 'S')

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 4

The largest character is: y

Entered command: 5

The smallest character is: K

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 6

The sum of unicode is: 653

Entered command: 2

Please enter a character to remove.

a was removed

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

2

Entered command: 2
Please enter a character to remove.

g 2 q 3

6

2 y 2 K 3

4

5

6

g was not removed

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 2

Please enter a character to remove.

q was not removed

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 3

('S', 'y', 'L', 'p', 'K', 'Y')

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 6

The sum of unicode is: 556

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 2

Please enter a character to remove.

y was removed

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 2

Please enter a character to remove.

K was removed

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 3

('S', 'Y', 'L', 'p')

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 4

The largest character is: p

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 5

The smallest character is: L

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

Entered command: 6

The sum of unicode is: 360

Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)

8

Entered command: 8

/*------------------------------------------------------------------------- // AUTHOR: your name
// FILENAME: Assignment6.java
// This class prompts a user to enter a size for the array

// and create a CharacterList object. Then it displays
// a menu to a user, and process a requested task accordingly. //----------------------------------------------------------------------*/ import javax.swing.*;

public class Assignment6 {
public static void main(String[] args) {

int size, command;
char inputChar;
String inputString;
// ask a user for a array size
size = Integer.parseInt(JOptionPane.showInputDialog("Please enter a size for the array: "));

// instantiate a CharacterList object CharacterList list1 = new CharacterList(size);

// print the menu printMenu();

do {

// ask a user to choose a command

command = Integer.parseInt(
JOptionPane.showInputDialog("Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)") );

System.out.println("Entered command: " + command); switch (command) {

case 1: // add a character
inputString = JOptionPane.showInputDialog("nPlease enter a character to add”); inputChar = inputString.charAt(0);
boolean added;

/*** ADD YOUR CODE HERE**/

if (added == true) System.out.println(inputChar + "

else
System.out.println(inputChar + "

break;
case 2: // remove a character

was added");
was not added");

inputString = JOptionPane.showInputDialog("nPlease enter a character to remove”); inputChar = inputString.charAt(0);
boolean removed;

/*** ADD YOUR CODE HERE ***/

if (removed == true)
System.out.println(inputChar + " was removed");

else
System.out.println(inputChar + " was not removed");

break;
case 3: // display the array

/*** ADD YOUR CODE HERE ***/ break;

case 4: // compute and display the largest

/*** ADD YOUR CODE HERE ***/ if (inputChar == ' ')

System.out.println(" The list is empty"); else

System.out.println(" The largest character is: " + inputChar); break;

case 5: // compute and display the smallest

/*** ADD YOUR CODE HERE ***/ if (inputChar == ' ')

System.out.println(" The list is empty"); else

System.out.println(" The smallest character is: " + inputChar); break;

case 6: // compute and display the sum of unicode System.out.println(" The sum of unicode is: " +

list1.computeSumOfUnicode()); break;

case 7: printMenu(); break;

}

} while (command != 8); } //end of the main method

// this method prints out the menu to a user

public static void printMenu() { System.out.print(" Command Options "

+ "----------------------------------- " + "1: add a character in the array "
+ "2: remove a character from the array " + "3: display the array "

+ "4: compute and display the largest character " + "5: compute and display the smallest character " + "6: compute and display the sum of unicode "
+ "7: display the menu again "
+ "8: quit this program ");

} // end of the printMenu method } // end of the Assignment7 class

Method

Description of the Method

public CharacterList (int arraySize)

A Constructor to construct a CharacterList object with an array capacity specified by the integer parameter
"arraySize". charArray needs to be instantiated and count needs to be initialized to 0. Every character in the array needs to be initialized to a space ' '.

private int
indexOf
(char searchingChar)

It searches the character specified by the parameter to see where it is located and returns the index of its location (use a loop for searching the specified character). If the character is not found, it returns -1. It is a helper method.

public boolean addCharacter (charcharToAdd)

The method checks if the character specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If the character doesn’t exist in the array and the array has not reached its capacity, the character is added to the array at the smallest available index (count keeps track how many characters are added so far). If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the character. If the character is added successfully, then count is incremented and the method returns true. If the character already exists in the array, the new character will not be added, and the method returns false.

public boolean removeCharacter (charcharToRemove)

The method checks if the character specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and if it does, it moves the character stored in the last index (count-1) to where charToRemove was found, and changes the content at the last index (count-1) to a space character ' ', decrements count, and return true. Otherwise, it returns false.

private void doubleArrayCapacity()

It is a helper method and doubles the capacity of the charArray. To do this, you will need to create another array with the doubled size first, then copy the content of charArray to this temporary array. Then the address of the temporary array can be copied to charArray.

public char findLargest()

It finds the character with the largest unicode among the characters stored so far (at the time when this method is called), and returns it. If the array is empty, return the space character ' '.

public char findSmallest()

It finds the character with the smallest unicode among the characters stored so far (at the time when this method is called), and returns it. If the array is empty, return the space character ' '.

public int computeSumOfUnicode()

It computes and returns the sum of unicode of all characters stored in the charArray so far (at the time when this method is called.) If the array is empty, return 0. The unicode (integer) of each character can be obtained by casting each character to an integer.

public String toString( )

Returns a String containing a list of characters stored in the charArray. An example of such string can be:

('a', 'd', 'H', 'L', 'W', 'c', 'k')

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.june;

import java.util.Scanner;

/**
*
* @author Sam
*/
public class CharacterList {
    private char[] charArray;
    private int count;

    public CharacterList(int arraySize) {
        charArray = new char[arraySize];
        count = 0;
    }
  
    private int indexOf(char searchingChar) {
        for (int i = 0; i < count; i++)
            if (searchingChar == charArray[i])
                return i;
        return -1;
    }
  
    public boolean addCharacter(char charToAdd) {
        if (indexOf(charToAdd)!= -1)
            return false;
        if (count == charArray.length)
            doubleArrayCapacity();
        charArray[count++] = charToAdd;
        return true;
    }
  
    public boolean removeCharacter(char charToRemove) {
        if (indexOf(charToRemove) == -1)
            return false;
        charArray[indexOf(charToRemove)] = charArray[count-1];
        count --;
        return true;
    }
  
    private void doubleArrayCapacity() {
        char[] newCharArray = new char[charArray.length*2];
        System.arraycopy(charArray, 0, newCharArray, 0, count);
        charArray = newCharArray;
    }
  
    public char findLargest() {
        if (count == 0)
            return ' ';
        char largest = charArray[0];
        for (int i = 1; i < count; i++)
            if ((int)charArray[i] > (int)largest)
                largest = charArray[i];
        return largest;
    }
  
    public char findSmallest(){
        if (count == 0)
            return ' ';
        char smallest = charArray[0];
        for (int i = 1; i < count; i++)
            if ((int)charArray[i] < (int)smallest)
                smallest = charArray[i];
        return smallest;
    }
  
    public int computeSumOfUnicode(){
        int sum = 0;
        for (char ch : charArray)
            sum += (int)ch;
        return sum;
    }
  
    @Override
    public String toString() {
        if (count == 0)
            return "()";
        String result = "(";
        for(int i = 0; i < count - 1; i++)
            result += "'" + charArray[i] + "',";
        result += "'" + charArray[count - 1] + "')";
        return result;
    }
  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int choice;
        boolean exit = false;
        CharacterList cl = new CharacterList(10);
        while (!exit) {
            System.out.println("Command Options ----------------------------------- " +
                    "1: add a character in the array " +
                    "2: remove a character from the array " +
                    "3: display the array " +
                    "4: compute and display the largest character "
                    + " 5: compute and display the smallest character "
                    + " 6: compute and display the sum of unicode "
                    + " 7: display the menu again " +
                    "8: quit this program");
            choice = Integer.parseInt(sc.nextLine());
            switch(choice) {
                case 1:
                    System.out.println("Enter a character to add:");
                    if (cl.addCharacter(sc.nextLine().charAt(0)))
                        System.out.println("Success");
                    else
                        System.out.println("Duplicate");
                    break;
                case 2:
                    System.out.println("Enter a character to remove:");
                    if (cl.removeCharacter(sc.nextLine().charAt(0)))
                        System.out.println("Success");
                    else
                        System.out.println("Not found");
                    break;
                case 3:
                    System.out.println(cl);
                    break;
                case 4:
                    System.out.println("Largest char is: " + cl.findLargest());
                    break;
                case 5:
                    System.out.println("Smallest char is: " + cl.findSmallest());
                    break;
                case 6:
                    System.out.println("Sum of all is: " + cl.computeSumOfUnicode());
                    break;
                case 8:
                    exit = true;
                case 7:
                    break;
                default:
                    System.out.println("Wrong choice");
            }
        }
    }
}

The code is ready for you. If you need exact output, You may consider changing minor parts of the main method. If you want me to do the change, please let me know.

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