Skip to content

Commit e5bebd1

Browse files
Merge pull request #436 from KhwajaYousuf/feature/enhanced-word-char-counter
Feature/enhanced word char counter
2 parents fc54a82 + 37a331a commit e5bebd1

File tree

2 files changed

+105
-119
lines changed

2 files changed

+105
-119
lines changed

Weather.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,38 @@ def main():
33
NUMBER_OF_HOURS = 24
44

55
# Initialize data
6-
data = []
7-
for i in range(NUMBER_OF_DAYS):
8-
data.append([])
9-
for j in range(NUMBER_OF_HOURS):
10-
data[i].append([])
11-
data[i][j].append(0) # Temperature value
12-
data[i][j].append(0) # Humidity value
6+
data = initialize_data(NUMBER_OF_DAYS, NUMBER_OF_HOURS)
137

148
# Read input using input redirection from a file
15-
for k in range(NUMBER_OF_DAYS * NUMBER_OF_HOURS):
9+
read_input(data, NUMBER_OF_DAYS, NUMBER_OF_HOURS)
10+
11+
# Find and display the average daily temperature and humidity
12+
calculate_and_display_averages(data, NUMBER_OF_DAYS, NUMBER_OF_HOURS)
13+
14+
15+
def initialize_data(num_days, num_hours):
16+
"""Initialize a 3D list for storing temperature and humidity data."""
17+
return [[[0, 0] for _ in range(num_hours)] for _ in range(num_days)]
18+
19+
20+
def read_input(data, num_days, num_hours):
21+
"""Read input data and populate the data list."""
22+
for _ in range(num_days * num_hours):
1623
line = input().strip().split()
17-
day = eval(line[0])
18-
hour = eval(line[1])
19-
temperature = eval(line[2])
20-
humidity = eval(line[3])
21-
data[day - 1][hour - 1][0] = temperature
22-
data[day - 1][hour - 1][1] = humidity
23-
24-
# Find the average daily temperature and humidity
25-
for i in range(NUMBER_OF_DAYS):
26-
dailyTemperatureTotal = 0
27-
dailyHumidityTotal = 0
28-
for j in range(NUMBER_OF_HOURS):
29-
dailyTemperatureTotal += data[i][j][0]
30-
dailyHumidityTotal += data[i][j][1]
24+
day, hour, temperature, humidity = map(eval, line)
25+
data[day - 1][hour - 1] = [temperature, humidity]
26+
27+
28+
def calculate_and_display_averages(data, num_days, num_hours):
29+
"""Calculate and display the average daily temperature and humidity."""
30+
for i in range(num_days):
31+
daily_temperature_total = sum(data[i][j][0] for j in range(num_hours))
32+
daily_humidity_total = sum(data[i][j][1] for j in range(num_hours))
3133

3234
# Display result
33-
print("Day" + str(i) + "'s average temperature is "
34-
+ str(dailyTemperatureTotal / NUMBER_OF_HOURS))
35-
print("Day " + str(i) + "'s average humidity is "
36-
+ str(dailyHumidityTotal / NUMBER_OF_HOURS))
35+
print(f"Day {i + 1}'s average temperature is {daily_temperature_total / num_hours:.2f}")
36+
print(f"Day {i + 1}'s average humidity is {daily_humidity_total / num_hours:.2f}")
37+
3738

38-
main() # Call the main function
39+
if __name__ == "__main__":
40+
main() # Call the main function

wordcounter.java

