import java.applet.*; import java.awt.*; import java.awt.event.*; public class quiz extends Applet implements ActionListener { //q is the question array static private String q[]={"What is a diode?", "What does scalability mean?", "Where did the term robot come from?"}; //d are the "distractors" (choices for answers) static private String d[]={"a switch" ,"a component allowing one-way flow of current" ,"a light sensor" ,"a design that can be made large or small" ,"making robots move" ,"making robots smart" ,"Leonardo daVinci" ,"Isaac Asimov" ,"K. Capek's Play, RUR"}; //a is the answer array static private int a[]={2,1,3}; static private int count=0; private TextField inputOutputField, question, distractor[]; private int correct=0; private Button displayButton,oneButton,twoButton,threeButton; public void init() { count=0; setLayout(null); displayButton=new Button("Display Question"); displayButton.addActionListener(this); add(displayButton); displayButton.setBounds(50,10,100,25); oneButton=new Button("1"); oneButton.addActionListener(this); add(oneButton); oneButton.setBounds(50,75,50,25); twoButton=new Button("2"); twoButton.addActionListener(this); add(twoButton); twoButton.setBounds(50,125,50,25); threeButton=new Button("3"); threeButton.addActionListener(this); add(threeButton); threeButton.setBounds(50,175,50,25); question=new TextField(q[count],25); add(question); //For longer questions, increase the 3rd parameter in setBounds question.setBounds(50,45,350,25); question.setEditable(false); distractor=new TextField[3]; for(int x=0;x<3;x++) { distractor[x]=new TextField(d[x+(3*count)],100); add(distractor[x]); //For longer answers, increase the 3rd parameter in setBounds distractor[x].setBounds(125,75+(50*x),350,25); distractor[x].setEditable(false); } inputOutputField=new TextField("Enter your choice",20); inputOutputField.setBackground(Color.white); add(inputOutputField); inputOutputField.setBounds(50,225,200,25); inputOutputField.setEditable(false); } public void paint(Graphics g) { setBackground(Color.blue); } public void actionPerformed(ActionEvent e) { disableButtons(); if(e.getActionCommand().equals("Display Question")) { if(count>=q.length) { inputOutputField.setText("No more questions."+ " Score: "+correct+"/3"); //***change 3 to the number of questions } else displayQuestion(); } if (e.getActionCommand().equals("1")&&a[count]==1) { inputOutputField.setText("correct"); correct++; count++; } else if (e.getActionCommand().equals("2")&&a[count]==2) { inputOutputField.setText("correct"); correct++; count++; } else if (e.getActionCommand().equals("3")&&a[count]==3) { inputOutputField.setText("correct"); correct++; count++; } else if(e.getActionCommand().equals("1")|| e.getActionCommand().equals("2")|| e.getActionCommand().equals("3")) { inputOutputField.setText("incorrect"); count++; } } public void displayQuestion() { question.setText(q[count]); enableButtons(); for(int x=0;x<3;x++) { distractor[x].setText(d[x+(3*count)]); } inputOutputField.setText(""); } public void disableButtons() { oneButton.setEnabled(false); twoButton.setEnabled(false); threeButton.setEnabled(false); } public void enableButtons() { oneButton.setEnabled(true); twoButton.setEnabled(true); threeButton.setEnabled(true); } }