Create a Java application that: Implements Object serialization. Implements read
ID: 3671146 • Letter: C
Question
Create a Java application that:
Implements Object serialization.
Implements reading, writing and searching a random access file.
Implements API and user defined exceptions.
Catches and recovers from thrown exceptions
Develops a program with several classes.
Creates UML class diagrams.
Prep Reading
Absolute Java Chapters 1 - 10
Project Requirements:
In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values.
Project detailsCreate a class to administer the question bank with the following functionality:
Command line menu.
Stores question data in a Random Access File (See details of file structure below)
Enter new questions including answer and question point value. Point value should be 1 to 5.
Delete questions by searching for a question in the random access file. I would suggest assigning a question id number to each question. (hint: Static variable)
Display all of the questions, associated answers and point value.
The question bank must be stored in a binary Random Access File with the following record structure
Question (@50 bytes) – ASCII char use one byte
Answer (@20 bytes)
Question point value (an int)
HINT: Use String.format method for padding
String.format("%" + n + "s", s);
where n is number of spaces and s is the String
Create and use user defined exception to ensure question and answer do not exceed maximum number of bytes and the point value is between 1 and 5(inclusive)
For the purposes of this project the question bank will contain at least 15 questions.
Example of text menu:
Trivia Game Administration
1. List all questions
2. Delete question
3. Add question
4. Quit
Enter choice:
Create a class for the players information with the following functionality
Player’s real name
Player’s gamer nick name
Current total score. Total score is a summation of all the Player’s game sessions.
Ability to store the user object to a serialized file, the file name is the user real name such as johnsmith.dat. No need to handle duplicate file names.
Create a class for questions including
question, answer, value and a question ID
Accessor methods to return all instance fields.
Create a class for the trivia game with the following functionality.
Stores a collection of Question objects.
Using random number generation to read 5 questions from the question bank and stores them in the collection. Remember the questions are stored in a record format.
Returns the next question object in the collection.
Evaluate the answer and returns the outcome. The evaluation should ignore case.
Create a class to run the game
Command line
Ask if new or existing user.
If new user create a new user object and ask for required user information(name, nick name)
If existing user
prompt for user name
read user information from serialized file into a user object.
If file not found handle exception robustly by providing error message and asking again.
Create trivia game object
Start the game, the game sequence is
Display a question
Prompt for an answer
Evaluate the answer
Display the evaluation results
Update the users total score
Display the user current game score.
Continue to display new questions until current game is over. (5 questions)
Once game is over, allow user to play again or quit
If user quit
Display his nick name, current game score and total score.
Write or overwrite the current user data to the serialized file.
Allow another user to play or quit the program.
Explanation / Answer
COULD NOT COMPLETE DUE TO LIMITED TIME
Player.java
package question.bank;
import java.io.Serializable;
public class Player implements Serializable {
private String name;
private String nick;
private int totalScore;
public String getName() {
return name;
}
public String getNick() {
return nick;
}
public int getTotalScore() {
return totalScore;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((nick == null) ? 0 : nick.hashCode());
result = prime * result + totalScore;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Player other = (Player) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (nick == null) {
if (other.nick != null)
return false;
} else if (!nick.equals(other.nick))
return false;
if (totalScore != other.totalScore)
return false;
return true;
}
}
Question.java
package question.bank;
public class Question {
private String question;
private String answer;
private int value;
private String questionID;
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
public int getValue() {
return value;
}
public String getQuestionID() {
return questionID;
}
}
QuestionBank.java
package question.bank;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class QuestionBank {
private static RandomAccessFile raf;
private static final String FILE_NAME = "question-bank.txt";
public static void main(String[] args) {
try {
raf = new RandomAccessFile(FILE_NAME, "rw");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner scanner = new Scanner(System.in);
int response = 0;
do {
System.out.println("Trivial Game Association");
System.out.println("1. List all questions");
System.out.println("2. Delete question");
System.out.println("3. Add question");
System.out.println("4. Quit");
System.out.print("Enter choice:");
response = scanner.nextInt();
} while (response != 4);
switch (response) {
case 1:
try {
raf = new RandomAccessFile(FILE_NAME, "rw");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
readAllQuestions(raf);
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
public static String padRight(String s, int n) {
return String.format("%1$-" + n + "s", s);
}
private static void readAllQuestions(RandomAccessFile raf) {
long currentPosition = 0, endPosition = 0;
byte[] b = null;
try {
endPosition = raf.length();
raf.seek(0);
while (currentPosition < endPosition) {
raf.read(b, 0, 50);
System.out.println(b);
raf.skipBytes(28);
currentPosition = raf.getFilePointer();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}