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

Java Project Code On Online Quiz: SUBMITTED TO - Mrs. Sonal Gehlot

This document contains the code for a Java program that creates an online quiz application. It includes code for a login page (#1), a test main class (#2), instructions for taking the quiz (#3), the quiz itself (#4), and sample quiz questions. The quiz allows the user to login, view instructions, attempt 10 multiple choice questions on space and the universe, save answers or mark for review, and view their final score.

Uploaded by

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

Java Project Code On Online Quiz: SUBMITTED TO - Mrs. Sonal Gehlot

This document contains the code for a Java program that creates an online quiz application. It includes code for a login page (#1), a test main class (#2), instructions for taking the quiz (#3), the quiz itself (#4), and sample quiz questions. The quiz allows the user to login, view instructions, attempt 10 multiple choice questions on space and the universe, save answers or mark for review, and view their final score.

Uploaded by

Priyam Mehra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA PROJECT CODE ON

ONLINE QUIZ

SUBMITTED TO Mrs. Sonal gehlot


BY- PRIYAM 16CSU267
Java PROGRAM code
#1 login page
package abcd;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class login
{ login(String str) {
JFrame f=new JFrame("Password Field");
Font font1=new Font("Bauhaus 93",Font.BOLD,28);
Font font2=new Font("Bauhaus 93",Font.BOLD,28);
Font font3=new Font("Aerial",Font.BOLD,14);
JPasswordField value=new JPasswordField();
value.setBounds(600,200,100,30);
JLabel text=new JLabel("*****WELCOME TO THE ONLINE GK QUIZ******");
text.setBounds(300,20,10000,30);
text.setFont(font1);
JLabel l=new JLabel("LOGIN HERE");
l.setBounds(525,80,200,50);
l.setFont(font2);
JLabel username=new JLabel("USERNAME");
username.setBounds(500,133,160,60);
username.setFont(font3);
JLabel password=new JLabel("PASSWORD");
password.setBounds(500,183,160,60);
password.setFont(font3);
JButton b=new JButton("LOGIN");
b.setBounds(550,250,80,30);
JTextField jtf=new JTextField();
jtf.setBounds(600,150,100,30);
f.add(value);
f.add(text);
f.add(username);
f.add(l);
f.add(password);
f.add(b);
f.add(jtf);
f.setSize(1250,450);
f.setLayout(null);
f.setVisible(true);

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name=jtf.getText();
String psd=String.valueOf(value.getPassword());
if(name.equals("16csu267")&&psd.equals("ncu"))
{
JOptionPane.showMessageDialog(jtf,"LOGGED IN SUCCESSFULLY");
new Instructions();
}
else
{
JOptionPane.showMessageDialog(jtf,"EITHER THE USERNAME OR PASSWORD IS
WRONG! TRY AGAIN");
}
}
});
}
}

#2 TEST MAIN CODE


