In: Computer Science
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message.
• A constructor that create a message object with the specified string sentence or phrase.
• Accessor and mutator (getter/setter) for string data field.
• A method named getVowels ( ) that returns the number of vowels in a sentence or phrase.
• A method named getConsonants( ) that returns the number of consonants in a sentence or phrase.
• A method named getDigits( ) that returns the number of digits in a sentence or phrase.
• A method named getUpperCase( ) that returns the number of uppercase letters in a sentence or phrase.
• A method named getLowerCase( ) that returns the number of lowercase letters in a sentence or phrase. Draw a UML diagram for the Message class and then implement it. Write a test program to use the class and demonstrate all methods in the message class.
What do I include as part of the solution ?
• UML diagram for message class.
• Algorithms (flowchart or pseudocode, 25 pts)
for the following methods: getVowels(), getConsonants( ), getDigits (), getUpperCase ( ), getLowerCase ( ). Use a pdf file to submit the flowchart. Remember to include a legend of the variables used as part of the solution.
• JAVA source codes (source.java) – class and demo program ( 25 pts )
SOLUTION -
Below is the java code
CODE -
//java code
import java.util.*;
class Message
{
private String sentence;
Message()
{
sentence="";
}
Message(String text)
{
setSentence(text);
}
void setSentence(String text)
{
sentence=text;
}
String getSentence()
{
return sentence;
}
int getVowels()
{
int count=0;
for(int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A'
|| ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
count=count+1;
}
}
return count;
}
int getConsonants()
{
int count=0;
for(int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if((ch>='a' && ch<='z') || (ch>='A' &&
ch<='Z'))
{
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A'
|| ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
}
else
{
count=count+1;
}
}
}
return count;
}
int getDigits()
{
int count=0;
for(int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if(ch>=48 && ch<=57)
{
count++;
}
}
return count;
}
int getUpperCase()
{
int count=0;
for(int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if(ch>=65 && ch<=90)
{
count++;
}
}
return count;
}
int getLowerCase()
{
int count=0;
for(int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if(ch>=97 && ch<=122)
{
count++;
}
}
return count;
}
public static void main(String args[])
{
String sentence;
Scanner input=new Scanner(System.in);
System.out.print("Enter a sentence : ");
sentence=input.nextLine();
Message msg=new Message(sentence);
System.out.println("Entered phrase or sentence is
"+msg.getSentence());
System.out.println("The number of vowels in the sentence:
"+msg.getVowels());
System.out.println("The number of consonants in the sentence:
"+msg.getConsonants());
System.out.println("The number of digits in the sentence:
"+msg.getDigits());
System.out.println("The number of uppercase letters in the
sentence: "+msg.getUpperCase());
System.out.println("The number of lowercase letters in the
sentence: "+msg.getLowerCase());
}
}
Output:
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------