Develop a GUI java application to conduct a cultural contest between two players
ID: 3578177 • Letter: D
Question
Develop a GUI java application to conduct a cultural contest between two players. Your application will consist of the following parts: Part 1: develop a class to represent a question and its answer. Each question will consist of a question text, three options (one of them is the answer) and the answer. You should provide constructor(s), toString, equals and any other appropriate methods. Part 2: develop a JFrame to allow an administrator to enter questions and save them to a file. The frame should look like Figure 1. When the administrator clicks on the save button, a question object should be appended to the question file (questions.dat). The reset button clears all fields of the frame and the exit button exits the frame after closing all streams. Part 3: develop the contest JFrame class that will allow two players to compete. Your class should first open the questions file (created in Part 2) and read all the question objects from it into an ArrayList object. Appropriate error message should be displayed if the file could not be opened. Players should alternatively be asked random questions from the ArrayList object. Questions that are asked should not be repeated again. There is a single point for each correct answer and the score for each player should be updated accordingly. When no more questions were available, the program should display appropriate message and then terminate. See Figure 2 for a sample run. Please note the following: · The assignment will be graded based on the design and correct execution of the application. You must submit the whole project along with your questions file (i.e. compress everything and upload it to Moodle). Bonus marks will be given for extra functionality (based on instructor judgment).Explanation / Answer
package Quiz;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class quizDemo
{
public static void main(String arg[])
{
question f=new question();
f.setVisible(true);
}
}
class question extends JFrame implements ActionListener,WindowListener
{
String que;
String ans1,ans2,ans3;
String rightAns;
int queNumber;
Panel p1,p2,p3;
TextArea txt_que;
TextField txt_ans1,txt_ans2,txt_ans3,txt_rightAns;
Label lbl_Que,lbl_option1,lbl_option2,lbl_option3,lbl_ans;
Button btn_save,btn_reset,btn_exit;
// JFrame f1,f2;
public question()
{
setSize(500,600);
setTitle("Add Questions:");
setLayout(new FlowLayout(FlowLayout.CENTER));
p1=new Panel();
p2=new Panel();
p3=new Panel();
/* f1=new JFrame();
f2=new JFrame();
f1.setLayout(new FlowLayout(FlowLayout.LEFT));
f2.setLayout(new GridLayout(2,3));*/
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new GridLayout(2,3));
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
lbl_Que=new Label("Question:");
lbl_option1=new Label("option1");
lbl_option2=new Label("option2");
lbl_option3=new Label("option3");
lbl_ans=new Label("Answer is :");
txt_ans1=new TextField(20);
txt_ans2=new TextField(20);
txt_ans3=new TextField(20);
txt_que=new TextArea();
txt_rightAns=new TextField(10);
btn_save=new Button("Save");
btn_reset=new Button("Reset");
btn_exit=new Button("Exit");
p1.add(lbl_Que);
p1.add(txt_que);
/* f1.add(lbl_Que);
f1.add(txt_que);*/
p2.add(lbl_option1);
p2.add(lbl_option2);
p2.add(lbl_option3);
p2.add(txt_ans1);
p2.add(txt_ans2);
p2.add(txt_ans3);
/*f2.add(lbl_option1);
f2.add(lbl_option2);
f2.add(lbl_option3);
f2.add(txt_ans1);
f2.add(txt_ans2);
f2.add(txt_ans3);*/
p3.add(lbl_ans);
p3.add(txt_rightAns);
p3.add(btn_save);
p3.add(btn_reset);
p3.add(btn_exit);
add(p1);
add(p2);
add(p3);
btn_exit.addActionListener(this);
btn_save.addActionListener(this);
btn_reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
FileWriter fw;
BufferedWriter bw;
FileInputStream fs;
BufferedReader br;
if(e.getSource()==btn_exit)
{
System.exit(0);
}
if(e.getSource()==btn_save)
{
que=txt_que.getText().toString();
ans1=txt_ans1.getText().toString();
ans2=txt_ans2.getText().toString();
ans3=txt_ans3.getText().toString();
rightAns=txt_rightAns.getText().toString();
File f=new File("question.dat");
if(!f.exists())
{
try
{
f.createNewFile();
//JOptionPane.showMessageDialog(this,"file is created....");
return;
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,ex);
}
}
else
{
// JOptionPane.showMessageDialog(this,"file is already exist....");
try
{
fw=new FileWriter("question.dat",true);
bw=new BufferedWriter(fw);
bw.write("#"+que+" "+ans1+" "+ans2+" "+ans3+" "+rightAns);
JOptionPane.showMessageDialog(this,"insertion Successfull...");
txt_que.setText("");
txt_ans1.setText("");
txt_ans2.setText("");
txt_ans3.setText("");
txt_rightAns.setText("");
bw.close();
fw.close();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(this,e1);
}
}
}
if(e.getSource()==btn_reset)
{
txt_que.setText("");
txt_ans1.setText("");
txt_ans2.setText("");
txt_ans3.setText("");
txt_rightAns.setText("");
}
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
package Quiz;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
public class player extends JFrame implements ActionListener,WindowListener
{
Panel p1,p2,p3;
TextArea txt_que;
TextField txt_ans1,txt_ans2,txt_ans3;
Label lbl_Que,lbl_option1,lbl_option2,lbl_option3,lbl_score,score;
Button btn_ans,btn_exit;
Scanner s;
int n=0;
ArrayList<String> list;
public player() throws FileNotFoundException
{
setSize(500,600);
setTitle("Questions:");
s = new Scanner(new File("question.dat"));
list = new ArrayList<String>();
while (s.hasNext())
{
list.add(s.next());
}
s.close();
setLayout(new FlowLayout(FlowLayout.CENTER));
p1=new Panel();
p2=new Panel();
p3=new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new GridLayout(2,3));
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
lbl_Que=new Label("Question:");
lbl_option1=new Label("option1");
lbl_option2=new Label("option2");
lbl_option3=new Label("option3");
lbl_score=new Label("SCORE");
score=new Label();
txt_ans1=new TextField(20);
txt_ans2=new TextField(20);
txt_ans3=new TextField(20);
txt_que=new TextArea();
btn_ans=new Button("Answer");
btn_exit=new Button("Exit");
p1.add(lbl_Que);
p1.add(txt_que);
p2.add(lbl_option1);
p2.add(lbl_option2);
p2.add(lbl_option3);
p2.add(txt_ans1);
p2.add(txt_ans2);
p2.add(txt_ans3);
p3.add(btn_ans);
p3.add(btn_exit);
p3.add(lbl_score);
p3.add(score);
add(p1);
add(p2);
add(p3);
btn_exit.addActionListener(this);
btn_ans.addActionListener(this);
selectAns();
}
public void selectAns()
{
String s=list.get(n).toString();
txt_que.setText(s);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn_exit)
{
System.exit(0);
}
if(e.getSource()==btn_ans)
{
score.setText(score.getText().toString());
}
}
public void windowActivated(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.