Answer:
static void sentenceAnalyzer(String sentence){
int lenOfString = sentence.length()-1;
if(sentence.charAt(lenOfString)=='.'){
System.out.println("Declarative");
}
else if(sentence.charAt(lenOfString)=='?'){
System.out.println("Interrogative");
}
else if(sentence.charAt(lenOfString)=='!'){
System.out.println("Exclamation");
}
else{
System.out.println("Unknown");
}
}
Explanation:
Using Java programming language
Create the method as required
Obtain the index of the last element using the string length method
Use if and else statements to check if the character at the last index is equal to any of the characters given and print the expected output
see a complete code with the main method below
<em>import java.util.Scanner;</em>
<em>public class num11 {</em>
<em> public static void main(String[] args) {</em>
<em> Scanner in = new Scanner(System.in);</em>
<em> //Receiving User input</em>
<em> System.out.println("Enter a sentence");</em>
<em> String sentence = in.nextLine();</em>
<em> // Calling the method</em>
<em> sentenceAnalyzer(sentence);</em>
<em> }</em>
<em> static void sentenceAnalyzer(String sentence){</em>
<em> int lenOfString = sentence.length()-1;</em>
<em />
<em> if(sentence.charAt(lenOfString)=='.'){</em>
<em> System.out.println("Declarative");</em>
<em> }</em>
<em> else if(sentence.charAt(lenOfString)=='?'){</em>
<em> System.out.println("Interrogative");</em>
<em> }</em>
<em> else if(sentence.charAt(lenOfString)=='!'){</em>
<em> System.out.println("Exclamation");</em>
<em> }</em>
<em> else{</em>
<em> System.out.println("Unknown");</em>
<em> }</em>
<em> }</em>
<em>}</em>
<em />