Lines changed: 76 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,76 @@
1-
import java.awt.event.*;
2-
import javax.swing.*;
3-
public class WCC extends JFrame implements ActionListener{
4-
JTextArea ta;
5-
JButton b1,b2;
6-
WCC(){
7-
super("Word Character Counter - JavaTpoint");
8-
ta=new JTextArea();
9-
ta.setBounds(50,50,300,200);
10-
11-
b1=new JButton("Word");
12-
b1.setBounds(50,300,100,30);
13-
14-
b2=new JButton("Character");
15-
b2.setBounds(180,300,100,30);
16-
17-
b1.addActionListener(this);
18-
b2.addActionListener(this);
19-
add(b1);add(b2);add(ta);
20-
setSize(400,400);
21-
setLayout(null);
22-
setVisible(true);
23-
}
24-
public void actionPerformed(ActionEvent e){
25-
String text=ta.getText();
26-
if(e.getSource()==b1){
27-
String words[]=text.split("\\s");
28-
JOptionPane.showMessageDialog(this,"Total words: "+words.length);
29-
}
30-
if(e.getSource()==b2){
31-
JOptionPane.showMessageDialog(this,"Total Characters with space: "+text.length());
32-
}
33-
}
34-
public static void main(String[] args) {
35-
new WCC();
36-
}
37-
}
38-
Word Count Example with Pad and Text Color
39-
import java.awt.*;
40-
import javax.swing.*;
41-
import java.awt.event.*;
42-
public class CharCount extends JFrame implements ActionListener{
43-
JLabel lb1,lb2;
44-
JTextArea ta;
45-
JButton b;
46-
JButton pad,text;
47-
CharCount(){
48-
super("Char Word Count Tool - JTP");
49-
lb1=new JLabel("Characters: ");
50-
lb1.setBounds(50,50,100,20);
51-
lb2=new JLabel("Words: ");
52-
lb2.setBounds(50,80,100,20);
53-
54-
ta=new JTextArea();
55-
ta.setBounds(50,110,300,200);
56-
57-
b=new JButton("click");
58-
b.setBounds(50,320, 80,30);//x,y,w,h
59-
b.addActionListener(this);
60-
61-
pad=new JButton("Pad Color");
62-
pad.setBounds(140,320, 110,30);//x,y,w,h
63-
pad.addActionListener(this);
64-
65-
text=new JButton("Text Color");
66-
text.setBounds(260,320, 110,30);//x,y,w,h
67-
text.addActionListener(this);
68-
69-
add(lb1);add(lb2);add(ta);add(b);add(pad);add(text);
70-
71-
setSize(400,400);
72-
setLayout(null);//using no layout manager
73-
setVisible(true);
74-
setDefaultCloseOperation(EXIT_ON_CLOSE);
75-
}
76-
public void actionPerformed(ActionEvent e){
77-
if(e.getSource()==b){
78-
String text=ta.getText();
79-
lb1.setText("Characters: "+text.length());
80-
String words[]=text.split("\\s");
81-
lb2.setText("Words: "+words.length);
82-
}else if(e.getSource()==pad){
83-
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);
84-
ta.setBackground(c);
85-
}else if(e.getSource()==text){
86-
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);
87-
ta.setForeground(c);
88-
}
89-
}
90-
public static void main(String[] args) {
91-
new CharCount();
92-
}}
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
class WordCharCount extends JFrame implements ActionListener {
7+
JTextArea textArea;
8+
JButton wordButton, charButton, bgColorButton, textColorButton;
9+
10+
WordCharCount() {
11+
super("Word Character Counter - JavaTpoint");
12+
13+
textArea = new JTextArea();
14+
textArea.setBounds(50, 50, 300, 200);
15+
16+
wordButton = createButton("Word", 50, 300, 100, 30);
17+
charButton = createButton("Character", 180, 300, 100, 30);
18+
bgColorButton = createButton("Background Color", 50, 340, 150, 30);
19+
textColorButton = createButton("Text Color", 210, 340, 120, 30);
20+
21+
addComponents();
22+
addActionListeners();
23+
24+
setSize(400, 400);
25+
setLayout(null);
26+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27+
setVisible(true);
28+
}
29+
30+
private JButton createButton(String label, int x, int y, int width, int height) {
31+
JButton button = new JButton(label);
32+
button.setBounds(x, y, width, height);
33+
return button;
34+
}
35+
36+
private void addComponents() {
37+
add(textArea);
38+
add(wordButton);
39+
add(charButton);
40+
add(bgColorButton);
41+
add(textColorButton);
42+
}
43+
44+
private void addActionListeners() {
45+
wordButton.addActionListener(this);
46+
charButton.addActionListener(this);
47+
bgColorButton.addActionListener(this);
48+
textColorButton.addActionListener(this);
49+
}
50+
51+
public void actionPerformed(ActionEvent e) {
52+
String text = textArea.getText();
53+
if (e.getSource() == wordButton) {
54+
String[] words = text.split("\\s");
55+
showMessage("Total words: " + words.length);
56+
} else if (e.getSource() == charButton) {
57+
showMessage("Total Characters with space: " + text.length());
58+
} else if (e.getSource() == bgColorButton) {
59+
Color bgColor = JColorChooser.showDialog(this, "Choose Background Color", Color.WHITE);
60+
textArea.setBackground(bgColor);
61+
} else if (e.getSource() == textColorButton) {
62+
Color textColor = JColorChooser.showDialog(this, "Choose Text Color", Color.BLACK);
63+
textArea.setForeground(textColor);
64+
}
65+
}
66+
67+
private void showMessage(String message) {
68+
JOptionPane.showMessageDialog(this, message);
69+
}
70+
}
71+
72+
public class CharWordCountTool {
73+
public static void main(String[] args) {
74+
SwingUtilities.invokeLater(WordCharCount::new);
75+
}
76+
}

0 commit comments

Comments
 (0)