Your assignment i to create a class called a CharacterList in file called Charac
ID: 3810015 • Letter: Y
Question
Your assignment i to create a class called a CharacterList in file called CharacterList.iava. (there is no main method in this cl A class Characte List has the following instance variables (they must be declared using private charArray-an array of characters an integer coun 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 Deseription of the Method Method A Constructor to construct a CharacterList object with an array capacity specified by the integer parameter "arra charArray needs to be instantiated and coun public CharacterList (int arraysize s to be initialized to 0. Every character in the array needs to be initialized to a space searches the character specified by the parameter to see where it is located and private int indexofchar searchingChar) turns the index of its location. If the character is not found, it returns -1. It is a er method. method checks if the character specified by the parameter exists in the array This can be done using the indexofmethod to see if it return or not) and also hecks if the array has not reached its capacity. If both are satisfied, the character s added to the array at the smallest available index (count keeps track how many public boolean addCharacter(char characters are added so far). If the array reached its capacity, double its size by charToAdd calling the method doubleArrayCapacity0 and add the character. If the character added successfully, then count is incremented and the method returns true. If the haracter already exists in the array, the new character will not be added, and the ethod returns false The method checks if the character specified by the parameter exists in the array This can be done using the indexofmethod to see if it returns -1 or not) and if it public boolean removeCharacter(char does, it moves the character stored in the last index (count-1) to where har ToRemove char ToRemove 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 s 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 private void doubleArrayCapacity0 of charArray to this temporary array. Then the address ofthe temporary array can e copied to charArra 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 public char est he space characterExplanation / Answer
package pack;
public class CharacterList {
static char charArray[];
static char doublecapacity[];
static int count=-1,size=0,doublesize=0,flag=0;/*here flag is indicator once the size of array doubled it
is never reversed to charArray and all the operation performed are on doublecapacity array*/
public CharacterList(int arraysize)
{
size=arraysize;
charArray=new char[arraysize];
}
private static int indexOf(char searchchar)/*returns the Index*/
{
int iloop;
for(iloop=0;iloop<charArray.length;iloop++)
{
if(charArray[iloop]==searchchar)
{
return iloop;
}
}
return iloop;
}
public static boolean AddCharacter(char add)/*Add Character*/
{
count++;/*increase the array pointer*/
if(count>=size||flag==1)
{
doubleArrayCapacity(add); /*if it croses limit of charArray*/
}
else
{
charArray[count]=add; /*Store element at address of count*/
}
return true;
}
public static void doubleArrayCapacity(char c)/*increase Array capacity*/
{
int iloop;
if(count==size) /*if count and size are equal we need to perform below operation*/
{
doublesize=size*2; /*To create char array with double capacity*/
doublecapacity=new char[doublesize];
for(iloop=0;iloop<count;iloop++)
{
doublecapacity[iloop]=charArray[iloop]; /*store all the element of charArray to doublecapacity*/
}
doublecapacity[count]=c;
flag=1;
}
else
{
doublecapacity[count]=c;
}
}
public static boolean removeCharcter(char c)/*remove character*/
{
if(count<size&&flag==0)
{
for(int iloop=0;iloop<count;iloop++)
{
if(charArray[iloop]==c)
{
for(int iloop1=iloop;iloop1<=count;iloop1++)
{
charArray[iloop1]=charArray[iloop1+1];/*removing element from charArray*/
}
count=count-1; /*decrease the counter*/
}
}
}
else
{
for(int iloop=0;iloop<count;iloop++)
{
if(charArray[iloop]==c)
{
for(int iloop1=iloop;iloop1<=count;iloop1++)
{
doublecapacity[iloop1]=doublecapacity[iloop1+1];/*removing element from
doublecapacity*/
}
count=count-1;/*decrease the counter*/
}
}
}
return true;
}
public static char findlargest()/*return largest char*/
{
char max;
if(count<size&&flag==0)
{
max = charArray[0];
for (int iloop=0;iloop<=count;iloop++)
{
if (charArray[iloop]>max)
{
max=charArray[iloop]; /*Store max element found*/
}
}
}
else
{
max = charArray[0];
for (int iloop=0;iloop<=count;iloop++)
{
if (doublecapacity[iloop]>max)
{
max = doublecapacity[iloop];/*Store max element found*/
}
}
}
return max;
}
public static char findsmallest()/*return smaller char*/
{
char min;
if(count<size&&flag==0)
{
min = charArray[0];
for (int iloop=0;iloop<=count;iloop++)
{
if (charArray[iloop]<min)
{
min=charArray[iloop];/*store min element found */
}
}
}
else
{
min=charArray[0];
for (int iloop=0;iloop<=count;iloop++)
{
if (doublecapacity[iloop]<min)
{
min=doublecapacity[iloop];/*store min element found */
}
}
}
return min;
}
public static int ComputeSumofUnicode()/*Compute Unicode value*/
{
int sum=0;
if(count<size&&flag==0)
{
for (int iloop=0;iloop<=count;iloop++)
{
sum=sum+charArray[iloop];/*calculate unicode value from charArray*/
}
}
else
{
for (int iloop=0;iloop<=count;iloop++)
{
sum=sum+doublecapacity[iloop];/*calculate unicode value from doublecapacity Array*/
}
}
return sum;
}
public String toString()/*prints the array*/
{
if(count<size&&flag==0)/*for charArray*/
{
System.out.print("{");
for(int iloop=0;iloop<=count;iloop++)
{
System.out.print("'"+charArray[iloop]+"'");
if(iloop<count)
{
System.out.print(",");
}
}
System.out.println("}");
return "";
}
else/*for doublecapacity Array*/
{
System.out.print("{");
for(int iloop=0;iloop<=count;iloop++)
{
System.out.print("'"+doublecapacity[iloop]+"'");
if(iloop<count)
{
System.out.print(",");
}
}
System.out.println("}");
return "";
}
}
}/*end of class CharacterList.java*/
/*-------------------------------------------------------------------------
//AUTHOR: your name
//FILENAME: Assignment6.java
//SPECIFICATION: 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.
//Your Lab Letter:
//----------------------------------------------------------------------*/
package pack;
import java.util.Scanner;
public class Assignment6
{
public static void main(String[] args)
{
int size, command;
char inputChar;
String inputString;
Scanner scan = new Scanner(System.in);
// ask a user for a array size
System.out.println("Please enter a size for the array. ");
size = scan.nextInt();
// instantiate a CharacterList object
CharacterList list1 = new CharacterList(size);
// print the menu
printMenu();
do
{
// ask a user to choose a command
System.out.println(" Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)");
command = scan.nextInt();
System.out.println("Entered command: " + command);
switch (command)
{
case 1: // add a character
System.out.println(" Please enter a character to add.");
inputString = scan.next();
inputChar = inputString.charAt(0);
boolean added=CharacterList.AddCharacter(inputChar);
if (added == true)
System.out.println(inputChar + " was added");
else
System.out.println(inputChar + " was not added");
break;
case 2: // remove a character
System.out.println(" Please enter a character to remove.");
inputString = scan.next();
inputChar = inputString.charAt(0);
boolean removed=CharacterList.removeCharcter(inputChar);
if (removed == true)
System.out.println(inputChar + " was removed");
else
System.out.println(inputChar + " was not removed");
break;
case 3: // display the array
System.out.println(list1.toString());
break;
case 4: // compute and display the largest
inputChar=CharacterList.findlargest();
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
inputChar=CharacterList.findsmallest();
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;
case 8:
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
/*********************OUTPUT**********************
Please enter a size for the array.
2
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
Entered command: 1
Please enter a character to add.
a
a was added
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
1
Entered command: 1
Please enter a character to add.
b
b was added
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
1
Entered command: 1
Please enter a character to add.
c
c was added
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
3
Entered command: 3
{'a','b','c'}
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
4
Entered command: 4
The largest character is: c
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
5
Entered command: 5
The smallest character is: a
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
6
Entered command: 6
The sum of unicode is: 294
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.
a
a was removed
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
3
Entered command: 3
{'b','c'}
Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)
7
Entered command: 7
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)
8
Entered command: 8
*************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.