0% found this document useful (0 votes)
9 views

Progamming Assignment Unit 7

Uploaded by

moelin tun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Progamming Assignment Unit 7

Uploaded by

moelin tun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

File “QuestionDialog.

java”

import java.awt.event.*;

import javax.swing.*;

public class QuestionDialog extends JDialog implements ActionListener {

String answer;

public void actionPerformed(ActionEvent e) {

answer = e.getActionCommand();

dispose();

File “Question.java”

import java.awt.GridLayout;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

public abstract class Question {

static int nQuestions = 0;

static int nCorrect = 0;

QuestionDialog question;

String correctAnswer;
Question (String question){ //the constructor build a dialog with of the
//content, parameter question

this.question = new QuestionDialog();

this.question.setLayout(new GridLayout(0,1));

this.question.add(new JLabel(" "+question+ " ",JLabel.CENTER));

};

void initQuestionDialog() {

question.setModal(true);

question.pack();

question.setLocationRelativeTo(null);

};

String ask () {

this.question.setVisible(true);

return this.question.answer;

};

void check () {

String answer = ask();

if (answer.equals(correctAnswer)) {

JOptionPane.showMessageDialog(null, "Correct");

nCorrect++;

else {
JOptionPane.showMessageDialog(null, "Incorrect");

nQuestions++;

static void showResults() {

JOptionPane.showMessageDialog(null,nCorrect + " correct out of " +


nQuestions + " questions" );

File “TrueFalseQuestion.java”

import javax.swing.*;

public class TrueFalseQuestion extends Question {

public TrueFalseQuestion (String question,String answer){

super(question);

JPanel buttons = new JPanel();

addButton(buttons,"TRUE");

addButton(buttons,"FALSE");

this.question.add(buttons);

this.question.setModal(true);

this.question.pack();

this.question.setLocationRelativeTo(null);

answer = answer.toUpperCase();

if (answer.equals("T") || answer.equals("TRUE") || answer.equals("Y") ||


answer.equals("YES")) {

this.correctAnswer = "TRUE";
}

else if (answer.equals("F") || answer.equals("FALSE") || answer.equals("N") ||


answer.equals("NO")) {

this.correctAnswer = "FALSE";

};

void addButton(JPanel buttons, String label) {

JButton button = new JButton(label);

button.addActionListener(this.question);

buttons.add(button);

File “MultipleChoiceQuestion.java”
import java.awt.*;

import javax.swing.*;

public class MultipleChoiceQuestion extends Question {

MultipleChoiceQuestion(String query, String a, String b, String

c, String d, String e, String answer) {

super(query);

addChoice("A", a);

addChoice("B",b);

addChoice("C",c);

addChoice("D",d);
addChoice("E",e);

initQuestionDialog();

correctAnswer = answer.toUpperCase();

};

void addChoice(String name, String label) {

JPanel choice = new JPanel(new BorderLayout());

JButton button = new JButton(name);

button.addActionListener(question);

choice.add(button,BorderLayout.WEST);

choice.add(new JLabel(" " +label+"


",JLabel.LEFT),BorderLayout.CENTER);

question.add(choice);

You might also like