Question

In: Computer Science

UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...

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());
}
}

Solutions

Expert Solution

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.


Related Solutions

Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String Which of the following is a correct declaration of the member in Java? 1 public String getHeaderField(String name) 2 public String getHeaderField; 3 public String getHeaderField(String) 4 public static String getHeaderField( name) ------------------------------------------------------------------------------------------------------------------------------------- Note: all answers are case sensitive. Inside a class, the declaration of a constructor looks like the following: public JButton(String text) 1. From the constructor, you infer that the name of...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Draw a UML diagram that describes a class that will be used to describe a product...
Draw a UML diagram that describes a class that will be used to describe a product for sale on Glamazon.com. The product has a name, a description, a price, ratings by many customers (1 to 5 stars), and a group of customer comments. New products have no ratings or comments by customers, but do have a name, description and price. The price can be changed and more customer ratings and comments can be added. A global average rating of all...
In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
import java.util.*;    public class DataAnalysis{    static Set<String> Data_NaN(Set<String> set){        Set<String> set2 =...
import java.util.*;    public class DataAnalysis{    static Set<String> Data_NaN(Set<String> set){        Set<String> set2 = new HashSet<String>();    for (String temp : set) {        temp = temp.replaceAll(        "[^0-9]", "");                  if(!(set.isEmpty())){            set2.add(temp);        }    }    return set2;               } public static void main(String args[]) { // create empty set Set<String> set = new HashSet<String>(); // {3, 25, 33, 21, 55, 43, 78,...
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT