Question

In: Computer Science

finish the java programming Create a new class named MyCharacterListTools that provides the API shown below....

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.

Solutions

Expert Solution

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)) ;
   }

}


Related Solutions

JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
In java: -Create a class named Animal
In java: -Create a class named Animal
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT