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;...
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 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?
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
Please read the article below, and answer the questions that follow. In doing so, remember the...
Please read the article below, and answer the questions that follow. In doing so, remember the following, - Although the use of generic theories, covered in class/your module guide/text book will provide a framework, the expectation for the student is to be able to discuss it in context with research relevant to South Africa and your own reflective experiences. - You are required to extensively research current South African trends, amidst our changing political landscape and benchmarked against a global...
Please print so I can read clearly. Thank you 1. Describe the effects of aging on...
Please print so I can read clearly. Thank you 1. Describe the effects of aging on fluid and electrolyte regulation. 2. Electrolytes to investigate: (Sodium, Potassium, Calcium, Magnesium). Include causation, clinical manifestations, and nursing interventions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT