Hw5

advertisement
HRS2422
Hw5
Directions:
 Using WinZip or a similar program, Pack (compress) all of the assignment files into an archive called
FirstName_LastNameHw5.zip.
Make sure you take a snapshot of your output run on the command prompt window (use Alt+PrintScreen for PCs) and
(Apple+Shift-3 for MACs) and save into a document called YourNameOutputRun.doc.
Include the document in your Hw4 folder before you compress it.
 Submit the zipped/compressed package by using the computer science department submission server, as follows:
a) Open My Computer and type the following URL in the Address box ftp://cs.pitt.edu/khalifa/hrs2422/Hw5
b) To complete the submission, click and drag your compressed folder then release the mouse in the submission window.
 Submission must be before the due date (by 11:59pm), otherwise, it will be considered late.
 Code must be your own (internet code is not allowed).
Do the following two programming problems
Create a simple trivia game for two players. The program will work like this:
1. Starting with Player 1, each player gets a turn answering 5 trivia questions (there are 10questions, 5 for each player). When a question is displayed, four possible answers are also
displayed. Only one of the answers is correct, and if the player selects the correct answer, he
or she earns a point.
2. After answers have been selected for all of the questions, the program displays the number of
points earned by each player and declares the player with the highest number of points the
winner.
You are to design a class called Question to hold the data for a trivia question.
The Question class should have String fields for the following data:
A trivia question
Possible answer-1
Possible answer-2
Possible answer-3
Possible answer-4
The number of the correct answers (1, 2, 3, or 4)
The Question class should have appropriate constructor(s), accessor (getter), and mutator
(setter) methods.
The program should create an array of 10 Question objects, one for each trivia question (If
you prefer, you can use an ArrayLIst instead of the array). Make up your own trivia
questions on the subject or subjects of your own choice for the objects, or use the given text
along with this hw (trvia.txt).
Suggested psuedocode for the program:
1) File: Question.java
//Question class
public class Question
{
// Constant for the number of questions
public final int NUM_QUESTIONS = 10;
// The trivia question
private String questionText;
// An array to hold possible answers.
private String possibleAnswers[] = new String[NUM_QUESTIONS];
// The number (1, 2, 3, or 4) of the correct answer.
private int correctAnswer;
//Constructor
public Question()
{
// Initialize all fields to "" or 0;
questionText = "";
correctAnswer = 0;
for (int i = 1; i <= NUM_QUESTIONS; i++)
setPossibleAnswer("", i);
}
public void setQuestion(String question)
{
questionText = //enter your code here –setup
}
public void setPossibleAnswer(String text, int num)
{
possibleAnswers[num - 1] = //enter your code here –setup
}
public void setCorrectAnswerNumber(int num)
{
correctAnswer = //enter your code here –setup
}
public String getQuestionText()
{
//enter your code here to return questionText var
}
public String getPossibleAnswer(int num)
{
//enter your code here to return possibleAnswers[num - 1]var
}
public int getCorrectAnswerNumber()
{
//enter your code here to return correctAnswer var
}
public String getCorrectAnswer()
{
//enter your code here to return possibleAnswers[correctAnswer - 1]
}
}
2) File: Player.java
public class Player
{
private int playerNumber; // The player number
private int points;
// Player's points
private int currentAnswer; // Current chosen answer
//Constructor
public Player(int playerNum)
{
playerNumber = playerNum;
points = 0;
}
public void chooseAnswer()
{
// Create a Scanner object for keyboard input.
// Get the user's chosen answer.
currentAnswer = your-key-board-input
}
public int getCurrentAnswer()
{
//enter your code here
}
public void incrementPoints()
{
//enter your code here
}
public int getPoints()
{
//enter your code here
}
}
3) File: TriviaGame.java (the driver for the code):
public class TriviaGame
{
public static void main(String args[]) throws IOException
{
// Constants
final int NUM_QUESTIONS = 10;
final int NUM_PLAYERS = 2;
// Variables
int playerTurn = 1;
//
int questionNum;
//
int playerAnswer;
//
int player1points = 0; //
int player2points = 0; //
The current player
The current question number
The player's chosen answer
Player 1's points
Player 2's points
// Create an array of Player objects for player #1 and player #2.
Player[] players = new Player[NUM_PLAYERS];
//enter code here for player1 & player2 (for loop)
for (int i = 0; i < NUM_PLAYERS; i++)
{
players[i] = new Player(i+1);
}
// Create an array to hold Question objects.
Question[] questions = new Question[NUM_QUESTIONS];
// Initialize the array with data.
initQuestions(questions);
// Play the game.
for (int i = 0; i < NUM_QUESTIONS; i++)
{
// Display the question.
//enter your code here
// Get the player's answer.
//enter your code here
// See if the correct answer was chosen.
//enter your code here
// See if the the player chose the wrong answer.
//enter your code here
// Switch players
if (playerTurn ==
playerTurn
else
playerTurn
for the next iteration.
1)
= 2;
= 1;
}
// Show the game results.
showGameResults(players);
}
/**
The initQuestions method uses the contents of the trivia.txt file
to populate the qArray parameter with Question objects. */
public static void initQuestions(Question qArray[]) throws IOException
{
// Open the trivia.txt file.
File file = new File("trivia.txt");
Scanner inputFile = new Scanner(file);
// Open the trivia.txt file.
File file = new File("trivia.txt");
Scanner inputFile = new Scanner(file);
// Populate the qArray with data from
// the file.
for (int i = 0; i < qArray.length; i++)
{
// Create a Question object in the array.
qArray[i] = new Question();
// Get the question text from the file.
qArray[i].setQuestion(inputFile.nextLine());
// Get the possible answers.
for (int j = 1; j <= 4; j++)
{
qArray[i].setPossibleAnswer(inputFile.nextLine(), j);
}
// Get the correct answer.
qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
}
}
public static void displayQuestion(Question q, int playerNum)
{
// Display the player number.
System.out.println("Question for player #" + playerNum);
System.out.println("------------------------");
// Display the question.
System.out.println(q.getQuestionText());
for (int i = 1; i <= 4; i++)
{
System.out.println(i + ". " + q.getPossibleAnswer(i));
}
}
public static void showGameResults(Player[] players)
{
// Display the stats.
System.out.println("Game Over!");
System.out.println("---------------------");
System.out.println("Player 1's points: " +
players[0].getPoints());
System.out.println("Player 2's points: " +
players[1].getPoints());
// Declare the winner.
if (players[0].getPoints() > players[1].getPoints())
System.out.println("Player 1 wins!");
else if (players[1].getPoints() > players[0].getPoints())
System.out.println("Player 2 wins!");
else
System.out.println("It's a TIE!");
}
}
4) File: trivia.txt (sample trivia questions—use your own to be more creative, or use
these questions):
(1) What famous document begins: "When in the course of human events..."?
The Gettysburg Address
The US Declaration of Independence
The Magna Carta
The US Bill of Rights
2
(2) Who said "A billion dollars isn't worth what it used to be"?
J. Paul Getty
Bill Gates
Warren Buffet
Henry Ford
1
(3) What number does "giga" stand for?
One thousand
One million
One billion
One trillion
3
(4) What number is 1 followed by 100 zeros?
A quintillion
A google
A moogle
A septaquintillion
2
(5) Which of the planets is closest in size to our moon?
Mercury
Venus
Mars
Jupiter
1
(6) What do you call a group of geese on the ground?
skein
pack
huddle
gaggle
4
(7) What do you call a group of geese in the air?
skein
pack
huddle
gaggle
1
(8) Talk show host Jerry Springer was the mayor of this city.
Chicago
Indianapolis
Cincinnati
Houston
3
(9) On a standard telephone keypad, the letters T, U, and V are matched to what number?
5
6
7
8
4
(10) Crickets hear through this part of their bodies.
Head
Knees
Ears
Tail
2
Output Sample:
Download