-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVowelCounter_Q24.java
121 lines (102 loc) · 3.71 KB
/
VowelCounter_Q24.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* VowelCounter_Q24.java
*
* This program creates a GUI application that counts the number of vowels
* in a string entered in a text field and displays the result in another text field.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VowelCounter_Q24 extends JFrame {
private JTextField inputField;
private JTextField resultField;
private JButton countButton;
private JButton resetButton;
private JButton exitButton;
public VowelCounter_Q24() {
// Set up the frame
super("Vowel Counter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
// Create components
JLabel inputLabel = new JLabel("Enter a string:");
inputField = new JTextField(20);
JLabel resultLabel = new JLabel("Number of vowels:");
resultField = new JTextField(5);
resultField.setEditable(false);
countButton = new JButton("Count Vowels");
resetButton = new JButton("Reset");
exitButton = new JButton("Exit");
// Set up the layout
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3, 2, 10, 10));
mainPanel.add(inputLabel);
mainPanel.add(inputField);
mainPanel.add(resultLabel);
mainPanel.add(resultField);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
buttonPanel.add(countButton);
buttonPanel.add(resetButton);
buttonPanel.add(exitButton);
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BorderLayout(10, 10));
contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPanel.add(mainPanel, BorderLayout.CENTER);
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(contentPanel);
// Add action listeners
countButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String input = inputField.getText();
int vowelCount = countVowels_Q24(input);
resultField.setText(String.valueOf(vowelCount));
}
});
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inputField.setText("");
resultField.setText("");
inputField.requestFocus();
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
/**
* Counts the number of vowels in a string.
*
* @param str the input string
* @return the number of vowels in the string
*/
private int countVowels_Q24(String str) {
if (str == null || str.isEmpty()) {
return 0;
}
int count = 0;
String vowels = "aeiouAEIOU";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (vowels.indexOf(ch) != -1) {
count++;
}
}
return count;
}
public static void main(String[] args) {
// Create and display the GUI on the Event Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VowelCounter_Q24().setVisible(true);
}
});
}
}