Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 program issue
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nezura




PostPosted: Thu Oct 21, 2004 12:28 am   Post subject: program issue

Your Task:
Write a program that asks the user 5 multiple choice questions about JAVA, each with at most 5 possible answers(but there must be a variety in the number of possible answers). The program should allow the user to click on an option, and then should tell them whether or not they had the correct answer and what the correct answer is. The user should only have one chance to answer the question. Keep track of their score and display it in a separate frame if they click on the "display score" button. Include an appropriate picture, a nice title for the program, and a title in the frame's browser bar.

Ok here's my dilema, I've typed up what i think the program needs inorder to run, but when i click run in "Ready To Program" it gives a fatal error and crashes. Now i'm sure this code isn't 100% perfect so i hope someone here would be able to help me figure out the problem and possibly a way to correct it. Thanks for any help given.



code:
//Created by Howard Tse
//Multiple Buttons Task
//Last modified: October 19, 2004

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

public class multiple_buttons extends JApplet implements ActionListener
{
    static int scorecounter = 0;
    JButton score = new JButton ("Click here to display your score"); //button when clicked displays your score
    JButton[] buttons = new JButton [5]; //answer buttons
    JButton nextq = new JButton ("Press Me for next Question"); //I had to add in an extra button to go to the next question
    JLabel Qlabel = new JLabel (""); //question label          //because they did not include any method to alternate
    static int counter = 0; //counts the quesion number        //between each question. I assume they intended to wait
    JLabel Plabel = new JLabel (""); //label to store picture   //perhaps 5 seconds after the answer has been shown to switch
    //to the next question, we have not been taught how to do so
    //I have changed it.

    JLabel shownscore = new JLabel ("Your Score is:");
    JLabel empty = new JLabel (""); //empty space, used to change between checkmark and Xmark
    JFrame Questionframe = new JFrame ("Multiple Buttons Question"); //main frame with all questions and answers


    public void init ()
    {
        Icon check = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/checkmark.bmp");
        Icon incorrect = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/incorrect.bmp");
        Icon blank = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/blank.bmp");

        questionframe.setSize (500, 800);
        questionframe.getContentPane ().setLayout (new GridLayout (5, 2));
        questionframe.getContentPane ().add (Qlabel);

        Qlabel.setText ("Who/What is known as the father of JAVA?");

        questionframe.getContentPane ().add (empty);
        questionframe.getContentPane ().add (score);


        for (int x = 0 ; x < 5 ; x++)
        {
            buttons [x] = new JButton (buttonLabels [x]);
            buttons [x].addActionListener (this);
            questionframe.getContentPane ().add (button [x]);
        }
        buttons [0].setText ("a) Sun MicroSystems");
        buttons [1].setText ("b) James Gosling");
        buttons [2].setText ("c) C++");
        buttons [3].setText ("d) Bill Gates");
        buttons [4].setText ("e) Microsoft");
        questionframe.getContentPane ().add (nextq);
        nextq.setEnable (false);
    }


