Hello, I was assigned to create a Java Program that outputs a sentence using a d
ID: 3783448 • Letter: H
Question
Hello, I was assigned to create a Java Program that outputs a sentence using a dialog box. Further more, depending on the last character of the sentence, I should also output another dialog box identifying the sentence as exclamatory, declarative, or interrogative. Below is a program I was working on based on the instructions I have mentioned. I do have an idea on what I should be doing, but I feel as if I'm going astray from what I've been instructed on doing. Can you critique the program and provide feedback? Thank you.
import javax.swing.JOptionPane;
public class YourLastNameDialogBox
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "Do you enjoy programming?");
JOptionPane.showMessageDialog(null, "interrogative");
System.exit(0); //Always required when using the JOptionPaneclass
}
}
Explanation / Answer
// YourLastNameDialogBox.java
import javax.swing.JOptionPane;
import java.util.Scanner;
import javax.swing.*;
public class YourLastNameDialogBox
{
public static void main(String[] args)
{
// input from user
JFrame frame = new JFrame("InputDialog");
String sentence = JOptionPane.showInputDialog(frame, "Input sentence");
JOptionPane.showMessageDialog(null, sentence);
// display the type on basis of last character in sentence
if(sentence.charAt(sentence.length()-1) == '?')
JOptionPane.showMessageDialog(null, "interrogative");
else if(sentence.charAt(sentence.length()-1) == '.')
JOptionPane.showMessageDialog(null, "declarative");
else if(sentence.charAt(sentence.length()-1) == '!')
JOptionPane.showMessageDialog(null, "exclamatory");
System.exit(0); //Always required when using the JOptionPaneclass
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.