Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

GUI Practice Java Trivia Game Write a GUI program that lets the user play a simp

ID: 3680028 • Letter: G

Question

GUI Practice Java Trivia Game 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, and 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 the 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 which encapsulates a question, answer, and point value for a particular trivia item. You can pick the trivia questions, answers, and point values.

Serialization You probably hard-coded the trivia questions, answers, and point values for the 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. Your interface doesn't need to be GUI-based, it can be all text, but if you make a GUIbased solution you can earn up to 10 additional extra credit points!

Explanation / Answer

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TriviaQuiz extends JFrame implements ActionListe //These are class-level variables; JLabel lblQuestion; JButton btn1; JButton btn2; JButton btn3; JButton btnNext; JButton btnPrev; JLabel lblCounter; JLabel lblOutput; int counter = 0; Problem[] questionList = new Problem[5]; public static void main(String[] args){ new Quiz(); } // end main public TriviaQuiz(){ super("This is a Quiz Game"); letssetupGUI(); toregisterListeners(); loadQuestion(); toupdateScreen(); } // end constructor public void letssetupGUI(){ //create components JPanel pnlQuestion = new JPanel(); lblQuestion = new JLabel("question"); btn1 = new JButton("Answer A"); btn2 = new JButton("Answer B"); btn3 = new JButton("Answer C"); JPanel pnlControls = new JPanel(); btnPrev = new JButton(""); //add components to Panels pnlQuestion.setLayout(new GridLayout(0,1)); pnlQuestion.add(lblQuestion); pnlQuestion.add(btn1); pnlQuestion.add(btn2); pnlQuestion.add(btn3); pnlControls.setLayout(new FlowLayout()); pnlControls.add(btnPrev); pnlControls.add(lblCounter); pnlControls.add(lblOutput); pnlControls.add(btnNext); //this is to set up main layout Container mainPanel = this.getContentPane(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(pnlQuestion, BorderLayout.CENTER); mainPanel.add(pnlControls, BorderLayout.SOUTH); // this is general housekeeping this.setSize(500, 300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // end letssetupGUI public void toregisterListeners(){ //register all buttons to self btnA.addActionListener(this); btnB.addActionListener(this); btnC.addActionListener(this); btnPrev.addActionListener(this); btnNext.addActionListener(this); } // end registerListeners public void loadQuestions(){ //This is to load up questions questionList[0] = new Problem( "What is your name?", "paul”, "jacob", "rahul", "A" ); questionList[1] = new Problem( "What is color of sky?", "green", "blue", "red", "B" ); questionList[2] = new Problem( "where is new york", "UK, “AUSTRALIA", “USA", "C" ); questionList[3] = new Problem( "What is your favorite color?", "Red", "Green", "Yellow. No, blue.", "C" ); questionList[4] = new Problem( "What is the airspeed velocity of an unladen swallow?", "42 beats per second", "10 miles per hour", "African or European?", "C" ); } public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); //check all button presses and send //control to appropriate methods if (e.getSource() == btn1){ checkAns("A"); } else if (e.getSource() == btn2){ checkAns("B"); } else if (e.getSource() == btn3){ checkAns("C"); } else if (e.getSource() == btnPrev){ prevQuestion(); } else if (e.getSource() == btnNext){ nextQuestion(); } else { lblOutput.setText("eroor here.something went wrong"); } // end if } // end actionPerformed public void checkAns(String guess){ //See if user's guess is correct and //provide appropriate output String correct = probList[counter].getCorrect(); if (guess.equals(correct)){ lblOutput.setText("Who are you so wise in the ways of science?"); } else { lblOutput.setText("Nii!"); } // end if } // end checkAns public void prevQuestion(){ //This is done to back up one question if possible counter--; if (counter < 0){ counter = 0; } // end if updateScreen(); } // end prevQuestion public void nextQuestion(){ //This is done to go forward one question if possible counter++; if (counter >= probList.length){ counter = probList.length - 1; } // end if updateScreen(); } // end prevQuestion public void toupdateScreen(){ //updates screen with current problem lblCounter.setText(String.valueOf(counter)); Problem p = probList[counter]; lblQuestion.setText(p.getQuestion()); btn1.setText(p.getAnsA()); btn2.setText(p.getAnsB()); btn3.setText(p.getAnsC()); lblOutput.setText("please click on your guess"); } // end updateScreen } // end class def class final_quiz { // each field is a private instance variable private String question; private String answerA; private String answerB; private String answerC; private String correct; //constructor expects all inputs public final_quiz (String tQuestion, String tAnsA, String tAnsB, String tAnsC, String tCorrect){ //copy parameters to instance variables question = tQuestion; answerA = tAnsA; answerB = tAnsB; answerC = tAnsC; correct = tCorrect; } // end constructor //all values are read-only with getters String getQuestion(){ return question; } // end getQuestion String getAnsA(){ return ansA; } // end getAnsA String getAnsB(){ return ansB; } // end getAnsB String getAnsC(){ return ansC; } // end getAnsC String getCorrect(){ return correct; } // end getCorrect