Please help me answer this problem. I need to debug my work and I would really a
ID: 3767538 • 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 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
public List<Question> getQuestions()
{
return questions;
}
TrivaGame() throws IOException
{
questions = readQuestions();
}
class Question
{
private String question;
private List<String> possibleAnswers;
private int answer;
public String toString()
{
return "Question [question=" + question + ", possibleAnswers="+ possibleAnswers + ", answer=" + answer + "]";
}
}
class Player
{
int playerNumber;
int points;
}
Function<String, Question> mapLineToQuestion = new Function<String, Question>()
{
public Question apply(String line)
{
Question question = new Question();
List<String> questionPieces = Splitter.on("|").trimResults().omitEmptyStrings().splitToList(line);
question.question = questionPieces.get(0);
question.possibleAnswers = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(questionPieces.get(1));
question.answer = Integer.parseInt(questionPieces.get(2));
return question;
}
};
public List<Question> readQuestions() throws IOException
{
List<Question> questions = Files.lines(Paths.get("src/main/resources/com/levelup/java/exercises/beginner/trivia.txt")).map(mapLineToQuestion).collect(Collectors.toList());
return questions;
}
public static int getRandomQuestionNumber(int numberOfQuestions)
{
Random random = new Random();
OptionalInt questionNumber = random.ints(1, numberOfQuestions).findFirst();
return questionNumber.getAsInt();
}
public static void displayQuestion(Question q, int playerNum)
{
System.out.println("Question for player #" + playerNum);
System.out.println("------------------------");
System.out.println(q.question);
for (int i = 0; i < q.possibleAnswers.size(); i++)
{
System.out.println((i + 1) + ". " + q.possibleAnswers.get(i));
}
}
public static void showGameResults(Player[] players)
{
System.out.println("Game Over!");
System.out.println("---------------------");
System.out.println("Player 1's points: " + players[0].points);
System.out.println("Player 2's points: " + players[1].points);
if (players[0].points > players[1].points)
{
System.out.println("Player 1 wins!");
}
else if (players[1].points > players[0].points)
{
System.out.println("Player 2 wins!");
}
else
{
System.out.println("It's a TIE!");
}
}
static int NUMBER_OF_PLAYERS = 2;
static int NUMBER_OF_CHANCES = 5;
public static void main(String args[]) throws IOException
{
TrivaGame trivaGame = new TrivaGame();
Scanner keyboard = new Scanner(System.in);
int numberOfQuestions = trivaGame.getQuestions().size();
Player[] players = { trivaGame.new Player(), trivaGame.new Player() };
for (int x = 0; x < players.length; x++)
{
Player currentPlayer = players[x];
for (int i = 0; i < NUMBER_OF_CHANCES; i++)
{
Question question = trivaGame.getQuestions().get(getRandomQuestionNumber(numberOfQuestions));
displayQuestion(question, x + 1);
System.out.print("Enter the number of the correct answer: ");
int currentAnswer = keyboard.nextInt();
if (currentAnswer == question.answer)
{
System.out.println("Correct! ");
currentPlayer.points += 1;
}
else
{
System.out.println("Sorry, that is incorrect. The correct "+ "answer is " + question.answer + ". ");
}
}
}
keyboard.close();
showGameResults(players);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.