Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Exam
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
67160191
Exam
Commits
a162ecc5
Commit
a162ecc5
authored
2 weeks ago
by
First Naja
Browse files
Options
Downloads
Patches
Plain Diff
"Calculator"
parents
Branches
Exam
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Test.java
+171
-0
171 additions, 0 deletions
src/Test.java
with
171 additions
and
0 deletions
src/Test.java
0 → 100644
+
171
−
0
View file @
a162ecc5
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.awt.event.WindowEvent
;
import
java.awt.event.WindowListener
;
public
class
Test
implements
WindowListener
,
ActionListener
{
double
num1
,
num2
,
ans
;
String
operand1
,
operand2
,
operator
;
String
numStr
,
ansStr
;
TextField
t
;
Frame
f
;
Panel
p
;
Panel
p2
;
Button
b1
,
b2
,
b3
,
b4
,
b5
,
b6
,
b7
,
b8
,
b9
,
b0
,
bPlus
,
bSub
,
bMul
,
bDiv
,
bEqual
,
bDot
;
public
Test
()
{
numStr
=
""
;
f
=
new
Frame
(
"Easy Calculator"
);
f
.
setSize
(
400
,
500
);
f
.
setBackground
(
Color
.
gray
);
f
.
addWindowListener
(
this
);
p
=
new
Panel
();
p
.
setSize
(
200
,
100
);
p
.
setBackground
(
Color
.
pink
);
t
=
new
TextField
();
t
.
setSize
(
400
,
100
);
t
.
setColumns
(
50
);
p
.
add
(
t
);
p2
=
new
Panel
();
p2
.
setSize
(
100
,
100
);
p2
.
setBackground
(
Color
.
orange
);
p2
.
setLayout
(
new
GridLayout
(
4
,
4
));
b1
=
new
Button
(
"1"
);
b2
=
new
Button
(
"2"
);
b3
=
new
Button
(
"3"
);
b4
=
new
Button
(
"4"
);
b5
=
new
Button
(
"5"
);
b6
=
new
Button
(
"6"
);
b7
=
new
Button
(
"7"
);
b8
=
new
Button
(
"8"
);
b9
=
new
Button
(
"9"
);
b0
=
new
Button
(
"0"
);
bPlus
=
new
Button
(
"+"
);
bSub
=
new
Button
(
"-"
);
bMul
=
new
Button
(
"*"
);
bDiv
=
new
Button
(
"/"
);
bDot
=
new
Button
(
"."
);
bEqual
=
new
Button
(
"="
);
// Add listeners to buttons
b1
.
addActionListener
(
this
);
b2
.
addActionListener
(
this
);
b3
.
addActionListener
(
this
);
b4
.
addActionListener
(
this
);
b5
.
addActionListener
(
this
);
b6
.
addActionListener
(
this
);
b7
.
addActionListener
(
this
);
b8
.
addActionListener
(
this
);
b9
.
addActionListener
(
this
);
b0
.
addActionListener
(
this
);
bDot
.
addActionListener
(
this
);
bPlus
.
addActionListener
(
this
);
bSub
.
addActionListener
(
this
);
bMul
.
addActionListener
(
this
);
bDiv
.
addActionListener
(
this
);
bEqual
.
addActionListener
(
this
);
// Add buttons to panel
p2
.
add
(
b7
);
p2
.
add
(
b8
);
p2
.
add
(
b9
);
p2
.
add
(
bPlus
);
p2
.
add
(
b4
);
p2
.
add
(
b5
);
p2
.
add
(
b6
);
p2
.
add
(
bSub
);
p2
.
add
(
b1
);
p2
.
add
(
b2
);
p2
.
add
(
b3
);
p2
.
add
(
bMul
);
p2
.
add
(
b0
);
p2
.
add
(
bDot
);
p2
.
add
(
bEqual
);
p2
.
add
(
bDiv
);
f
.
add
(
p
,
BorderLayout
.
NORTH
);
f
.
add
(
p2
,
BorderLayout
.
CENTER
);
f
.
setVisible
(
true
);
}
public
static
void
main
(
String
[]
args
)
{
Test
test2
=
new
Test
();
}
@Override
public
void
windowActivated
(
WindowEvent
arg0
)
{
}
@Override
public
void
windowClosed
(
WindowEvent
e
)
{
}
@Override
public
void
windowClosing
(
WindowEvent
e
)
{
System
.
exit
(
0
);
}
@Override
public
void
windowDeactivated
(
WindowEvent
e
)
{
}
@Override
public
void
windowDeiconified
(
WindowEvent
e
)
{
}
@Override
public
void
windowIconified
(
WindowEvent
e
)
{
}
@Override
public
void
windowOpened
(
WindowEvent
e
)
{
}
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
String
cmd
=
e
.
getActionCommand
();
// If a number or decimal is pressed
if
(
cmd
.
equals
(
"0"
)
||
cmd
.
equals
(
"1"
)
||
cmd
.
equals
(
"2"
)
||
cmd
.
equals
(
"3"
)
||
cmd
.
equals
(
"4"
)
||
cmd
.
equals
(
"5"
)
||
cmd
.
equals
(
"6"
)
||
cmd
.
equals
(
"7"
)
||
cmd
.
equals
(
"8"
)
||
cmd
.
equals
(
"9"
)
||
cmd
.
equals
(
"."
))
{
numStr
=
numStr
+
cmd
;
t
.
setText
(
numStr
);
}
// If an operator is pressed
else
if
(
cmd
.
equals
(
"+"
)
||
cmd
.
equals
(
"-"
)
||
cmd
.
equals
(
"*"
)
||
cmd
.
equals
(
"/"
))
{
operand1
=
numStr
;
operator
=
cmd
;
numStr
=
""
;
}
// If equal sign is pressed
else
if
(
cmd
.
equals
(
"="
))
{
operand2
=
numStr
;
num1
=
Double
.
parseDouble
(
operand1
);
num2
=
Double
.
parseDouble
(
operand2
);
switch
(
operator
)
{
case
"+"
:
ans
=
num1
+
num2
;
break
;
case
"-"
:
ans
=
num1
-
num2
;
break
;
case
"*"
:
ans
=
num1
*
num2
;
break
;
case
"/"
:
if
(
num2
!=
0
)
{
ans
=
num1
/
num2
;
}
else
{
ansStr
=
"Error"
;
t
.
setText
(
ansStr
);
return
;
}
break
;
}
ansStr
=
Double
.
toString
(
ans
);
t
.
setText
(
ansStr
);
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment