Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help on java H/W. Thanks Create the String - \"I love Java, it is my favori

ID: 3833816 • Letter: N

Question

Need help on java H/W. Thanks

Create the String - "I love Java, it is my favorite thing!!" as a class object. Create a method that will return the number of vowels in the string. Vowels are {a, e, i, o, u, y} Create a method that will return the number of consonants in the string. Use method naming conventions. Main should only be used to execute methods created in the class. Whiteboard / Pseudocode Solution (Attach Pseudocode, Source Code, and Output Below.) Advance implementation look to count the accuracy of each individual vowel. For example there are 3 a's.

Explanation / Answer

Here is the code for CountVowelsAndConsonants.java:

class CountVowelsAndConsonants
{
String string;
public CountVowelsAndConsonants(String string)
{
this.string = string;
}
public void setString(String string)
{
this.string = string;
}
public int countVowels()
{
String temp = string.toLowerCase();
int count = 0;
for(int i = 0; i < string.length(); i++)
if(temp.charAt(i) == 'a' || temp.charAt(i) == 'e' || temp.charAt(i) == 'i' || temp.charAt(i) == 'o' || temp.charAt(i) == 'u')
count++;
return count;
}
public int countConsonants()
{
int count = 0;
for(int i = 0; i < string.length(); i++)
if(Character.isLetter(string.charAt(i)))
count++;
return count - this.countVowels();
}
}

And here is the code for CountVowelsAndConsonantsDemo.java:

import java.util.*;
class CountVowelsAndConsonantsDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//1. The user is asked to enter a string.
System.out.print("Enter the string: ");
String string = sc.nextLine();
CountVowelsAndConsonants cs = new CountVowelsAndConsonants(string);
//2. The program displays the following menu:
//a. Count the number of vowels in the string
//b. Count the number of consonants in the string
//c. Count both the vowels and consonants in the string
//d. Enter another string
//e. Exit the program
while(true)
{
System.out.println("a. Count the number of vowels in the string");
System.out.println("b. Count the number of consonants in the string");
System.out.println("c. Count both the vowels and consonants in the string");
System.out.println("d. Enter another string");
System.out.println("e. Exit the program");
System.out.println("Enter your choice: ");
char choice = sc.next().charAt(0);
switch(choice)
{
case 'a':   System.out.println("The number of vowels in the string are: " + cs.countVowels()); break;
case 'b':   System.out.println("The number of consonants in the string are: " + cs.countConsonants()); break;
case 'c':   System.out.println("The number of vowels and consonants in the string are: " + (cs.countVowels() + cs.countConsonants())); break;
case 'd':   System.out.println("Enter the string to update: ");
           string = sc.nextLine();
           string = sc.nextLine();
           cs.setString(string);
           break;
case 'e':   return;
default :    System.out.println("Invalid menu choice.");
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote