import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MultipleChoice1 extends Applet implements ItemListener
{
String question1 = new String("1. 7+2 = ?  a.6 b.8 c.9 d.10");
String question2 = new String("2. 9-5 = ?  a.2 b.4 c.6 d.8");
String question3 = new String("3. 144/12 = ?  a.12 b.10 c.14 d.16");
String question4 = new String("4. 5*5 = ?  a.10 b.20 c.25 d.125");
String question5 = new String("5. 2+3+5 = ?  a.7 b.8 c.9 d.10");

Font regFont = new Font("Arial", Font.BOLD,16);

CheckboxGroup ques1Grp = new CheckboxGroup();
Checkbox a1 = new Checkbox("6",false,ques1Grp);
Checkbox b1 = new Checkbox("8",false,ques1Grp);
Checkbox c1 = new Checkbox("9",false,ques1Grp);
Checkbox d1 = new Checkbox("10",false,ques1Grp);

CheckboxGroup ques2Grp = new CheckboxGroup();
Checkbox a2 = new Checkbox("2",false,ques2Grp);
Checkbox b2 = new Checkbox("4",false,ques2Grp);
Checkbox c2 = new Checkbox("6",false,ques2Grp);
Checkbox d2 = new Checkbox("8",false,ques2Grp);

CheckboxGroup ques3Grp = new CheckboxGroup();
Checkbox a3 = new Checkbox("12",false,ques3Grp);
Checkbox b3 = new Checkbox("10",false,ques3Grp);
Checkbox c3 = new Checkbox("14",false,ques3Grp);
Checkbox d3 = new Checkbox("16",false,ques3Grp);

CheckboxGroup ques4Grp = new CheckboxGroup();
Checkbox a4 = new Checkbox("10",false,ques4Grp);
Checkbox b4 = new Checkbox("20",false,ques4Grp);
Checkbox c4 = new Checkbox("25",false,ques4Grp);
Checkbox d4 = new Checkbox("125",false,ques4Grp);

CheckboxGroup ques5Grp = new CheckboxGroup();
Checkbox a5 = new Checkbox("7",false,ques5Grp);
Checkbox b5 = new Checkbox("8",false,ques5Grp);
Checkbox c5 = new Checkbox("9",false,ques5Grp);
Checkbox d5 = new Checkbox("10",false,ques5Grp);

public void init()
{
add(a1);
a1.addItemListener(this);
add(b1);
b1.addItemListener(this);
add(c1);
c1.addItemListener(this);
add(d1);
d1.addItemListener(this);

add(a2);
a2.addItemListener(this);
add(b2);
b2.addItemListener(this);
add(c2);
c2.addItemListener(this);
add(d2);
d2.addItemListener(this);

add(a3);
a3.addItemListener(this);
add(b3);
b3.addItemListener(this);
add(c3);
c3.addItemListener(this);
add(d3);
d3.addItemListener(this);

add(a4);
a4.addItemListener(this);
add(b4);
b4.addItemListener(this);
add(c4);
c4.addItemListener(this);
add(d4);
d4.addItemListener(this);

add(a5);
a5.addItemListener(this);
add(b5);
b5.addItemListener(this);
add(c5);
c5.addItemListener(this);
add(d5);
d5.addItemListener(this);
}
public void paint(Graphics gr)
{
gr.fillRect(20,20,800,800);
setBackground(Color.pink);
gr.setFont(regFont);
gr.setColor(Color.pink);

gr.drawString(question1,250,100);
a1.setLocation(250,114);
b1.setLocation(280,114);
c1.setLocation(310,114);
d1.setLocation(340,114);

gr.drawString(question2,250,200);
a2.setLocation(250,214);
b2.setLocation(280,214);
c2.setLocation(310,214);
d2.setLocation(340,214);

gr.drawString(question3,250,300);
a3.setLocation(250,314);
b3.setLocation(280,314);
c3.setLocation(310,314);
d3.setLocation(340,314);

gr.drawString(question4,250,400);
a4.setLocation(250,414);
b4.setLocation(280,414);
c4.setLocation(310,414);
d4.setLocation(340,414);

gr.drawString(question5,250,500);
a5.setLocation(250,514);
b5.setLocation(280,514);
c5.setLocation(310,514);
d5.setLocation(340,514);

gr.setColor(Color.blue);

}

	public void itemStateChanged(ItemEvent e)
	{
		int numberCorrect=0;
		int score;
				
		Checkbox user1Selection = ques1Grp.getSelectedCheckbox();
		System.out.println(user1Selection);
		if(user1Selection == c1)// checks whether the answer matches
		{
		System.out.println	("Correct");//displays correct if the answer matches
		numberCorrect++; // adds 1 to the number of correct answers
		}else 
		System.out.println("Sorry! The correct answer is c \n"); // if the answer is incorrect displays the correct answer 
		

		Checkbox user2Selection = ques2Grp.getSelectedCheckbox();
		System.out.println(user2Selection);
		if(user2Selection == b2)// checks whether the answer matches
		{
		System.out.println("Correct");//displays correct if the answer matches
		numberCorrect++; // adds 1 to the number of correct answers
		}else 
		System.out.println("Sorry! The correct answer is b \n"); // if the answer is incorrect displays the correct answer 
		
	
		Checkbox user3Selection = ques3Grp.getSelectedCheckbox();
		System.out.println(user3Selection);
		if(user3Selection == a3)// checks whether the answer matches
		{
		System.out.println("Correct");//displays correct if the answer matches
		numberCorrect++; // adds 1 to the number of correct answers
		}else 
		System.out.println("Sorry! The correct answer is a \n"); // if the answer is incorrect displays the correct answer 
		

		Checkbox user4Selection = ques4Grp.getSelectedCheckbox();
		System.out.println(user4Selection);
		if(user4Selection == c4)// checks whether the answer matches
		{
		System.out.println("Correct");//displays correct if the answer matches
		numberCorrect++; // adds 1 to the number of correct answers
		}else 
		System.out.println("Sorry! The correct answer is c \n"); // if the answer is incorrect displays the correct answer 
		
	
		Checkbox user5Selection = ques5Grp.getSelectedCheckbox();
		System.out.println(user5Selection);
		if(user5Selection == d5)// checks whether the answer matches
		{
		System.out.println("Correct");//displays correct if the answer matches
		numberCorrect++; // adds 1 to the number of correct answers
		}else 
		System.out.println("Sorry! The correct answer is d \n"); // if the answer is incorrect displays the correct answer 
			System.out.println("\n You had  "+numberCorrect+" question correct.\n");// displays number of questions correct
	
		score = (numberCorrect*20);//score calculation
		if (score>=60)
		{
		// Displays the end result if the user scores >= 60 it congratulates them or if it's <60 displays better luck next time.
		System.out.println("Congratulations,you passed with a score of "+score+ "%\n");	
		}else{
		System.out.println("Sorry, your grade was  "+score+"% Better luck next time!"); 
		
	repaint();}
 }
}
<HTML>
<H1>Math Quiz!!!!</H1>
<APPLET CODE = "MultipleChoice1.class" WIDTH = 800 HEIGHT = 800>
</APPLET>
</HTML>
The one's which I've  marked in red are not getting displayed on the screen ........... since it comes under "if else loop" under the itemStateChanged method.....
 

1. 7+2 = ? 
   a.6 b.8 c.9 d.10
   c - is marked in the check box since I've used check boxes .......
[B]   Correct[/B]

2. 9-5 = ? 
      a.2 b.4 c.6 d.8
   b - is marked in the check box since I've used check boxes .......
   [B]Correct[/B]


3. 144/12 = ? a.10 b.12 c.14 d.16
   b - is marked in the check box since I've used check boxes .......
   [B]Correct[/B]


4. 5*5 = ? a.10 b.20 c.25 d.125
   c - - is marked in the check box since I've used check boxes .......
  [B] Correct [/B]


5. 2+3+5 = ? a.7 b.8 c.9 d.10
   d - is marked in the check box since I've used check boxes .......
   [B]Correct[/B]

[B]You had 5 question correct.

Congratulations,you passed with a score of 100%[/B]

Many thanks,
Radhika

Recommended Answers

All 2 Replies

I'm sorry but this is not "drop your assignment we will sort it for you service".

Clear problem description of problem is missing...

My program is working fine except that when the user answers the question on html page I'm not able to display whether it is"Correct" or "Incorrect" according to the user's answer ........ and at the end it should congratulate the user as per number of correct answers .........

I'm not able to display the output which comes under the itemStateChanged method .......

Many thanks,
Radhika

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.