Skip to content

Commit

Permalink
Questions shuffing added
Browse files Browse the repository at this point in the history
Each client now get different order of questions. Question shuffing feature added at server side.
  • Loading branch information
bhavik2936 committed Oct 28, 2019
1 parent 9ae5085 commit fc466b3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
8 changes: 5 additions & 3 deletions AttendExam.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public AttendExam() {
papers = new ListOfPapers();
loadPapers();
}

// loads the list of exam papers
public boolean loadPapers() {
ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream = null;
String fileName = "./QuestionPapers.dat";
ListOfPapers examPapers;

try {
fileInputStream = new FileInputStream(fileName);
objectInputStream = new ObjectInputStream(fileInputStream);
examPapers = (ListOfPapers) objectInputStream.readObject();
papers.resetPapers(examPapers);
papers.resetClientPapers(examPapers);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
Expand All @@ -41,6 +41,8 @@ public boolean loadPapers() {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
Expand Down
6 changes: 5 additions & 1 deletion ExamSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ boolean loadChanges() {
objectInputStream = new ObjectInputStream(fileInputStream);

examPapers = (ListOfPapers) objectInputStream.readObject();
papers.resetPapers(examPapers);
papers.resetExamSetterPapers(examPapers);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
Expand All @@ -52,6 +52,8 @@ boolean loadChanges() {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
Expand Down Expand Up @@ -105,6 +107,8 @@ boolean appendChanges() {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
Expand Down
17 changes: 15 additions & 2 deletions ListOfPapers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

public class ListOfPapers extends QuestionPaper implements Serializable {

private static final long serialVersionUID = 5940166290730324489L;
private static final long serialVersionUID = 1L;
private Vector<QuestionPaper> setOfPapers = new Vector<QuestionPaper>();

// Default Constructor
Expand Down Expand Up @@ -47,8 +47,21 @@ public QuestionPaper getPaper(int index) {
return (setOfPapers.get(index));
}

// to shuffle questions of every paper
private void shufflePapers() {
for (QuestionPaper questionPaper : setOfPapers) {
questionPaper.shufflePaper();
}
}

// reset papers while loading previous changes
protected void resetPapers(ListOfPapers examPapers) {
protected void resetExamSetterPapers(ListOfPapers examPapers) {
this.setOfPapers = examPapers.setOfPapers;
}

// reset and shuffle clients' papers only while loading previous changes
protected void resetClientPapers(ListOfPapers examPapers) {
examPapers.shufflePapers();
this.setOfPapers = examPapers.setOfPapers;
}

Expand Down
21 changes: 20 additions & 1 deletion QuestionPaper.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.io.Serializable;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;

Expand Down Expand Up @@ -27,7 +28,6 @@ public QuestionPaper(String paperName, Question[] questions) {
}

// Prints Question Paper Directly
// Under Construction
@Override
public String toString() {
StringBuffer str = new StringBuffer(paperName + "\n\n");
Expand Down Expand Up @@ -124,6 +124,25 @@ void removeQuestion(int index) {
setOfQuestions.remove(index - 1);
}

// shuffle Questions of Paper
void shufflePaper() {
Random rand = new Random();
Vector<Integer> questionVector = new Vector<Integer>();
QuestionPaper questionPaper = new QuestionPaper();

do {
int paperIndex = rand.nextInt(setOfQuestions.size());
if (!questionVector.contains(paperIndex)) {
questionVector.add(paperIndex);
}
} while (questionVector.size() < setOfQuestions.size());

for (int questionIndex : questionVector) {
questionPaper.addQuestion(setOfQuestions.get(questionIndex));
}
this.setOfQuestions = questionPaper.setOfQuestions;
}

// to take and verify the answers
int checkPaper(Vector<Integer> answers) {
int marks = 0;
Expand Down
13 changes: 7 additions & 6 deletions Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;

Expand All @@ -30,23 +28,24 @@ public boolean startServer() throws UnknownHostException, IOException {

// responsible for client connection
private boolean acceptClient() throws IOException {
System.out.println("Waiting for client to connect from " + new Date());
System.out.println();
System.out.println("Waiting for client to connect since\t" + new Date());
s = serverSocket.accept();
System.out.println("New client connected at " + new Date());
System.out.println("New client connected at\t\t\t" + new Date());
return true;
}

// responsible for distributing the paper
private boolean serveClient() throws IOException {
System.out.println("Atteding to distribute paper at" + new Date());
System.out.println("Atteding to distribute paper at\t\t" + new Date());
objos = new ObjectOutputStream(s.getOutputStream());
AttendExam attendExam = new AttendExam();
objos.writeObject(attendExam);
return true;
}

// keeps the server live
public void liveServer() throws IOException {
public void liveServer() throws IOException, InterruptedException {
while (true) {
acceptClient();
serveClient();
Expand All @@ -68,6 +67,8 @@ public static void main(String[] args) {
main(args);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("Exiting at... " + new Date());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down

0 comments on commit fc466b3

Please sign in to comment.