Question

In: Computer Science

***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...

***USE JAVA AND NO IMPORTS***

Method editString takes a string and breaks it into an array of single
       words which are then are edited based on the command
       Possible Commands:
       remove: for the sent words, remove those from the text
       replace: replace the word in an even element of words with a word in an odd element of words
       add: add the word given word in the index indicated after the word

       editString("What is up bro", "remove", ["bro"]) returns ["What", "is", "up"]
       editString("What is up bro", "replace", ["bro", "brother"]) returns ["What", "is", "up", "brother"]
       editString("What is up", "add", ["my", "3", "friend", "4"]) returns ["What", "is", "up", "my", "friend"]
       editString("I can only do so much", "remove", ["only", "so"]) returns ["I", "can", "do", "much"]
       editString("Tis but a flesh wound", "replace", ["flesh", "scratch", "wound", "mark"]) returns ["Tis", "but", "a", "scratch", "mark"]
       editString("I can code in java", "add", ["html", "4", "and", 5]) returns ["I", "can", "code", "in", "html", "and", "java"]
       @param text String of words
       @return an array of lowercased words in alphabetical order
   */
   public static ArrayList<String> editString(String text, String command, ArrayList<String> words)
   {
       ArrayList<String> edited = new ArrayList<String>();

   return edited;
   }//end editString

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

---------------solution.java----------------

import java.util.*; // import the ArrayList, Scanner class

public class solution
{
   public static ArrayList<String> editString(String text, String command, ArrayList<String> words){
       ArrayList<String> edited = new ArrayList<String>();
       List<String> list = Arrays.asList(text.split(" "));   //convert text to List
       for(String w: list){                               //convert List to ArrayList
           edited.add(w);
       }

       if (command == "remove"){           //when command is remove
           for(String w: words){           //for each word in words, remove it from edited ArrayList
               edited.remove(w);
           }
       }

       else if(command == "replace"){       //when command is replace
           String from, to;
           for(int i = 0; i < words.size(); i = i+2){   //loop in words ArrayList
               from = words.get(i);                   //get from word and to word
               to = words.get(i+1);
               for(int j = 0; j < edited.size(); j++){   //loop in edited ArrayList
                   if(edited.get(j).equals(from))   //if String is from word, then change into to word
                       edited.set(j, to);
               }
           }
       }


       else if(command == "add"){       //when command is add
           int index;
           for(int i = 0; i < words.size(); i = i+2){       //loop for each word in words
               index = Integer.valueOf(words.get(i+1));   //get the index where we must add it
               edited.add(index, words.get(i));           //add the word at the given index
           }
       }
       return edited;
   }


   public static void printList(ArrayList<String> list){
       System.out.println("The list is: ");
       for(String w: list){
           System.out.print(w + " ");
       }
       System.out.println("\n");
   }


   public static void main(String[] args)
   {
       //for testing the method
       ArrayList<String> edited = new ArrayList<String>();
       ArrayList<String> words = new ArrayList<String>();

       System.out.println("editString(\"What is up bro\", \"remove\", [\"bro\"])");           //test case 1
       words = new ArrayList<String>(Arrays.asList("bro"));
       edited = editString("What is up bro", "remove", words);
       printList(edited);

       System.out.println("editString(\"What is up bro\", \"replace\", [\"bro\", \"brother\"])"); //test case 2
       words = new ArrayList<String>(Arrays.asList("bro", "brother"));
       edited = editString("What is up bro", "replace", words);
       printList(edited);

       System.out.println("editString(\"What is up\", \"add\", [\"my\", \"3\", \"friend\", \"4\"])"); //test case 3
       words = new ArrayList<String>(Arrays.asList("my", "3", "friend", "4"));
       edited = editString("What is up", "add", words);
       printList(edited);

       System.out.println("editString(\"I can only do so much\", \"remove\", [\"only\", \"so\"])"); //test case 4
       words = new ArrayList<String>(Arrays.asList("only", "so"));      
       edited = editString("I can only do so much", "remove", words);
       printList(edited);

       System.out.println("editString(\"Tis but a flesh wound\", \"replace\", [\"flesh\", \"scratch\", \"wound\", \"mark\"])");
       words = new ArrayList<String>(Arrays.asList("flesh", "scratch", "wound", "mark")); //test case 5
       edited = editString("Tis but a flesh wound", "replace", words);
       printList(edited);

       System.out.println("editString(\"I can code in java\", \"add\", [\"html\", \"4\", \"and\", \"5\"])"); //test case 6
       words = new ArrayList<String>(Arrays.asList("html", "4", "and", "5"));
       edited = editString("I can code in java", "add", words);
       printList(edited);
   }
}

--------------Screenshots--------------------

------------------Output------------------

---------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.


Related Solutions

Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array. (i.e. "public static Comparable getMax(Comparable [] anArray)"). 5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT