Question

In: Computer Science

Please, read the code below so you can figure out what is the steps that are...

Please, read the code below so you can figure out what is the steps that are used. (Answer in words)

import java.util.*;
import java.io.*;

/
class WordHelper{
public static String vowels = "aeiouyAEIOUY";
  
//method to check whether a given character is vowel or not
public static String[] sortByVowels(String[] words){
Integer[] noOfVowels = new Integer[words.length];
String[] newArray = new String[words.length];   
newArray[i] = words[i];
int cnt = 0;
for(int j = 0; j < words[i].length(); j++){
String temp = Character.toString(words[i].charAt(j));
if(vowels.contains(temp)){
cnt++;
}
}
noOfVowels[i] = cnt;
}
for(int i = 0; i < newArray.length; i++){
for(int j = 0; j < newArray.length - i - 1; j++){
if(noOfVowels[j] > noOfVowels[j+1]){
String temp = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = temp;
  
Integer tmp = noOfVowels[j];
noOfVowels[j] = noOfVowels[j+1];
noOfVowels[j+1] = tmp;
}
}
}
return newArray;
}

public static String[] sortByConsonants(String[] words){
Integer[] noOfConsonants = new Integer[words.length];
String[] newArray = new String[words.length];
for(int i = 0; i < words.length; i++){
newArray[i] = words[i];
int cnt = 0;
for(int j = 0; j < words[i].length(); j++){
String temp = Character.toString(words[i].charAt(j));
if(vowels.contains(temp)){
cnt++;
}
}
noOfConsonants[i] = words[i].length() - cnt;
}

for(int i = 0; i < newArray.length; i++){
for(int j = 0; j < newArray.length - i - 1; j++){
if(noOfConsonants[j] > noOfConsonants[j+1]){
String temp = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = temp;
  
Integer tmp = noOfConsonants[j];
noOfConsonants[j] = noOfConsonants[j+1];
noOfConsonants[j+1] = tmp;
}
}
}
return newArray;
}

public static String[] sortByLength(String[] words){
Integer[] length = new Integer[words.length];
String[] newArray = new String[words.length];
for(int i = 0; i < words.length; i++){
newArray[i] = words[i];
length[i] = words[i].length();
}
for(int i = 0; i < newArray.length; i++){
for(int j = 0; j < newArray.length - i - 1; j++){
if(length[j] > length[j+1]){
String temp = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = temp;
  
Integer tmp = length[j];
length[j] = length[j+1];
length[j+1] = tmp;
}
}
}
return newArray;
}
}


/* ADD YOUR CODE HERE */


Solutions

Expert Solution

After going through the code I have understand that you want to sort array of words given based on

1 .vowel count

2. consonant count

3. word length.

I wrote code for that and in the code below

initially takes total number of words for sorting , and reads those many words

later sorts based on all above 3 cases and prints the respective words respectively. go through below code

//java code starts from here

import java.util.*;
import java.io.*;

public class WordHelper {
   public static String vowels = "aeiouAEIOU";//vowels

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter number of string to sort");
       int totalWords = scan.nextInt();// reading no of strings
       System.out.println("Enter words ");
       String words[] = new String[totalWords];
       for (int i = 0; i < totalWords; i++) {
           words[i] = scan.next();// inserting into an array
       }

       String[] sort = null;
       sort = sortByVowels(words);// calling sort by vowel method
       System.out.println("***********After sorting by vowels************");
       for (int i = 0; i < sort.length; i++) {
           System.out.println(sort[i]);
       }

       sort = sortByConsonants(words);// calling sort by consonants method
       System.out.println("***********After sorting by consonants********");
       for (int i = 0; i < sort.length; i++) {
           System.out.println(sort[i]);
       }

       sort = sortByLength(words);// calling sort by length method
       System.out.println("***********After sorting by length*********");
       for (int i = 0; i < sort.length; i++) {
           System.out.println(sort[i]);
       }
   }

   public static String[] sortByVowels(String[] words) {

       // initailizing arrays based on sizes
       Integer[] noOfVowels = new Integer[words.length];
       String[] newArray = new String[words.length];

       // calculating vowels length for every string and storing into array on index
       // based
       for (int i = 0; i < words.length; i++) {
           int count = 0;
           for (int j = 0; j < words[i].length(); j++) {
               if (vowels.contains(words[i].charAt(j) + "")) {
                   count++;
               }
           }
           newArray[i] = words[i];
           noOfVowels[i] = count;
       }

       // comparing vowels length and sorting and interchanging based on indexes
       for (int i = 0; i < noOfVowels.length; i++) {
           for (int j = i + 1; j < noOfVowels.length; j++) {
               if (noOfVowels[i] > noOfVowels[j]) {
                   String temp = newArray[i];
                   newArray[i] = newArray[j];
                   newArray[j] = temp;
               }
           }
       }
       return newArray;
   }

   public static String[] sortByConsonants(String[] words) {
       // initailizing arrays based on sizes
       Integer[] noOfConsonants = new Integer[words.length];
       String[] newArray = new String[words.length];

       // calculating vowels length for every string and storing into array on index
       // based
       for (int i = 0; i < words.length; i++) {
           int count = 0;
           for (int j = 0; j < words[i].length(); j++) {
               if (!vowels.contains(words[i].charAt(j) + "")) {
                   count++;
               }
           }
           newArray[i] = words[i];
           noOfConsonants[i] = count;
       }

       // comparing vowels length and sorting and interchanging based on indexes
       for (int i = 0; i < noOfConsonants.length; i++) {
           for (int j = i + 1; j < noOfConsonants.length; j++) {
               if (noOfConsonants[i] > noOfConsonants[j]) {
                   String temp = newArray[i];
                   newArray[i] = newArray[j];
                   newArray[j] = temp;
               }
           }
       }
       return newArray;
   }

   public static String[] sortByLength(String[] words) {

       String[] newArray = new String[words.length];
       for (int i = 0; i < words.length; i++) {
           newArray[i] = words[i];
       }
      
       // using bubble sort for sorting based on length
       for (int i = 0; i < newArray.length; i++) {
           for (int j = i + 1; j < newArray.length; j++) {
               if (newArray[i].length() > newArray[j].length()) {
                   String temp = newArray[j];
                   newArray[j] = words[i];
                   newArray[i] = temp;
               }
           }
       }
       return words;
   }
}


Related Solutions

Download “dict.h” and “dictionary.c” posted on blackboard. a) read both code to figure out what they...
Download “dict.h” and “dictionary.c” posted on blackboard. a) read both code to figure out what they do. b) supply am appropriate content to the main function in “dictionary.c” to demonstrate that you understand the provided code by invoking those functions defined in “dictionary.c” Submit your source code and script files. /* dict.h header for data dictionary routines. */ #include <stdio.h> struct dict_elem{    char d_name[15]; // name of dictionary member    int d_start; //starting position in record    int d_length;...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Can you please take this code and just rewrite it so it can be easily be...
Can you please take this code and just rewrite it so it can be easily be able to put into a complier. in java Implementation of above program in java: import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of internet logs from a notepad. 2. then you will extract the following. - a host (e.g., '146.204.224.152') - a user_name (e.g., 'feest6811' note: sometimes the username is missing! In this case, use '-' as the value for the username.) - the timme a request was made (e.g., '21/Jun/2019:15:45:24-0700') - the post request type (e.g., 'POST /incentivize HTTP/1.1' note: not everthing is a POST!) Your...
If you can distinguish between addition and condensation polymers, then you can figure out what the...
If you can distinguish between addition and condensation polymers, then you can figure out what the monomers are. Describe how you would do this for the three kinds of polymers.
Read the text below and answer this: What regulatory steps can the Indian government take to...
Read the text below and answer this: What regulatory steps can the Indian government take to make the environment more hospitable to investors? Excessive red tapism, poor infrastructure, complex tax policies, protectionist labor laws, high licensing and inspection costs, congested judicial system and land acquisition problems are major bottlenecks forcing investors from Korea to opt for other Asian countries over India, according to a study by apex industry body ASSOCHAM. "With just 1.25 per cent share i.e. about $2.6 billion...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
I wrote this code and it produces a typeError, so please can you fix it? import...
I wrote this code and it produces a typeError, so please can you fix it? import random def first_to_a_word(): print("###### First to a Word ######") print("Instructions:") print("You will take turns choosing letters one at a time until a word is formed.") print("After each letter is chosen you will have a chance to confirm whether or not a word has been formed.") print("When a word is formed, the player who played the last letter wins!") print("One of you has been chosen...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
What are two ways you can figure out the serotype of a bacteria? Why would you...
What are two ways you can figure out the serotype of a bacteria? Why would you not provide antibiotics for a salmonella infection? Under what circumstances would you give antibiotics?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT