In: Computer Science
UML Diagram for this java 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());
}
}
UML Class Diagram
The UML class diagram for the given class named "Message" is given above. The class has 1 attribute, 2 constructors, and 8 methods. In the UML class diagram we represent each class with a rectangle containing 3 sections: Class Name, List of attributes, List of constructors and methods. Since we have only 1 class, we have only 1 rectangle.
Representing the attributes
The Class name is Message, so the Title is set to "Message". There is only 1 attribute called "sentence" of data type String, so it is listed next in the attribute section. The attribute name is preceded with a - (minus) sign to indicate that its scope is private.
Representing the constructors
There are 2 constructors: one no-argument constructor and another parameterized constructor. They are then listed in the 3rd section of the rectangle, each preceded by a ~ (tilde) to indicate they are of package scope (i.e. default scope in Java). Constructors have no return type so none are mentioned. The parameters to the constructor are specified within parenthesis after the constructor name as parameter-name : data-type.
Representing the methods
Each method of the class is listed next after the constructors. Methods which return int are stated as so by appending the return type after the parameter list after the colon. Methods which do not return any value are specified as void. The main() method is a static method and hence is underlined as per conventions of UML class diagrams.