Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
Q
QuizRMI
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
diaa.hanna
QuizRMI
Commits
2613b598
Commit
2613b598
authored
Nov 16, 2025
by
drnull03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished the code might add some changes
parents
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
240 additions
and
0 deletions
+240
-0
.gitignore
.gitignore
+1
-0
pom.xml
pom.xml
+32
-0
QuizClient.java
src/main/java/com/quiz/client/QuizClient.java
+45
-0
Question.java
src/main/java/com/quiz/common/Question.java
+15
-0
QuizCallback.java
src/main/java/com/quiz/common/QuizCallback.java
+7
-0
QuizService.java
src/main/java/com/quiz/common/QuizService.java
+9
-0
StudentScore.java
src/main/java/com/quiz/common/StudentScore.java
+16
-0
QuizServer.java
src/main/java/com/quiz/server/QuizServer.java
+77
-0
AppTest.java
src/test/java/com/quiz/AppTest.java
+38
-0
No files found.
.gitignore
0 → 100644
View file @
2613b598
target/
pom.xml
0 → 100644
View file @
2613b598
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.quiz
</groupId>
<artifactId>
QuizRMI
</artifactId>
<packaging>
jar
</packaging>
<version>
1.0-SNAPSHOT
</version>
<name>
QuizRMI
</name>
<url>
http://maven.apache.org
</url>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.1
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.10.1
</version>
<configuration>
<source>
17
</source>
<!-- or 11, or 8 -->
<target>
17
</target>
<!-- same as source -->
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/quiz/client/QuizClient.java
0 → 100644
View file @
2613b598
package
com
.
quiz
.
client
;
import
com.quiz.common.*
;
import
java.rmi.registry.LocateRegistry
;
import
java.rmi.registry.Registry
;
import
java.rmi.server.UnicastRemoteObject
;
import
java.util.Scanner
;
public
class
QuizClient
extends
UnicastRemoteObject
implements
QuizCallback
{
private
QuizService
service
;
private
String
studentName
;
protected
QuizClient
(
String
name
)
throws
Exception
{
super
();
this
.
studentName
=
name
;
Registry
registry
=
LocateRegistry
.
getRegistry
(
"localhost"
,
1099
);
service
=
(
QuizService
)
registry
.
lookup
(
"QuizService"
);
service
.
registerCallback
(
studentName
,
this
);
}
@Override
public
void
updateLeaderboard
(
String
leaderboard
)
{
System
.
out
.
println
(
"\n--- Leaderboard Update ---\n"
+
leaderboard
);
}
public
void
start
()
throws
Exception
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
while
(
true
)
{
Question
q
=
service
.
getQuestion
(
studentName
);
System
.
out
.
println
(
"Question: "
+
q
.
getQuestion
());
System
.
out
.
print
(
"Your Answer: "
);
String
ans
=
sc
.
nextLine
();
String
feedback
=
service
.
submitAnswer
(
studentName
,
ans
);
System
.
out
.
println
(
feedback
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter your name: "
);
String
name
=
sc
.
nextLine
();
QuizClient
client
=
new
QuizClient
(
name
);
client
.
start
();
}
}
src/main/java/com/quiz/common/Question.java
0 → 100644
View file @
2613b598
package
com
.
quiz
.
common
;
import
java.io.Serializable
;
public
class
Question
implements
Serializable
{
private
String
question
;
private
String
answer
;
public
Question
(
String
question
,
String
answer
)
{
this
.
question
=
question
;
this
.
answer
=
answer
;
}
public
String
getQuestion
()
{
return
question
;
}
public
String
getAnswer
()
{
return
answer
;
}
}
src/main/java/com/quiz/common/QuizCallback.java
0 → 100644
View file @
2613b598
package
com
.
quiz
.
common
;
import
java.rmi.Remote
;
import
java.rmi.RemoteException
;
public
interface
QuizCallback
extends
Remote
{
void
updateLeaderboard
(
String
leaderboard
)
throws
RemoteException
;
}
src/main/java/com/quiz/common/QuizService.java
0 → 100644
View file @
2613b598
package
com
.
quiz
.
common
;
import
java.rmi.Remote
;
import
java.rmi.RemoteException
;
public
interface
QuizService
extends
Remote
{
Question
getQuestion
(
String
studentName
)
throws
RemoteException
;
String
submitAnswer
(
String
studentName
,
String
answer
)
throws
RemoteException
;
void
registerCallback
(
String
studentName
,
QuizCallback
callback
)
throws
RemoteException
;
}
src/main/java/com/quiz/common/StudentScore.java
0 → 100644
View file @
2613b598
package
com
.
quiz
.
common
;
import
java.io.Serializable
;
public
class
StudentScore
implements
Serializable
{
private
String
name
;
private
int
score
;
public
StudentScore
(
String
name
)
{
this
.
name
=
name
;
this
.
score
=
0
;
}
public
String
getName
()
{
return
name
;
}
public
int
getScore
()
{
return
score
;
}
public
void
incrementScore
()
{
score
++;
}
}
src/main/java/com/quiz/server/QuizServer.java
0 → 100644
View file @
2613b598
package
com
.
quiz
.
server
;
import
com.quiz.common.*
;
import
java.rmi.RemoteException
;
import
java.rmi.server.UnicastRemoteObject
;
import
java.rmi.registry.LocateRegistry
;
import
java.rmi.registry.Registry
;
import
java.util.*
;
public
class
QuizServer
extends
UnicastRemoteObject
implements
QuizService
{
private
Map
<
String
,
StudentScore
>
studentScores
=
new
HashMap
<>();
private
List
<
Question
>
questions
=
new
ArrayList
<>();
private
Map
<
String
,
Question
>
currentQuestions
=
new
HashMap
<>();
private
Map
<
String
,
QuizCallback
>
callbacks
=
new
HashMap
<>();
protected
QuizServer
()
throws
RemoteException
{
super
();
questions
.
add
(
new
Question
(
"What is 2 + 2?"
,
"4"
));
questions
.
add
(
new
Question
(
"Capital of France?"
,
"Paris"
));
questions
.
add
(
new
Question
(
"Java is ___?"
,
"Programming language"
));
}
@Override
public
Question
getQuestion
(
String
studentName
)
throws
RemoteException
{
studentScores
.
putIfAbsent
(
studentName
,
new
StudentScore
(
studentName
));
Question
q
=
questions
.
get
(
new
Random
().
nextInt
(
questions
.
size
()));
currentQuestions
.
put
(
studentName
,
q
);
return
q
;
}
@Override
public
String
submitAnswer
(
String
studentName
,
String
answer
)
throws
RemoteException
{
Question
q
=
currentQuestions
.
get
(
studentName
);
if
(
q
==
null
)
return
"No question assigned!"
;
String
feedback
;
if
(
q
.
getAnswer
().
equalsIgnoreCase
(
answer
.
trim
()))
{
studentScores
.
get
(
studentName
).
incrementScore
();
feedback
=
"Correct! Score: "
+
studentScores
.
get
(
studentName
).
getScore
();
}
else
{
feedback
=
"Wrong! Score: "
+
studentScores
.
get
(
studentName
).
getScore
();
}
updateLeaderboard
();
return
feedback
;
}
@Override
public
void
registerCallback
(
String
studentName
,
QuizCallback
callback
)
throws
RemoteException
{
callbacks
.
put
(
studentName
,
callback
);
}
private
void
updateLeaderboard
()
{
List
<
StudentScore
>
top
=
new
ArrayList
<>(
studentScores
.
values
());
top
.
sort
((
a
,
b
)
->
b
.
getScore
()
-
a
.
getScore
());
StringBuilder
leaderboard
=
new
StringBuilder
(
"Leaderboard:\n"
);
for
(
int
i
=
0
;
i
<
Math
.
min
(
3
,
top
.
size
());
i
++)
{
leaderboard
.
append
((
i
+
1
)
+
". "
+
top
.
get
(
i
).
getName
()
+
" - "
+
top
.
get
(
i
).
getScore
()
+
"\n"
);
}
for
(
QuizCallback
cb
:
callbacks
.
values
())
{
try
{
cb
.
updateLeaderboard
(
leaderboard
.
toString
());
}
catch
(
Exception
ignored
)
{}
}
}
public
static
void
main
(
String
[]
args
)
{
try
{
QuizServer
server
=
new
QuizServer
();
Registry
registry
=
LocateRegistry
.
createRegistry
(
1099
);
registry
.
rebind
(
"QuizService"
,
server
);
System
.
out
.
println
(
"Quiz Server is running..."
);
synchronized
(
server
)
{
server
.
wait
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
src/test/java/com/quiz/AppTest.java
0 → 100644
View file @
2613b598
package
com
.
quiz
;
import
junit.framework.Test
;
import
junit.framework.TestCase
;
import
junit.framework.TestSuite
;
/**
* Unit test for simple App.
*/
public
class
AppTest
extends
TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public
AppTest
(
String
testName
)
{
super
(
testName
);
}
/**
* @return the suite of tests being tested
*/
public
static
Test
suite
()
{
return
new
TestSuite
(
AppTest
.
class
);
}
/**
* Rigourous Test :-)
*/
public
void
testApp
()
{
assertTrue
(
true
);
}
}
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