Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
homework
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hasan.slami
homework
Commits
d2096db9
Commit
d2096db9
authored
Nov 04, 2025
by
hasan.slami
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add README
parent
bd8346b3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
0 deletions
+98
-0
Main.java
HomeWork/Main.java
+98
-0
No files found.
HomeWork/Main.java
0 → 100644
View file @
d2096db9
import
java.util.Random
;
class
VaultHackingRace
{
static
class
Vault
{
private
final
int
password
;
public
Vault
()
{
this
.
password
=
new
Random
().
nextInt
(
10000
);
System
.
out
.
println
(
"Vault created with password: "
+
this
.
password
);
}
public
boolean
isCorrectPassword
(
int
guess
)
{
try
{
Thread
.
sleep
(
5
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
return
guess
==
this
.
password
;
}
}
abstract
static
class
HackerThread
extends
Thread
{
protected
Vault
vault
;
public
HackerThread
(
Vault
vault
)
{
this
.
vault
=
vault
;
this
.
setName
(
this
.
getClass
().
getSimpleName
());
this
.
setPriority
(
Thread
.
MAX_PRIORITY
);
}
@Override
public
void
start
()
{
System
.
out
.
println
(
this
.
getName
()
+
" started working"
);
super
.
start
();
}
}
static
class
AscendingHackerThread
extends
HackerThread
{
public
AscendingHackerThread
(
Vault
vault
)
{
super
(
vault
);
}
@Override
public
void
run
()
{
for
(
int
guess
=
0
;
guess
<=
9999
;
guess
++)
{
if
(
vault
.
isCorrectPassword
(
guess
))
{
System
.
out
.
println
(
this
.
getName
()
+
" won with password: "
+
guess
);
System
.
exit
(
0
);
}
}
}
}
static
class
DescendingHackerThread
extends
HackerThread
{
public
DescendingHackerThread
(
Vault
vault
)
{
super
(
vault
);
}
@Override
public
void
run
()
{
for
(
int
guess
=
9999
;
guess
>=
0
;
guess
--)
{
if
(
vault
.
isCorrectPassword
(
guess
))
{
System
.
out
.
println
(
this
.
getName
()
+
" won with password: "
+
guess
);
System
.
exit
(
0
);
}
}
}
}
static
class
PoliceThread
extends
Thread
{
@Override
public
void
run
()
{
for
(
int
i
=
10
;
i
>
0
;
i
--)
{
try
{
Thread
.
sleep
(
1000
);
System
.
out
.
println
(
"Time remaining: "
+
i
+
" seconds"
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
System
.
out
.
println
(
"Game over for you hackers!"
);
System
.
exit
(
0
);
}
}
public
static
void
main
(
String
[]
args
)
{
Vault
vault
=
new
Vault
();
AscendingHackerThread
ascendingHacker
=
new
AscendingHackerThread
(
vault
);
DescendingHackerThread
descendingHacker
=
new
DescendingHackerThread
(
vault
);
PoliceThread
police
=
new
PoliceThread
();
ascendingHacker
.
start
();
descendingHacker
.
start
();
police
.
start
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment