SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 3 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 14 Nov 2017 My Price 10.00

public class Quiz extends JFrame implements ActionListener

Hi,

   Please if you could adjust the java coding to just display the question and answer and allow the user to pick a,b, or c and if they pic the wrong answer ask the user to pick another answer until they have pick the correct one.

 

/**
 *
 * @author SOPHOL
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Quiz extends JFrame implements ActionListener {

    private static final long serialVersionUID = 4511551924301248243L;

    //class-level variables;
    private static JRadioButton answer1, answer2, answer3;
    JButton btnNext, btnPrev;
    JLabel questionLabel, outputLabel;
    JPanel questionsPanel, answersPanel, controlsPanel, outputPanel, mainPanel;
    ButtonGroup choices;

    // Button to restart the quiz session
    JButton resetButton = new JButton("Restart Quiz");

    int counter = 0;
    int numOfCorrectAnswers = 0;
    // Array of problems
    Problem[] probList = new Problem[10];

    // Boolean array to saved answered questions
    // This is to ensure user doesn't answer a question twice
    Boolean[] isAnswered = new Boolean[10];

    public static void main(String[] args) {
        new Quiz();
    } // end main

    public Quiz() {
        super("Quiz Game");
        loadProblems();
        setupGUI();
        registerListeners();
        updateScreen();
    } // end constructor

    public void setupGUI() {
        // Set frame title, size and location
        setTitle("Multiple Choice Questions");
        setSize(500, 500);
        setLocationRelativeTo(null);

        //create components
        mainPanel = new JPanel();
        questionsPanel = new JPanel();
        answersPanel = new JPanel();
        controlsPanel = new JPanel();

        mainPanel.setSize(400, 400);
        // set mainPanel layout to GridBagLayout
        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        btnPrev = new JButton("<< previous");
        outputLabel = new JLabel("Please click on your answer");
        btnNext = new JButton("next >>");

        //add components to Panels
        questionLabel = new JLabel((counter + 1) + ". " + probList[counter].getQuestion());
        questionsPanel.setLayout(new GridLayout(0, 1, 20, 20));
        questionsPanel.add(questionLabel);

        // RadioButtons        
        answer1 = new JRadioButton(probList[counter].getAnsA());
        answer2 = new JRadioButton(probList[counter].getAnsB());
        answer3 = new JRadioButton(probList[counter].getAnsC());

        choices = new ButtonGroup();
        answersPanel.setLayout(new GridLayout(3, 1, 20, 20));
        // Add the radio buttons to choices ButtonGroup
        choices.add(answer1);
        choices.add(answer2);
        choices.add(answer3);
        answersPanel.add(answer1);
        answersPanel.add(answer2);
        answersPanel.add(answer3);

        controlsPanel = new JPanel();
        controlsPanel.setLayout(new GridLayout(1, 1, 20, 20));
        controlsPanel.add(btnPrev);
        controlsPanel.add(btnNext);

        // Add components to mainPanel
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.ipadx = 150;
        gbc.ipady = 10;
        mainPanel.add(questionsPanel, gbc);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.ipadx = 150;
        gbc.ipady = 80;
        mainPanel.add(answersPanel, gbc);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.ipadx = 50;
        gbc.ipady = 50;
        mainPanel.add(outputLabel, gbc);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.ipadx = 50;
        gbc.ipady = 20;
        mainPanel.add(controlsPanel, gbc);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.ipadx = 50;
        gbc.ipady = 20;
        //top padding
        gbc.insets = new Insets(20, 0, 0, 0);
        mainPanel.add(resetButton, gbc);

        // mainPanel to frame
        add(mainPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } // end setupGUI

    public void registerListeners() {
        //register all buttons to self
        answer1.addActionListener(this);
        answer2.addActionListener(this);
        answer3.addActionListener(this);
        btnPrev.addActionListener(this);
        btnNext.addActionListener(this);
        resetButton.addActionListener(this);
    } // end registerListeners

    public void loadProblems() {
        //load up probList array with data

        probList[0] = new Problem(
                "What is the Happiest Place on Earth?",
                "Legoland",
                "Universal Studios",
                "Disney World",
                "C"
        );
        probList[1] = new Problem(
                "Who is my favorite Disney Character?",
                "Mickey Mouse",
                "Minnie Mouse",
                "Donald Duck",
                "C"
        );
        probList[2] = new Problem(
                "What is Goofy son's name?",
                "Pluto",
                "Max",
                "Shrek",
                "B"
        );
        probList[3] = new Problem(
                "Who is the most popular Disney Character?",
                "Minnie Mouse",
                "Daisy Duck",
                "Mickey Mouse",
                "C"
        );
        probList[4] = new Problem(
                "what is my favorite video game?",
                "Super Mario Bro",
                "Duck Hunt",
                "King of Fighters",
                "C"
        );
        probList[5] = new Problem(
                "Who is my favorite video game figher?",
                "Ryu",
                "Kyo",
                "Akuma",
                "B"
        );
        probList[6] = new Problem(
                "Who is my favorite singer?",
                "Paula Abdul",
                "Michael Jackson",
                "Selena",
                "B"
        );
        probList[7] = new Problem(
                "What is my favorite actvity?",
                "Photography",
                "Exercising",
                "Cooking",
                "A"
        );
        probList[8] = new Problem(
                "What is my favorite movie?",
                "Cars",
                "Despicable Me",
                "Star Wars",
                "B"
        );
        probList[9] = new Problem(
                "What is my favorite season?",
                "Fall",
                "Spring",
                "Winter",
                "B"
        );
    } // end loadProblems

    @Override
    public void actionPerformed(ActionEvent e) {
        //check all button presses and send control to appropriate methods
        if (e.getSource() == answer1) {
            checkAns("A");
        } else if (e.getSource() == answer2) {
            checkAns("B");
        } else if (e.getSource() == answer3) {
            checkAns("C");
        } else if (e.getSource() == btnPrev) {
            prevQuestion();
            // Unselect previously selected radio button
            choices.clearSelection();
        } else if (e.getSource() == btnNext) {
            nextQuestion();
            // Unselect previously selected radio button
            choices.clearSelection();
        } else if (e.getSource() == resetButton) {

            this.dispose();
            // Call constructor
            new Quiz();
        } else {
            outputLabel.setText("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)) {
            outputLabel.setText("Correct");
            // Only increase number of correct answers if user has selected the
            // answer once
            if (isAnswered[counter] == null) {
                // set question as answered
                isAnswered[counter] = true;
                numOfCorrectAnswers++;
            }
        } else {
            outputLabel.setText("Wrong! The correct answer is: " + correct);
            // set question as answered
            isAnswered[counter] = true;
        } // end if
    } // end checkAns

    public void prevQuestion() {
        //back up one question if possible
        counter--;
        if (counter < 0) {
            counter = 0;
        } // end if
        updateScreen();
    } // end prevQuestion

    public void nextQuestion() {
        //go forward one question if possible
        counter++;
        if (counter >= probList.length) {
            // Display results in a dialog box
            showResults();
        } // end if
        else {
            updateScreen();
        }
    } // end prevQuestion

    public void updateScreen() {
        Problem p = probList[counter];
        questionLabel.setText((counter + 1) + ". " + p.getQuestion());
        answer1.setText("A. " + p.getAnsA());
        answer2.setText("B. " + p.getAnsB());
        answer3.setText("C. " + p.getAnsC());
        outputLabel.setText("Please select your guess");
    } // end updateScreen

    public void showResults() {
        questionLabel.setText("QUIZ REPORT:");
        // Display results
        outputLabel.setText("Total Correct Answers: " + numOfCorrectAnswers
                + "  Total Wrong Answers: " + (probList.length - numOfCorrectAnswers));
        // Hide answersPanel and controlsPanel
        answersPanel.setVisible(false);
        controlsPanel.setVisible(false);
    } // end showResults()

} // end class def

class Problem {

    // each field is a private instance variable
    private final String question;
    private final String ansA;
    private final String ansB;
    private final String ansC;
    private final String correct;

    //constructor expects all inputs
    public Problem(String tQuestion, String tAnsA, String tAnsB,
            String tAnsC, String tCorrect) {
        //copy parameters to instance variables
        question = tQuestion;
        ansA = tAnsA;
        ansB = tAnsB;
        ansC = tAnsC;
        correct = tCorrect;
    } // end problem 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

} // end Problem class def


Answers

(5)
Status NEW Posted 14 Nov 2017 07:11 AM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)