Question

In: Computer Science

Create a class that calls for user to input a string. Pass the string from that...

Create a class that calls for user to input a string. Pass the string from that class to another and count how many words the string has, count how many vowels are in the string and count how many letters end with d.

java language

Solutions

Expert Solution

//TestCode.java
import java.util.Scanner;
public class TestCode {
    public static int countWords(String s){
        return s.split(" ").length;
    }

    public static int countVowels(String s){
        int count = 0;
        char ch;
        for(int i = 0;i<s.length();i++){
            ch = s.charAt(i);
            if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
                count += 1;
            }
        }
        return count;
    }

    public static int countWordsEndsWithD(String s){
        String[] splits = s.split(" ");
        int count = 0;
        for(String x: splits){
            if(x.endsWith("d")){
                count += 1;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s;
        System.out.print("Enter string: ");
        s = scanner.nextLine();
        System.out.println("Number of words = "+countWords(s));
        System.out.println("Number of vowels = "+countVowels(s));
        System.out.println("Number of words ends with letter d = "+countWordsEndsWithD(s));
    }
}


Related Solutions

Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Python! Create a program that does the following: Reads a number from the user. Calls a...
Python! Create a program that does the following: Reads a number from the user. Calls a function that finds all the divisors of that number. Calls another function to see if the number 7 is a divisor of the original number. Keeps reading input from the user and calling the function above until the user enters the letter ‘q’. Create 2 functions findDivisors() and lucky7(). Use docstrings to explain what each function does. Use the help() function to output the...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
Q.1: create a python function that takes two integers from the user ( pass them from...
Q.1: create a python function that takes two integers from the user ( pass them from the main program). The function performs the addition operation and returns the result. In the program, ask the user to guess the result. Then, the program compares the user input ( guess) with the returned value, and displays a message that tells if the user guessing is correct or not. --define the function here --- # the program starts here print(“ This program tests...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
• Using the Scanner class to obtain input from the user. • Using printf to output...
• Using the Scanner class to obtain input from the user. • Using printf to output information to the user. • Using arithmetic operators to perform calculations. • Using if statements to make decisions based on the truth or falsity of a condition. • Using relational operators to compare variable values. Project 1: Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division). Sample Output:...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT