(need help with part 3) Write a GUI program that lets the user play a simple tri
ID: 3680503 • Letter: #
Question
(need help with part 3)
Write a GUI program that lets the user play a simple trivia game. Use a layout of your choice with the appropriate text fields, labels, a nd buttons to implement your design. The game should ask only one question at a time and output the correct answer to a GUI component (e.g. label, textfield, etc.) if the player answers a question incorrectly. When all questions have been answered, show th e final score and exit the program. The game should have five questions. Each question should have a corresponding answer and point value between 1 and 3 based on the difficulty of the question. Implement the game using a single array of a Trivia object wh ich encapsulates a question, answer, and point value for a particular trivia item. You can pick the trivia questions, answers, and point values.
3) (10 points) Serialization You probably hard - coded the trivia questions, answers, and point values for th e previous problem. For this problem, write an administrative interface so someone can add or delete questions/answers/values. It is your option if you want to be able to edit existing questions. Your administrative program should save and load data using serialization with binary output and input. Modify your program from question 2 so that it loads the data file saved via serialization.
Explanation / Answer
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TriviaGame
{
public static void main(String[] args)
{
TriviaGame triviaGame = new TriviaGame();
MyWindow w = new MyWindow(Color.BLUE);
w.setVisible(true);
MyWindow c = new MyWindow(Color.RED);
w.setVisible(true);
}
static void Question(EndingListener.Question question, int playerTurn)
{
throw new UnsupportedOperationException("Not yet implemented");
}
}
class MyWindow extends JFrame {
public MyWindow(Color theColor) {
super();
setSize(300, 200);
setTitle("Trivia Quest");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
getContentPane().setBackground(theColor);
JButton endButton = new JButton("Click to end program.");
endButton.addActionListener((ActionListener) new EndingListener());
add(endButton);
}
}
class EndingListener implements ActionListener
{
private class NUM_PLAYER {
public NUM_PLAYER() {
}
}
private class i<T> {
public i() {
}
}
private class qArray {
public qArray() {
}
}
public void Action(ActionEvent e)
{
System.exit(0);
final int NUM_QUESTIONS = 5;
final int NUM_PLAYERS = 1;
int playerTurn = 1;
int questionNum;
int playerAnswer;
int player1points = 0;
Player[] p = new Player[NUM_PLAYERS];
Player[] player = null;
for (int i = 0; i < NUM_PLAYERS; i++)
{
}
Question[] questions = new Question[NUM_QUESTIONS];
Question[] qArray = null;
for (int i = 0; i < NUM_QUESTIONS; i++) {
TriviaGame.Question(qArray[i], playerTurn);
player[playerTurn - 1].chooseAnswer();
if (qArray[i].getCorrectAnswerNumber()
== player[playerTurn - 1].getCurrentAnswer()) {
player[playerTurn - 1].incrementPoints();
}
}
class Init
{
File file = new File("trivia.txt");
Scanner inputFile = null;
try {
inputFile = new Scanner(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(EndingListener.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < qArray.length; i++) {
qArray[i].setQuestion(inputFile.nextLine());
for (int j = 1; j <= 4; j++) {
qArray[i].setPossibleAnswer(inputFile.nextLine(), j);
}
qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
}
}
public void Question(Question q, int playerNum)
{
System.out.println("Question for player #" + playerNum);
System.out.println("------------------------");
System.out.println(q.getQuestionText());
for (int i = 1; i <= 4; i++) {
System.out.println(i + ". " + q.getPossibleAnswer(i));
}
}
public void Result(Player[] players) {
System.out.println("Game Over!");
System.out.println("Player's points: " + players[0].getPoints());
if (players[0].getPoints() > players[1].getPoints()) {
System.out.println("Player wins!");
}
}
public final class Question
{
public final int NUM_ANSWERS = 5;
private String questionText;
private String possibleAnswers[] = new String[NUM_ANSWERS];
private int correctAnswer;
public Question() {
questionText = "";
correctAnswer = 0;
for (int i = 1; i < NUM_ANSWERS; i++)
{
setPossibleAnswer("", i);
}
}
public void setQuestion(String question)
{
this.questionText = question;
}
public void setPossibleAnswer(String text, int num)
{
this.possibleAnswers[num] = text;
}
public void setCorrectAnswerNumber(int num) {
this.correctAnswer = num;
}
public String getQuestionText()
{
return this.questionText;
}
public String getPossibleAnswer(int num)
{
return this.possibleAnswers[num];
}
public int getCorrectAnswerNumber()
{
return this.correctAnswer;
}
public String getCorrectAnswer()
{
return this.possibleAnswers[this.correctAnswer];
}
}
public class Player
{
private int playerNumber;
private int points;
private int currentAnswer;
public Player(int playerNum) {
playerNumber = playerNum;
points = 0;
}
public void chooseAnswer()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your Answer");
this.currentAnswer = keyboard.nextInt();
}
public int getCurrentAnswer()
{
return this.currentAnswer;
}
public void incrementPoints()
{
this.points++;
}
public int getPoints()
{
return this.points;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.