    public void actionPerformed (ActionEvent e)
    {
        Container yourscore = new Container ();
        yourscore.setLayout (new FlowLayout ());
        yourscore.add (shownscore); //adds the label shownscore to the container
        yourscore.add (scorecounter); //adds the number of correct answers to the container
        JFrame scoreframe = new JFrame ("Your Score is...");
        scoreframe.setSize (400, 400);
        scoreframe.getContentPane ().add (yourscore); //adds the container to the new frame
        scoreframe.setVisible (false); //sets the frame invisible

        if (e.getSource () == nextq)
        {
            counter++;
        }

        else if (e.getSource () == score)
        {

            scoreframe.setVisible (true);
        }


        else if (counter == 0) //Question 1
        {
            if (e.getSource () == buttons [0])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("b) James Gosling");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [1])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (check);
                scorecounter++;
                Qlabel.setText ("b) James Gosling");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [2])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("b) James Gosling");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [3])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("b) James Gosling");
                nextq.setEnable (true);
            }
            else if (e.getSource () == buttons [4])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("b) James Gosling");
                nextq.setEnable (true);
            }
        }
        else if (counter == 1) //Question 2
        {
            for (a = 0 ; a < 5 ; a++) //All the values inside the question and answer are reset to those of question 2
            {
                buttons [a].setEnable (true);
            }
            empty.setIcon (blank);
            Qlabel.setText ("Which of the Following is an integer type?");
            buttons [0].setText ("a) boolean");
            buttons [1].setText ("b) double");
            buttons [2].setText ("c) chor");
            buttons [3].setText ("d) long");
            buttons [4].setText ("");
            buttons [4].setEnable (false);
            nextq.setEnable (false);

            if (e.getSource () == buttons [0])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("d) long");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [1])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("d) long");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [2])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("d) long");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [3])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (check);
                scorecounter++;
                Qlabel.setText ("d) long");
                nextq.setEnable (true);
            }

        }

        else if (counter == 2) //Question 3
        {
            for (a = 0 ; a < 5 ; a++) //All the values inside the question and answer are reset to those of question 3
            {
                buttons [a].setEnable (true);
            }
            empty.setIcon (blank);
            Qlabel.setText ("Which of the following adds 5 spaces");
            buttons [0].setText ("a) /t");
            buttons [1].setText ("b) \t");
            buttons [2].setText ("c) \space");
            buttons [3].setText ("");
            buttons [4].setText ("");

            buttons [3].setEnable (false);
            buttons [4].setEnable (false);
            nextq.setEnable (false);

            if (e.getSource () == buttons [0])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("b) \t");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [1])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (check);
                scorecounter++;
                Qlabel.setText ("b) \t");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [2])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("b) \t");
                nextq.setEnable (true);
            }

        }
        else if (counter == 3) //Question 4
        {
            for (a = 0 ; a < 5 ; a++) //All the values inside the question and answer are reset to those of question 4
            {
                buttons [a].setEnable (true);
            }
            empty.setIcon (blank);
            Qlabel.setText ("!= means");
            buttons [0].setText ("a) not equal");
            buttons [1].setText ("b) equal");
            buttons [2].setText ("");
            buttons [3].setText ("");
            buttons [4].setText ("");

            buttons [2].setEnable (false);
            buttons [3].setEnable (false);
            buttons [4].setEnable (false);
            nextq.setEnable (false);

            if (e.getSource () == buttons [0])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (check);
                scorecounter++;
                Qlabel.setText ("a) not equal");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [1])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("a) not equal");
                nextq.setEnable (true);
            }

        }

        else if (counter == 4) //Question 5
        {
            for (a = 0 ; a < 5 ; a++) //All the values inside the question and answer are reset to those of question 5
            {
                buttons [a].setEnable (true);
            }
            empty.setIcon (blank);
            Qlabel.setText ("Is JAVA a compiled or interpreted language?");
            buttons [0].setText ("a) Compiled");
            buttons [1].setText ("b) Interpreted");
            buttons [2].setText ("c) Neither");
            buttons [3].setText ("d) Both");
            buttons [4].setText ("");

            buttons [4].setEnable (false);
            nextq.setEnable (false);

            if (e.getSource () == buttons [0])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (check);
                scorecounter++
                    Qlabel.setText ("a) Compiled");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [1])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("a) Compiled");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [2])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("a) Compiled");
                nextq.setEnable (true);
            }

            else if (e.getSource () == buttons [3])
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setEnable (false);
                }
                empty.setIcon (incorrect);
                Qlabel.setText ("a) Compiled");
                nextq.setEnable (true);
            }


            else if (counter == 5) //All questions are answered page
            {
                for (a = 0 ; a < 5 ; a++)
                {
                    buttons [a].setText ("");
                }
                empty.setIcon (blank);
                Qlabel.setText ("You are now complete the quiz, Press score button");

                nextq.setText ("");
                nextq.setEnable (false);

            }
        }


Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: