I need help with the tester class is there a answer for this? Project Outcomes:
ID: 3695098 • Letter: I
Question
I need help with the tester class is there a answer for this?
Project Outcomes: Using the decision constructs in a Java Application Using the looping constructs in a Java Application Uses an array of objects in a Java Application Use more than one class and multiple objects in in a Java Application Preparatory Readings Absolute Java Chapters 1 - 6 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: 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. Specific Requirements for the Question class: Constant - INVALIDVALUE = -1. (see details below) Instance variables String question - The trivia question String answer - The answer to the question int value the value of the question. The value will be between 1 - 5 (inclusive) based on difficulty , 1 is easy, 5 is difficult. 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. Default constructor that sets all instance variables to default values. 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) Accessor and mutator methods for all the instance variables (see UML Class diagram above) toString() returns a nicely formatted with one attribute on each line using the ' ' escape character (see sample output below for an example). The UML class diagram for the Question class looks like this: Question question String answer: String value: int Question() Question(String, String, int)getQuestion(): String getAnswer(): String getValue(): int setQuestion(String) setAnswer(String) setValue(int) toString: String Specific Requirements for theTriviaGame class:Instance Variables Question[] gameQuestions - size of 10. int score - the current score of the user playing the game 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). int currentQuestion - the index of the current question being asked. Parameterized constructor Parameter - a FileInputStream object (chpt 2). The FileInputStreamObject is connected to a file that contains the 10 questions, their respective answers and values. Reads the file one line at a time, creates Question object from each line and stores the Question object in the array of questions. 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. sets currentQuestion instance variable to zero. Methods Accessor methods for score, numberOfQuestions and currentQuestion instance variable. no mutator method needed play method -Starts the game. resets the score and currentQuestion instance variable to zero. parameter - int numQuestion representing the number of question the player want to answer for this game. (Maximum of 10) nextQuestion method Uses the currentQuestion instance variable to access the gameQuestion array. Returns the a String representing the next question.. evaluateAnswer(String answer) Compares the parameter answer with the currentQuestion answer. Returns true if correct, false if incorrect. Increment the currentQuestion variable. toString method - prints a nicely formated string that lists all the questions in the array, their respective answers and score value. The UML class diagram for the TriviaGame class looks like this: TriviaGame gameQuestions: Question[] score: int numberOfQuestion: int. currentQuestion: int Trivia(FileInputStreamObject) getScore():int getNumberOfQuestion():int getCurrentQuestion():int play(int) nextQuestion(): String evaluateAnswer(String):boolean toString(): String Build a PlayTrivaGame class to play a game of trivia. 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. Create a TriviaGame Object that passes a FileInputStreamObject that connect to the file with the questions, answers and scores. Prompt the user to input how many question they would like to answer, up to a maximum of 10. Calls the play method passing the number of question from item 2. Loops through the follow until the number of question from item 2 are asked and answered. Calls nextQuestion and displays the next question Prompts for the answer Calls evalutateAnswer method and prints message depend on evaluteAnswer results such as answer is correct, or answer is incorrect. Once current game is complete calls getScore and displays the current players score. Ask the user if they wish to try again and if so go back to step 3,(calling the play method). If the user wishes to exit, call the Trivia game toString method and exit the program. Submission Requirements: Your project must be submitted using our standard submission procedures. You should turn in the following files for this assignment: Question.java Trivia.java PlayTriviaGame.java Remember to include javadoc for all the classes. See the Programming Project Comments Template and Java Commented Sample Code under Chapter 5 on elearning
Explanation / Answer
public class Question {
// no. of answers
public final int NoOfANSWERS =10 ;
private String textOfQuestion ;
// hoding array answers
private String fesibleRespons[] =new String [ NoOfANSWERS ] ;
// The no. (1, 2, 3, or 4) of the exact answer.
private int rightRespons ;
// class Constructor
public Question ( ) {
textOfQuestion ="" ;
rightRespons =0 ;
for( int i =1 ;i <NoOfANSWERS ;i++ )
setFesibleRespond ( "" ,i ) ;
}
public void setDebate ( String question ) {
this.textOfQuestion =question ; // Settings d questions
}
public void setFesibleRespond ( String text ,int num ) {
this.fesibleRespons [ num ] =text ; // Settings feasible Answer
}
public void setrightResponsNumber ( int num ) {
this.rightRespons =num ; // Settings exact Answer
}
public String gettextOfQuestion ( ){
return this.textOfQuestion ; // ReOccuring Question Text
}
public String getPossibleAnswer ( int num ) {
return this.fesibleRespons [ num ] ; // ReOccuring feasible Amswer
}
public int getrightResponsNumber ( ) {
return this.rightRespons ; // ReOccuring exact Answer
}
public String getrightRespons ( ) {
return this.fesibleRespons [ this.rightRespons ] ; // ReOccuring feasible Answer
}
}
--------------------------------------------------
import java.util.Scanner ;
import java.io.IOException ;
import java.io.File ;
public class PlayTriviaGame {
public static void main ( String[] args )throws IOException
{
// Constants
final int NO_OF_QUESTIONS =10 ;
final int NO_OF_PLAYERS =2 ;
int performngPlayer =1 ; //present player
int num_question ; // present question No.
int playerAnswer ; // player's selected answer
int player1points =0 ; // Player 1 points
int player2points =0 ; // Player 2 points
// build an arrayOf Player obje's 4'r player 1 & player 2
Player[] plyr =new Player [ NO_OF_PLAYERS ] ;
for( int i =0 ;i <NO_OF_PLAYERS ;i++ ) {
plyr [ i ] =new Player ( i + 1 ) ;
}
// build an array 2 pick Question objec's
Question[] qust =new Question[ NO_OF_QUESTIONS ] ;
// begining d array with information.
startQuestins ( qust ) ;
// start d game
for( int i =0 ;i <NO_OF_QUESTIONS ;i++ ) {
// show d question
PlayTriviaGame.showsQuestn ( arrayQustns [ i ] ,performngPlayer ) ;
// take d players answer
plyr[performngPlayer - 1].chooseAnswer();
// if d exact answer was selected.
if( arrayQustns [ i ].getrightResponsNumber ( ) ==plyr [ performngPlayer -1 ].getCurrentAnswer ( ) ) {
plyr [ performngPlayer - 1 ].incrementPoints ( ) ;
}
// if d player selected the incorrect answer.
// Switch players 4'r d next loop.
if( performngPlayer ==1 )
performngPlayer =2 ;
else
performngPlayer =1 ;
}
// display d game outcome
displayGameOutcomes ( plyr ) ;
}
/**The startQuestins function utilizes d contents of the trivia.txt doc 2
* occypy d arrayQustns arguments with Question object's */
public static void startQuestins ( Question arrayQustns [ ] )throws IOException
{
// reveal d trivia.txt doc
File doc =new File ( "trivia.txt" ) ;
Scanner docIp =new Scanner ( doc ) ;
// occupy d arrayQustns with info from the doc.
for( int i =0 ;i <arrayQustns.length ;i++ ){
// build a Question objet in the array.
arrayQustns [ i ] =new Question ( ) ;
// gather d question text 4'f the doc.
arrayQustns [ i ].setDebate ( docIp.nextLine ( ) ) ;
// gather d fesible outcomes.
for( int j =1 ;j <=4 ; j++ ) {
arrayQustns [ i ].setFesibleRespond ( docIp.nextLine ( ) ,j ) ;
}
// gather d exact outcome.
arrayQustns [ i ].setrightResponsNumber ( Integer.parseInt ( docIp.nextLine ( ) ) ) ;
}
}
public static void showsQuestn ( Question q ,int playerNum ) {
// show d player no.
System.out.println ( " Question 4'r player " +playerNum ) ;
System.out.println ( "**********************************" ) ;
// show d question.
System.out.println ( q.gettextOfQuestion ( ) ) ;
for( int i =1 ;i <=4 ;i++ ) {
System.out.println ( i +". " +q.getPossibleAnswer ( i ) ) ;
}
}
public static void displayGameOutcomes ( Player [ ] plyr ) {
// show the statistics.
System.out.println ( " Game Finished ! " ) ;
System.out.println ( " ************************** " ) ;
System.out.println ( " Player 1 points :" +plyr [ 0 ].getPoints ( ) ) ;
System.out.println ( " Player 2 points :" +plyr [ 1 ].getPoints ( ) ) ;
// menction d winner.
if( plyr [ 0 ].getPoints ( ) >plyr [ 1 ].getPoints ( ) )
System.out.println ( " Player 1 winer ! " ) ;
else if( plyr [ 1 ].getPoints ( ) >plyr [ 0 ].getPoints ( ) )
System.out.println ( " Player 2 winer ! " ) ;
else
System.out.println ( " It is a TIE ! " ) ;
}
}