Write a java program that will take a sentence from user input and outputs a sta
ID: 3631297 • Letter: W
Question
Write a java program that will take a sentence from user input and outputs a statement that identifies what kind of sentence it is. There will be four types of sentences: Declarative, interrogative, exclamatory, or other. The program will do this by identifying punctuation at the end of the sentence.
For example, a sentence of: I ate a sandwich.
Will be considered a declarative sentence.
a sentence of: Did you eat a sandwich?
Will be considered a interrogative sentence.
a sentence of: You ate a sandwich!
will be considered an exclamatory sentence
Any sentence that does not end with punctuation, or the punctuation does not include any of those already mentioned will be considered other
such as: You at a sandwhich #@
Here is an example of how an input and output should look like:
The sentence MUST use JOptionPane to get user input and display output.
The main method will call a method that has one String type
parameter. The sentence that the user entered will be passed to the method and based on
the sentence passed to the method, the method will return a String that is equal to one of
the following Strings:
“declarative”, “interrogative”, “exclamatory”, or “unknown sentence type”
NOTE: The method MUST use a switch statement based on the last character in the sentence passed to the method.
Thanks for all your help guys.
Explanation / Answer
import javax.swing.JOptionPane; public class Cramster { public static void main(String[] args) { String str = JOptionPane.showInputDialog(null, "Enter some text", null); JOptionPane.showMessageDialog(null, "Your sentence is: " + WordSmith(str)); } public static String WordSmith(String input) { char valueToSwitch = input.charAt(input.length()-1); switch(valueToSwitch) { case '.': return "Declarative"; case '!': return "Exclamatory"; case '?': return "Interrogative"; default: return "Unknown"; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.