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

CODE

This document contains code for a quiz application in Java with three classes - Login, Rules, and Quiz. The Login class creates a login frame that allows the user to enter their name and includes buttons to view the rules or go back. The Rules class displays the game rules to the user. It takes the user name as a parameter and includes buttons to start the quiz or go back. The Quiz class contains the main quiz functionality - it stores the questions and answers, displays one at a time, and includes buttons to navigate between questions or submit answers. It tracks the user's score and uses timers.

Uploaded by

Aditya Slathia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

CODE

This document contains code for a quiz application in Java with three classes - Login, Rules, and Quiz. The Login class creates a login frame that allows the user to enter their name and includes buttons to view the rules or go back. The Rules class displays the game rules to the user. It takes the user name as a parameter and includes buttons to start the quiz or go back. The Quiz class contains the main quiz functionality - it stores the questions and answers, displays one at a time, and includes buttons to navigate between questions or submit answers. It tracks the user's score and uses timers.

Uploaded by

Aditya Slathia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CODE

Login.java

package quizapllication;

import java.awt.Color;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Login extends JFrame implements ActionListener {

JButton Rules, Back;


JTextField tfname;
JPasswordField passwordField;

Login() {
getContentPane().setBackground(Color.WHITE);
setLayout(null);
ImageIcon i1 = new
ImageIcon(ClassLoader.getSystemResource("icons/Login.jpeg"));
JLabel image = new JLabel(i1);
image.setBounds(0, 0, 600, 500);
add(image);

JLabel heading = new JLabel("Simple Minds");


heading.setBounds(750, 60, 300, 45);
heading.setFont(new Font("Viner Hand ITC", Font.BOLD, 28));
heading.setForeground(Color.BLUE);
add(heading);
JLabel name = new JLabel("Enter Your Name");
name.setBounds(775, 150, 300, 20);
name.setFont(new Font("Mongolian Baiti", Font.BOLD, 18));
name.setForeground(Color.BLUE);
add(name);

passwordField = new JPasswordField(); // Use JPasswordField


passwordField.setBounds(735, 200, 300, 25);
passwordField.setFont(new Font("Times New Roman", Font.BOLD, 20));
add(passwordField);

tfname = new JTextField();


tfname.setBounds(735, 200, 300, 25);
tfname.setFont(new Font("Times New Roman", Font.BOLD, 20));
add(tfname);

Rules = new JButton("Rules");


Rules.setBounds(735, 270, 120, 25);
Rules.setBackground(Color.BLUE);
Rules.setForeground(Color.WHITE);
Rules.addActionListener(this);
add(Rules);

Back = new JButton("Back");


Back.setBounds(915, 270, 120, 25);
Back.setBackground(Color.BLUE);
Back.setForeground(Color.WHITE);
Back.addActionListener(this);
add(Back);

setSize(1200, 500);
setLocation(340, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == Rules) {
String name = tfname.getText();
setVisible(false);
new Rules(name);

} else if (ae.getSource() == Back) {


setVisible(false);

public static void main(String[] args) {


new Login();
}
}

Rules.java

package quizapllication;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Rules extends JFrame implements ActionListener{

String name;
JButton Start , Back;

public Rules(String name) {


this.name = name;
getContentPane().setBackground(Color.WHITE);
setLayout(null);

JLabel heading = new JLabel("Welcome " + name + " to Simple Minds");


heading.setBounds(50, 10, 800, 45);
heading.setFont(new Font("Viner Hand ITC", Font.BOLD, 28));
heading.setForeground(Color.BLUE);
add(heading);

JLabel Rules = new JLabel();


Rules.setBounds(50, 40, 1000, 355);
Rules.setFont(new Font("Tahoma", Font.PLAIN, 16));
Rules.setText(
"<html>"+
"1. Read the instructions thoroughly" + "<br><br>" +
"2. Determine what the question is asking by identifying key words in
the stem" + "<br><br>" +
"3. Try to answer the question before looking at the answers, but make
sure you read every answer before choosing" + "<br><br>" +
"4. Eliminate options you know to be incorrect." + "<br><br>" +
"5. Translate double negatives into positive statements." + "<br><br>"
+
"6. Do not keep changing your answers. Stick with your first impression
unless you misread the question the first time. " + "<br><br>" +
"7. Use exam questions to help you answer other exam questions.
Correlating information between questions may assist you in finding the right
answers." + "<br><br>" +
"8. Overall, remember that you are looking for the BEST possible
answer provided among the options" + "<br><br>" +
"<html>"
);
add(Rules);

Back = new JButton("Back");


Back.setBounds(250, 500, 100, 30);
Back.setBackground(Color.BLUE);
Back.setForeground(Color.WHITE);
Back.addActionListener(this);
add(Back);
Start = new JButton("Start");
Start.setBounds(400, 500, 100, 30);
Start.setBackground(Color.BLUE);
Start.setForeground(Color.WHITE);
Start.addActionListener(this);
add(Start);

setSize(1080, 650);
setLocation(350, 150);
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {


if (ae.getSource() == Start){
setVisible(false);
new Quiz(name);

} else {
setVisible(false);
new Login();
}
}

public static void main(String[] args) {


new Rules("User");
}

}
Quiz.java

package quizapllication;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Quiz extends JFrame implements ActionListener {

String questions[][] = new String[10][5];


String answers[][] = new String[10][2];
String useranswers[][] = new String[10][1];
JLabel qno, question;
JRadioButton opt1, opt2, opt3, opt4;
ButtonGroup groupoptions;
JButton next, submit, lifeline;

public static int timer = 15;


public static int ans_given = 0;
public static int count = 0;
public static int score = 0;

String name;

Quiz(String name) {
this.name = name;
setBounds(50, 0, 1440, 850);
getContentPane().setBackground(Color.WHITE);
setLayout(null);

ImageIcon i1 = new
ImageIcon(ClassLoader.getSystemResource("icons/quiz.jpg"));
JLabel image = new JLabel(i1);
image.setBounds(0, 0, 1440, 392);
add(image);

qno = new JLabel();


qno.setBounds(100, 450, 50, 30);
qno.setFont(new Font("Tahoma", Font.PLAIN, 24));
add(qno);

question = new JLabel();


question.setBounds(150, 450, 900, 30);
question.setFont(new Font("Tahoma", Font.PLAIN, 24));
add(question);

questions[0][0] = "Which is used to find and fix bugs in the Java


programs.?";
questions[0][1] = "JVM";
questions[0][2] = "JDB";
questions[0][3] = "JDK";
questions[0][4] = "JRE";

questions[1][0] = "What is the return type of the hashCode() method in the


Object class?";
questions[1][1] = "int";
questions[1][2] = "Object";
questions[1][3] = "long";
questions[1][4] = "void";

questions[2][0] = "Which package contains the Random class?";


questions[2][1] = "java.util package";
questions[2][2] = "java.lang package";
questions[2][3] = "java.awt package";
questions[2][4] = "java.io package";

questions[3][0] = "An interface with no fields or methods is known as?";


questions[3][1] = "Runnable Interface";
questions[3][2] = "Abstract Interface";
questions[3][3] = "Marker Interface";
questions[3][4] = "CharSequence Interface";

questions[4][0] = "In which memory a String is stored, when we create a


string using new operator?";
questions[4][1] = "Stack";
questions[4][2] = "String memory";
questions[4][3] = "Random storage space";
questions[4][4] = "Heap memory";

questions[5][0] = "Which of the following is a marker interface?";


questions[5][1] = "Runnable interface";
questions[5][2] = "Remote interface";
questions[5][3] = "Readable interface";
questions[5][4] = "Result interface";

questions[6][0] = "Which keyword is used for accessing the features of a


package?";
questions[6][1] = "import";
questions[6][2] = "package";
questions[6][3] = "extends";
questions[6][4] = "export";

questions[7][0] = "In java, jar stands for?";


questions[7][1] = "Java Archive Runner";
questions[7][2] = "Java Archive";
questions[7][3] = "Java Application Resource";
questions[7][4] = "Java Application Runner";

questions[8][0] = "Which of the following is a mutable class in java?";


questions[8][1] = "java.lang.StringBuilder";
questions[8][2] = "java.lang.Short";
questions[8][3] = "java.lang.Byte";
questions[8][4] = "java.lang.String";

questions[9][0] = "Which of the following option leads to the portability


and security of Java?";
questions[9][1] = "Bytecode is executed by JVM";
questions[9][2] = "The applet makes the Java code secure and portable";
questions[9][3] = "Use of exception handling";
questions[9][4] = "Dynamic binding between objects";

answers[0][1] = "JDB";
answers[1][1] = "int";
answers[2][1] = "java.util package";
answers[3][1] = "Marker Interface";
answers[4][1] = "Heap memory";
answers[5][1] = "Remote interface";
answers[6][1] = "import";
answers[7][1] = "Java Archive";
answers[8][1] = "java.lang.StringBuilder";
answers[9][1] = "Bytecode is executed by JVM";

opt1 = new JRadioButton();


opt1.setBounds(170, 520, 700, 30);
opt1.setBackground(Color.WHITE);
opt1.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt1);

opt2 = new JRadioButton();


opt2.setBounds(170, 560, 700, 30);
opt2.setBackground(Color.WHITE);
opt2.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt2);

opt3 = new JRadioButton();


opt3.setBounds(170, 600, 700, 30);
opt3.setBackground(Color.WHITE);
opt3.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt3);

opt4 = new JRadioButton();


opt4.setBounds(170, 640, 700, 30);
opt4.setBackground(Color.WHITE);
opt4.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt4);

groupoptions = new ButtonGroup();


groupoptions.add(opt1);
groupoptions.add(opt2);
groupoptions.add(opt3);
groupoptions.add(opt4);
next = new JButton("Next");
next.setBounds(1100, 550, 200, 40);
next.setFont(new Font("Tahoma", Font.PLAIN, 22));
next.setBackground(new Color(30, 144, 255));
next.setForeground(Color.WHITE);
next.addActionListener(this);
add(next);

lifeline = new JButton("50-50 Lifeline");


lifeline.setBounds(1100, 630, 200, 40);
lifeline.setFont(new Font("Tahoma", Font.PLAIN, 22));
lifeline.setBackground(new Color(30, 144, 255));
lifeline.setForeground(Color.WHITE);
lifeline.addActionListener(this);
add(lifeline);

submit = new JButton("Submit");


submit.setBounds(1100, 710, 200, 40);
submit.setFont(new Font("Tahoma", Font.PLAIN, 22));
submit.setBackground(new Color(30, 144, 255));
submit.setForeground(Color.WHITE);
submit.addActionListener(this);
submit.setEnabled(false);
add(submit);

start(count);

setVisible(true);
}

