Please choose from one of the following options: 1. Convert to all uppercase 2.
ID: 3751911 • Letter: P
Question
Please choose from one of the following options: 1. Convert to all uppercase 2. Convert to all lowercase 3. Show the number of characters 4. Show the 12th character All input and output for this program should be done through the use of graphical dialog boxes as discussed in class (i.e. JOptionPane). Instructions: 1. Create a second class named StringManipulator. This class should have one member variable to hold the text that is entered by the user. You will need to create get and set methods for the input member variable. You will also need to create an empty constructor that will initialize the value of the input String to an empty. You will also need to create four public methods in the StringManipulator class. One method will return the input string converted to all uppercase, the second will return the input string converted to all lowercase, the third will return the number of characters in the input string and the last will return the 12th character in the input string if it exists (if it does not exist, the method should return an error message). The methods should be formed as follows: 2. a. convertToUppercase()-no arguments and returns a String value b. convertToLowercase() - no arguments and returns a String value c. getNumCharacters() - no arguments and returns an int value d. lastMethod) (choose a good name)-no arguments and returns a String value 3. In the Main class, create a variable of type StringManipulator that you created in the previous step. Use an Input dialog box to prompt the user for a string value, store that value in the StringManipulator variable you just created, and then call a separate method to display the menu above in a Message dialog box. 4 After hz user makes a setection you well catl another method that wit use a switch shatement to chick the valns of yle munu entny made by he user and then use he shingman pwntor that you Created earier i J dptanPane,ts report to er he infe regesteExplanation / Answer
String Manipulator
=================================================================
package com.string.manipulator;
public class StringManipulator {
public StringManipulator() {
string="";
}
private String string;
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public String convertToUppercase(){
return this.getString().toUpperCase();
}
public String convertToLowercase(){
return this.getString().toLowerCase();
}
public int getNumCharacters(){
return this.getString().length();
}
public String tewelthChar(){
return this.getString().charAt(11)+"";
}
}
========================================================
Main
=======================================================
package com.string.manipulator;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
@SuppressWarnings("serial")
public class Main extends JPanel
implements ActionListener {
private static JFrame f;
private static String string;
static StringManipulator stringManipulator;
public Main() {
super(new BorderLayout());
//Create the radio buttons.
JRadioButton convertToUppercase = new JRadioButton("convertToUppercase");
convertToUppercase.setMnemonic(KeyEvent.VK_U);
convertToUppercase.setActionCommand("convertToUppercase");
//convertToUppercase.setSelected(true);
JRadioButton convertToLowercase = new JRadioButton("convertToLowercase");
convertToLowercase.setMnemonic(KeyEvent.VK_L);
convertToLowercase.setActionCommand("convertToLowercase");
JRadioButton getNumCharacters = new JRadioButton("getNumCharacters");
getNumCharacters.setMnemonic(KeyEvent.VK_N);
getNumCharacters.setActionCommand("getNumCharacters");
JRadioButton tewelthChar = new JRadioButton("tewelthChar");
tewelthChar.setMnemonic(KeyEvent.VK_T);
tewelthChar.setActionCommand("tewelthChar");
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(convertToUppercase);
group.add(convertToLowercase);
group.add(getNumCharacters);
group.add(tewelthChar);
//Register a listener for the radio buttons.
convertToUppercase.addActionListener(this);
convertToLowercase.addActionListener(this);
getNumCharacters.addActionListener(this);
tewelthChar.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(convertToUppercase);
radioPanel.add(convertToLowercase);
radioPanel.add(getNumCharacters);
radioPanel.add(tewelthChar);
add(radioPanel, BorderLayout.LINE_START);
//add(picture, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e) {
String selection=e.getActionCommand();
switch (selection) {
case "convertToUppercase":
stringManipulator.setString(string);
JOptionPane.showMessageDialog(f,"Upper Case of input provided : "+stringManipulator.convertToUppercase());
break;
case "convertToLowercase":
stringManipulator.setString(string);
JOptionPane.showMessageDialog(f,"Lower Case of input provided : "+stringManipulator.convertToLowercase());
break;
case "getNumCharacters":
stringManipulator.setString(string);
JOptionPane.showMessageDialog(f,"Number of characters in the input provided : "+stringManipulator.getNumCharacters());
break;
case "tewelthChar":
stringManipulator.setString(string);
if(string.length()>=12){
JOptionPane.showMessageDialog(f,"Tweleth Char in the input provided : "+stringManipulator.tewelthChar());
}
else {
JOptionPane.showMessageDialog(f,"Length of the String provided is less than 12, Can't fetch 12th Character");
}
break;
default:
break;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
f = new JFrame("RadioButtonDemo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Main();
newContentPane.setOpaque(true); //content panes must be opaque
f.setContentPane(newContentPane);
string=JOptionPane.showInputDialog(f,"Ente the String");
stringManipulator=new StringManipulator();
stringManipulator.setString(string);
//Display the window.
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
===============================