Please help me answer this problem. I need to debug my work and I would really a
ID: 3767805 • Letter: P
Question
Please help me answer this problem. I need to debug my work and I would really appreciate a working program that I can compare it to, so please provide each of the 3 required files constructed seperately (TrivaGame, Question, PlayTiviaGame) and step by step instructions as comments. Thank you!
---------------------------------------------------------------------------------------------------------------------------------
Background Information:
For this assignment, you are going to build a program that plays a simple trivia game.. Your program will ask the user a question, obtain the answers and evaluate the answer.
The Unified Modeling Language (UML) provides a useful notation for designing and developing object-oriented software systems. One of the basic components of the UML is a class diagram, which are used to depict the attributes and behaviors of a class. A basic class diagram (as shown in the figure below) has three components. The first is the class name. The second component includes the class's attributes or fields. Each attribute is followed by a colon (:) and its data type. The third component includes the class's behaviors or methods. If the method takes parameters, their types are included in parentheses. Each behavior is also followed by a colon (:) and its return type. If the return value of a method is void, the return type can be omitted. For more information on the UML, refer to http://www.uml.org/.
Project Requirements:
1. Develop a simple TriviaGame program. We will have two classes, a TriviaGame class and a Question class. The TriviaGame class will contain several Question objects stored in an array and will have several operations. We will also have a tester class to test the PlayTriviaGame class.
2. Specific Requirements for the Question class:
a. Constant - INVALIDVALUE = -1. (see details below)
b. Instance variables
i. String question - The trivia question
ii. String answer - The answer to the question
iii. int value
iiia. the value of the question.
iiib. The value will be between 1 - 5 (inclusive) based on difficulty , 1 is easy, 5 is difficult.
iiic. Your program must ensure that the value is in the range of 1 -5, if not print out an error message and set the value to the constant INVALIDVALUE.
c. Default constructor that sets all instance variables to default values.
d. A parameterizes constructor that takes in parameters to set the class's instance variables . Ensure value is check for validity (hint call the mutator method for value and do your error handling there)
e. Accessor and mutator methods for all the instance variables (see UML Class diagram above)
f. toString() returns a nicely formatted with one attribute on each line using the ' ' escape character (see sample output below for an example).
g. The UML class diagram for the Question class looks like this:
3. Specific Requirements for theTriviaGame class:
a. Instance Variables
i. Question[] gameQuestions - size of 10.
ii. int score - the current score of the user playing the game
iii. int numberOfQuestion - the number of question used in the current game, (partially filled array value) it can not be greater than the size of the array of question. (10).
iv. int currentQuestion - the index of the current question being asked.
b. Parameterized constructor
i. Parameter - a FileInputStream object (chpt 2). The FileInputStreamObject is connected to a file that contains the 10 questions, their respective answers and values.
ii. Reads the file one line at a time, creates Question object from each line and stores the Question object in the array of questions.
iii. See the sample file posted on elearning. The file structure is one line for the question, one line for the answer and one line for the question value.
iv. sets currentQuestion instance variable to zero.
c. Methods
i. Accessor methods for score, numberOfQuestions and currentQuestion instance variable. no mutator method needed
ii. play method -Starts the game.
iia. resets the score and currentQuestion instance variable to zero.
iib. parameter - int numQuestion representing the number of question the player want to answer for this game. (Maximum of 10)
iii. nextQuestion method
iiia. Uses the currentQuestion instance variable to access the gameQuestion array.
iiib. Returns the a String representing the next question..
iv. evaluateAnswer(String answer)
iva. Compares the parameter answer with the currentQuestion answer.
ivb. Returns true if correct, false if incorrect.
ivc. Increment the currentQuestion variable.
d. toString method - prints a nicely formated string that lists all the questions in the array, their respective answers and score value.
e. The UML class diagram for the TriviaGame class looks like this:
4. Build a PlayTrivaGame class to play a game of trivia.
a. Create a FileInputStreamObject - NOTE for ease of grading, call the file used to create this object questions.dat. You can just use the sample file provide if you like.
b. Create a TriviaGame Object that passes a FileInputStreamObject that connect to the file with the questions, answers and scores.
c. Prompt the user to input how many question they would like to answer, up to a maximum of 10.
d. Calls the play method passing the number of question from item 2.
e. Loops through the follow until the number of question from item 2 are asked and answered.
i. Calls nextQuestion and displays the next question
ii. Prompts for the answer
iii. Calls evalutateAnswer method and prints message depend on evaluteAnswer results such as answer is correct, or answer is incorrect.
f. Once current game is complete calls getScore and displays the current players score.
g. Ask the user if they wish to try again and if so go back to step 3,(calling the play method).
h. If the user wishes to exit, call the Trivia game toString method and exit the program.
Question question Stringanswer: String
value: int Question()
Question(String, String, int)getQuestion(): String
getAnswer(): String
getValue(): int
setQuestion(String)
setAnswer(String)
setValue(int)
toString: String
Explanation / Answer
Question.Java
package com.rbc.game;
public class Question {
int INVALIDVALUE = -1;
String question;
String answer;
int value;
Question() {
this.question = null;
this.answer = null;
this.value = 0;
}
Question(String qtn, String ans, int val) {
this.question = qtn;
this.answer = ans;
setValue(val);
}
/**
* @return the question
*/
public String getQuestion() {
return question;
}
/**
* @param question
* the question to set
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* @return the answer
*/
public String getAnswer() {
return answer;
}
/**
* @param answer
* the answer to set
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value
* the value to set
*/
public void setValue(int value) {
int tempVal = value;
if (tempVal == 0 || tempVal > 5) {
System.out.println("The Value should be between 1 and 5");
INVALIDVALUE = value;
} else {
this.value = tempVal;
}
}
public String toString() {
return value + " " + question + " " + answer + " ";
}
}
TriviaGame.java
package com.rbc.game;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class TriviaGame {
Question[] gameQuestions = new Question[10];
int score;
int numberOfQuestion;
int currentQuestion;
TriviaGame(FileInputStream obj) {
BufferedReader br = new BufferedReader(new InputStreamReader(obj));
String StrLine = null;
int i = 0;
try {
while (null != (StrLine = br.readLine())) {
Question ques = new Question();
ques.setQuestion(StrLine);
ques.setAnswer(br.readLine());
ques.setValue(Integer.valueOf(br.readLine()));
gameQuestions[i] = ques;
i++;
StrLine = null;
}
br.close();
} catch (IOException e) {
System.out.println("Unexpected Exception while reading the file");
}
this.currentQuestion = 0;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @return the numberOfQuestion
*/
public int getNumberOfQuestion() {
return numberOfQuestion;
}
/**
* @return the currentQuestion
*/
public int getCurrentQuestion() {
return currentQuestion;
}
public void play(int numOfQtn) {
this.score = 0;
if (numOfQtn > 10) {
System.out.println("Max questions will be 10");
} else {
this.numberOfQuestion = numOfQtn;
}
}
public String nextQuestion() {
String ques = null;
if (null != gameQuestions[currentQuestion]
&& null != gameQuestions[currentQuestion].getQuestion()) {
ques = gameQuestions[currentQuestion].getQuestion();
return ques;
} else {
return ques = "Exceeded";
}
}
public boolean evaluateAnswer(String ans) {
if (gameQuestions[currentQuestion].getAnswer().equals(ans)) {
currentQuestion++;
score++;
return true;
}
currentQuestion++;
return false;
}
public String toString() {
return "Your final Score: " + score;
}
}
PlayTriviaGame.java
package com.rbc.game;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class PlayTriviaGame {
public static void main(String[] args) {
Scanner sc = null;
Scanner ans = null;
boolean result = false;
int numOfQuestions = 0;
char choice = 0;
FileInputStream fis = null;
TriviaGame triviaGame = null;
boolean notValid = false;
do {
try {
sc=new Scanner(System.in);
fis = new FileInputStream(
"C:/Users/Penchal/Desktop/Question1.txt");
triviaGame = new TriviaGame(fis);
System.out
.println("How many questions would you like to answer up to a maximum of 10:");
numOfQuestions = sc.nextInt();
do {
if (numOfQuestions <= 0 || numOfQuestions > 10) {
System.out
.println("Enter valid number of Question between 1 to 10:");
notValid = true;
System.out
.println("How many questions would you like to answer up to a maximum of 10:");
numOfQuestions = sc.nextInt();
} else {
notValid = false;
}
} while (notValid);
triviaGame.play(numOfQuestions);
do {
String question=triviaGame.nextQuestion();
if(!"Exceeded".equals(question))
{
System.out.println(question);
}else{
System.out
.println("There are No Questions in file.Only "
+ triviaGame.currentQuestion
+ " questions in file but you opted for "
+ numOfQuestions);
System.exit(0);
}
System.out.println("Enter your Answer");
ans=new Scanner(System.in);
result = triviaGame.evaluateAnswer(ans.nextLine());
if (result) {
System.out.println("Answer is correct");
} else {
System.out.println("Answer is incorrect");
}
} while (numOfQuestions != triviaGame.currentQuestion);
System.out.println(triviaGame.toString());
System.out.println("Do you Want to play again Y/N");
String tempChoice = sc.next();
choice = tempChoice.charAt(0);
fis.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Unexpected Exception in reading file");
}
} while (choice == 'y' || choice == 'Y');
sc.close();
ans.close();
System.exit(0);
}
}
Output:
How many questions would you like to answer up to a maximum of 10:
2
Who are you?
Enter your Answer
iam pavan kumar
Answer is correct
What is capital of india?
Enter your Answer
delhi new capital
Answer is correct
Your final Score: 2
Do you Want to play again Y/N
y
How many questions would you like to answer up to a maximum of 10:
11
Enter valid number of Question between 1 to 10:
How many questions would you like to answer up to a maximum of 10:
0
Enter valid number of Question between 1 to 10:
How many questions would you like to answer up to a maximum of 10:
2
Who are you?
Enter your Answer
iam pavan
Answer is incorrect
What is capital of india?
Enter your Answer
new delf
Answer is incorrect
Your final Score: 0
Do you Want to play again Y/N
n
Question1.txt
Who are you?
iam pavan kumar
1
What is capital of india?
delhi new capital
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.