public void actionPerformed(ActionEvent ae) {


if (ae.getSource() == next) {
repaint();
opt1.setEnabled(true);
opt2.setEnabled(true);
opt3.setEnabled(true);
opt4.setEnabled(true);

ans_given = 1;
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] =
groupoptions.getSelection().getActionCommand();
}

if (count == 8) {
next.setEnabled(false);
submit.setEnabled(true);
}

count++;
start(count);
} else if (ae.getSource() == lifeline) {
if (count == 2 || count == 4 || count == 6 || count == 8 || count == 9) {
opt2.setEnabled(false);
opt3.setEnabled(false);
} else {
opt1.setEnabled(false);
opt4.setEnabled(false);
}
lifeline.setEnabled(false);
} else if (ae.getSource() == submit) {
ans_given = 1;
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] =
groupoptions.getSelection().getActionCommand();
}

for (int i = 0; i < useranswers.length; i++) {


if (useranswers[i][0].equals(answers[i][1])) {
score += 10;
} else {
score += 0;
}
}
setVisible(false);
new Score(name, score);
}
}

public void repaint(Graphics g) {


super.paint(g);

String time = "Time left - " + timer + " seconds"; // 15


g.setColor(Color.RED);
g.setFont(new Font("Tahoma", Font.BOLD, 25));

if (timer > 0) {
g.drawString(time, 1100, 500);
} else {
g.drawString("Times up!!", 1100, 500);
}

timer--; // 14

try {
Thread.sleep(1000);
repaint();
} catch (Exception e) {
e.printStackTrace();
}

if (ans_given == 1) {
ans_given = 0;
timer = 15;
} else if (timer < 0) {
timer = 15;
opt1.setEnabled(true);
opt2.setEnabled(true);
opt3.setEnabled(true);
opt4.setEnabled(true);

if (count == 8) {
next.setEnabled(false);
submit.setEnabled(true);
}
if (count == 9) { // submit button
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] =
groupoptions.getSelection().getActionCommand();
}

for (int i = 0; i < useranswers.length; i++) {


if (useranswers[i][0].equals(answers[i][1])) {
score += 10;
} else {
score += 0;
}
}
setVisible(false);
new Score(name, score);
} else { // next button
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] =
groupoptions.getSelection().getActionCommand();
}
count++; // 0 // 1
start(count);
}
}