public class TestMain
{ public static void main(String args[])
{
new login("WELCOME");
}

#3 INSTRUCTIONS CODE
package abcd;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Instructions


{ Instructions()
{ JFrame jf=new JFrame();
Font font1 = new Font("Bauhaus 93",Font.BOLD,28);
Font font2 = new Font("Bauhaus 93",Font.BOLD,28);
JLabel l=new JLabel("*****READ THE FOLLOWING INSTRUCTIONS
CAREFULLY*****");
JLabel l1,l2,l3,l4,l5,l6;
l.setBounds(330,20,1000,30);
l.setFont(font1);
l1=new JLabel("#1 This is a multiple choice questions quiz on the
topic SPACE & UNIVERSE.");
l1.setBounds(250,150,1200,30);
l1.setFont(font2);

l2=new JLabel("#2 The quiz contains 10 questions and every question


is mandatory.");
l2.setBounds(250,200,1000,30);
l2.setFont(font2);
l3=new JLabel("#3 Click on the button SAVE AND NEXT to attempt the
next question.");
l3.setBounds(250,250,1000,30);
l3.setFont(font2);

l4=new JLabel("#4 You can bookmark a question by clicking on the


FLAG button.");
l4.setBounds(250,300,1000,30);
l4.setFont(font2);

l5=new JLabel("#5 The result is visible as soon as you submit the


quiz.");
l5.setBounds(250,350,1000,30);
l5.setFont(font2);

l6=new JLabel("#6. Any question left unanswered will be marked as


wrong.");
l6.setBounds(250,400,1000,30);
l6.setFont(font2);

JButton b=new JButton("I AM READY!");


b.setBounds(650,500,220,30);
jf.add(l);
jf.add(l1);
jf.add(l2);
jf.add(l3);
jf.add(l4);
jf.add(l5);
jf.add(l6);
jf.add(b);

jf.setSize(1500,650);
jf.setLayout(null);
jf.setVisible(true);

b.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
new OnlineExam("ONLINE QUIZ");
}
}
});
}

#4 CODE FOR THE QUIZ


package abcd;

import java.awt.event.*;
import javax.swing.*;
class OnlineExam extends JFrame implements ActionListener
{ private static final long serialVersionUID = 1L;
JRadioButton jrb[]=new JRadioButton[5];
JLabel lb;
JButton b1,b2;
ButtonGroup bg;
int ctrright=0,ctrwrong=0,currentques=0,x=1,y=1,now=0;
int totalques[]=new int[10];

OnlineExam(String str)
{ super(str);
lb=new JLabel();
add(lb);
bg=new ButtonGroup();
for(int i=0;i<5;i++)
{ jrb[i]=new JRadioButton();
add(jrb[i]);
bg.add(jrb[i]);
}
b1=new JButton("SAVE AND NEXT");
b2=new JButton("MARK FOR REVIEW");
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);
add(b2);
SetQuestions();
lb.setBounds(30,40,450,20);
jrb[0].setBounds(50,80,1000,20);
jrb[1].setBounds(50,110,1000,20);
jrb[2].setBounds(50,140,1000,20);
jrb[3].setBounds(50,170,1000,20);
b1.setBounds(100,240,130,30);
b2.setBounds(300,240,150,30);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setLocation(250,100);
setVisible(true);
setSize(825,350);
}

public void actionPerformed(ActionEvent ae)


{ if(ae.getSource()==b1)
{ if(CheckQues()==true)
ctrright++;
else
if(CheckQues()==false)
ctrwrong++;
currentques++;
SetQuestions();
if(currentques==9)
{ b1.setEnabled(false);
b2.setText("SUBMIT");
}
}
if(ae.getActionCommand().equals("MARK FOR REVIEW"))
{ JButton mfr=new JButton("MARKED #Q"+x);
mfr.setBounds(600,20+30*x,200,30);
add(mfr);
mfr.addActionListener(this);
totalques[x]=currentques;
x++;
currentques++;
SetQuestions();
if(currentques==9)
b2.setText("SUBMIT");
setVisible(false);
setVisible(true);
}
for(int i=0,y=1;i<x;i++,y++)
{ if(ae.getActionCommand().equals("MARKED #Q"+y))
{ if(CheckQues())
ctrright++;
else
ctrwrong++;
now=currentques;
currentques=totalques[y];
SetQuestions();
((JButton)ae.getSource()).setEnabled(false);
currentques=now;
}
}
if(ae.getActionCommand().equals("SUBMIT"))
{ if(CheckQues())
ctrright++;
else
ctrwrong++;
currentques++;
JOptionPane.showMessageDialog(this,"*CORRECT ANSWERS="+ctrright);
JOptionPane.showMessageDialog(this,"*WRONG ANSWERS="+ctrwrong);
System.exit(0);
}
}

void SetQuestions()
{ jrb[4].setSelected(true);

if(currentques==0)
{
lb.setText("#Q1 Considering facts about Sun, mean average distance of Sun from Earth is");
jrb[0].setText("150 million Km");
jrb[1].setText("200 million Km");
jrb[2].setText("250 million Km");
jrb[3].setText("300 million Km");
}

if(currentques==1)
{
lb.setText("#Q2 Second closest dwarf planet to Sun is");
jrb[0].setText("Eris");
jrb[1].setText("Pluto");
jrb[2].setText("Saturn");
jrb[3].setText("Ceres");
}

if(currentques==2)
{
lb.setText("#Q3 Considering facts about Uranus, number of known artificial satellites 'Moon'
are");
jrb[0].setText("35");
jrb[1].setText("32");
jrb[2].setText("27");
jrb[3].setText("25");
}

if(currentques==3)
{
lb.setText("#Q4 Name of most massive dwarf planet is");
jrb[0].setText("Saturn");
jrb[1].setText("Ceres");
jrb[2].setText("Eres");
jrb[3].setText("Pluto");
}

if(currentques==4)
{
lb.setText("#Q5 In December solstice, sun shines indirectly on");
jrb[0].setText("northern hemisphere");
jrb[1].setText("southern hemisphere");
jrb[2].setText("eastern hemisphere");
jrb[3].setText("western hemisphere");
}

if(currentques==5)
{
lb.setText("#Q6 First man made craft Venera 3 was launched in");
jrb[0].setText("1966");
jrb[1].setText("1986");
jrb[2].setText("1981");
jrb[3].setText("1991");
}

if(currentques==6)
{
lb.setText("#Q7 Smallest planet in Solar System which is closest to Sun is ");
jrb[0].setText("Earth");
jrb[1].setText("Mercury");
jrb[2].setText("Venus");
jrb[3].setText("Mars");
}

if(currentques==7)
{
lb.setText("#Q8 Diameter of Earth's natural satellite Moon is");
jrb[0].setText("3575 KM");
jrb[1].setText("3475 KM");
jrb[2].setText("4475 KM");
jrb[3].setText("5575 KM");
}

if(currentques==8)
{
lb.setText("#Q9 Considering facts about Mars, orbit distance of planet Mars in terms of
Astronomical Units is");
jrb[0].setText("1.38");
jrb[1].setText("1.58");
jrb[2].setText("1.48");
jrb[3].setText("1.68");
}

if(currentques==9)
{
lb.setText("#Q10 Orbit of planet Venus around Sun is completed in");
jrb[0].setText("230 Earth days");
jrb[1].setText("225 Earth days");
jrb[2].setText("265 Earth days");
jrb[3].setText("285 Earth days");
}
lb.setBounds(30,40,450,20);
for(int i=0,j=0;i<=90;i+=30,j++)
jrb[j].setBounds(50,80+i,200,20);
}

boolean CheckQues()
{
if(currentques==0)
return(jrb[1].isSelected());

if(currentques==1)
return(jrb[2].isSelected());

if(currentques==2)
return(jrb[4].isSelected());

if(currentques==3)
return(jrb[3].isSelected());

if(currentques==4)
return(jrb[1].isSelected());

if(currentques==5)
return(jrb[1].isSelected());

if(currentques==6)
return(jrb[2].isSelected());

if(currentques==7)
return(jrb[2].isSelected());

if(currentques==8)
return(jrb[1].isSelected());

if(currentques==9)
return(jrb[2].isSelected());

return false;
}
}

You might also like