In: Computer Science
finish the java programming
Create a new class named MyCharacterListTools that provides the API shown below.
This class does not need a constructor nor will any user-defined constructor be called by the
provided code in MainClassQ1.java.
◦ MyLinkedList createCharacterList(String str) – This method is to
return a MyLinkedList whose data items are the characters of str. All of the
characters must appear in the list and in the same order as they are given in str.
◦ void removeNonLetters(MyLinkedList list) – This method is to remove
from the list any spaces, numbers and punctuation characters (this is to be done inplace).
Letters of the alphabet are to be left in the list in the same order they were given.
For example, if the list contained {H_e_l_l_o_ _W_o_r_l_d_!} then after calling this
function, the list would hold {H_e_l_l_o_W_o_r_l_d}. You may use the built-in Java
static method Character.isLetter(char ch) to test whether the list items are
letters.
◦ boolean testEquality(MyLinkedList l1, MyLinkedList l2) – This
method is to compare two MyLinkedLists for equality. Two lists are considered
equal if their contents are the same and in the same order. Letter case should not be
observed (i.e. 'A' is equal to 'a', 'B' is equal to 'b', etc...). If the two lists are equal then
return true. Return false otherwise. The static built-in Java methods
Character.toLowerCase(char ch) and Character.toUpperCase(char
ch) may be used in this method.
Please follow comments in code
MyLinkedList.java :-
-----------------------------------
import java.util.ArrayList;
/**
*
* This class has list which saves characters of a String in
ArrayList Container
*
*/
public class MyLinkedList {
ArrayList<Character> characters = new
ArrayList<Character>();
//Add Character to list
void add(char ch){
Character cho = new
Character(ch);
characters.add(cho);
}
//get Element at given index
char getItem(int index){
return
characters.get(index).charValue();
}
//get Size of List
int getSize(){
return characters.size();
}
//Remove item at given index
void remove(int index){
characters.remove(index);
}
}
MyCharacterListTools.java :-
----------------------------------------------
/**
*
* This class implements methods mentioned in exercise
*
*/
public class MyCharacterListTools {
MyLinkedList createCharacterList(String str){
MyLinkedList list = new
MyLinkedList();
//Get each character in String and
add to MyLinkedList
for(int i = 0 ; i < str.length()
; i++){
list.add(str.charAt(i));
}
return list;
}
void removeNonLetters(MyLinkedList list) {
//Get each character in
MyLinkedList
for(int i = 0 ; i <
list.getSize() ; i++){
//get Item and
Check if it is Aplhabet or not
//Ascii for
Space is 32 , 33 is for Punctuation mark
if(Character.isDigit(list.getItem(i)) ||
list.getItem(i) == 32
||
list.getItem(i) == 33){
//Remove it if not Aplhabet
list.remove(i);
--i;
}
}
}
boolean testEquality(MyLinkedList l1, MyLinkedList
l2){
//fist check sizes of both, if not
equal return false
if(l1.getSize()
!=l2.getSize()){
return
false;
}
//Loop through Lists and check each
character
for(int i = 0 ; i < l1.getSize()
; i++){
if(Character.toLowerCase(l1.getItem(i)) !=
Character.toLowerCase(l2.getItem(i))){
return false;
}
}
return true;
}
}
MainClassQ1.java :-
---------------------------------
public class MainClassQ1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method
stub
//Create some test
Strings
String test1 = "H_e_l_l_o_
_W_o_r_l_d_!";
String test2 = "H_e_6l_l_7o_
_W_o_r_999l_d_!";
//create new object
MyCharacterListTools tool = new
MyCharacterListTools();
//Create MyLinkedList for both
Strings
MyLinkedList list1 =
tool.createCharacterList(test1);
MyLinkedList list2 =
tool.createCharacterList(test2);
//Test
tool.removeNonLetters(list1);
tool.removeNonLetters(list2);
System.out.println("Equality of
both lists =="+tool.testEquality(list1, list2)) ;
}
}