public void start(int count) {


qno.setText("" + (count + 1) + ". ");
question.setText(questions[count][0]);
opt1.setText(questions[count][1]);
opt1.setActionCommand(questions[count][1]);

opt2.setText(questions[count][2]);
opt2.setActionCommand(questions[count][2]);

opt3.setText(questions[count][3]);
opt3.setActionCommand(questions[count][3]);

opt4.setText(questions[count][4]);
opt4.setActionCommand(questions[count][4]);

groupoptions.clearSelection();
}

public static void main(String[] args) {


new Quiz("User");
}
}

Score.java

package quizapllication;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Score extends JFrame implements ActionListener {

Score(String name, int score) {


setBounds(400, 150, 750, 550);
getContentPane().setBackground(Color.WHITE);
setLayout(null);

ImageIcon i1 = new
ImageIcon(ClassLoader.getSystemResource("icons/score.png"));
Image i2 = i1.getImage().getScaledInstance(300, 250,
Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel image = new JLabel(i3);
image.setBounds(0, 200, 300, 250);
add(image);

JLabel heading = new JLabel("Thankyou " + name + " for playing Simple
Minds");
heading.setBounds(45, 30, 700, 30);
heading.setFont(new Font("Tahoma", Font.PLAIN, 26));
add(heading);

JLabel lblscore = new JLabel("Your score is " + score);


lblscore.setBounds(350, 200, 300, 30);
lblscore.setFont(new Font("Tahoma", Font.PLAIN, 26));
add(lblscore);

JButton submit = new JButton("Play Again");


submit.setBounds(380, 270, 120, 30);
submit.setBackground(new Color(30, 144, 255));
submit.setForeground(Color.WHITE);
submit.addActionListener(this);
add(submit);

setVisible(true);
}

public void actionPerformed(ActionEvent ae) {


setVisible(false);
new Login();
}

public static void main(String[] args) {


new Score("User", 0);
}
}

You might also like