-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberReversalGUI.java
More file actions
75 lines (74 loc) · 1.84 KB
/
NumberReversalGUI.java
File metadata and controls
75 lines (74 loc) · 1.84 KB
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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class NumberReversalGUI extends JFrame implements ActionListener{
JButton ok;
JLabel noLabel;
JTextField noField, checkField;
int a, b, d, length, ans, c;
String no, e;
ArrayList<String>reverNum=new ArrayList<String>();
NumberReversalGUI(){
super("Number Reversal");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane=new JPanel();
noField= new JTextField(10);
checkField= new JTextField(10);
noLabel= new JLabel("Enter a Number");
ok= new JButton("OK");
ok.addActionListener(this);
pane.add(noLabel);
pane.add(noField);
pane.add(ok);
add(pane);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if (source == ok){
no=noField.getText();
checkField.setText("0");
c=Integer.parseInt(checkField.getText());
length=no.length();
d=length;
d--;
for (a=1; a<=length; a++){
e=String.valueOf(no.charAt(d));
reverNum.add(e);
d--;
}
length=reverNum.size();
d=length;
for (a=1; a<=d; a++){
if (c==0){
checkField.setText(String.valueOf(reverNum.get(length-1)));
c=Integer.parseInt(checkField.getText());
}
else{
checkField.setText(String.valueOf(reverNum.get(length-1)+c));
c=Integer.parseInt(checkField.getText());
}
length--;
}
}
ans=Integer.parseInt(checkField.getText());
JOptionPane.showMessageDialog(null, "The reversed number is"+" "+ans, "Answer", JOptionPane.PLAIN_MESSAGE);
a=0;
noField.setText("");
checkField.setText("");
reverNum.clear();
b=0;
c=0;
d=0;
length=0;
e="0";
no="0";
ans=0;
repaint();
}
public static void main(String[] args){
NumberReversalGUI nrgui=new NumberReversalGUI();
}
}