//Quiz.java /*--------------------------------------------------------------- ' CLASS: Quiz ' ' AUTHOR: Shawn McKenna ' ' DESC: This is a java applet that is a simple Star Trek Quiz (TOS). Currently at 10 ' questions. After you enter the submit button, it goes to the corresponding HTML ' page. Better results, better ending (I suppose). Of course, there could be tons ' of changes for the better. ' ' NOTE: Server-side quizzes work better. This program was written in a structured approach ' as opposed to a Object-Oriented approach. The code is easily read and can be changed ' or added to quite effortlessly. ' ' GENERIC NOTE: All my code has been tested, my apologies for any unforseen errors. ' Suggestions are always welcome. Feel free to use or change any of this ' code. ' ' CONSTRUCTOR: NONE ' ' METHODS: ' init() ' MODIFIER: public ' DESC: Applet is initialized. ' RETURN TYPE: void ' ' action() ' MODIFIER: public ' DESC: After submit button is pushed, action happens here. ' RETURN TYPE: boolean ' NOTE: Depreacted method. I just did this to actually have a program with this ' method. Use listeners instead. ' ' goToPage(Integer intRight) ' MODIFIER: public ' DESC: This method is passed the number correct on the quiz and opens to the ' corresponding (URL) web page. Depending on the number right -- (better ' score, better ending). ' RETURN TYPE: VOID ' ' DATE MODIFIED: 11/15/2000 ' VERSION: 1.4 ' ' PREVIOUS VERSIONS: ' Version 1.4 -- added more comments and changed more code. ' Version 1.3 -- change source code. Still have to make endings for 2 or more right. ' Version 1.2 -- I now have an ending for 1 right and I changed some code. ' Version 1.1 -- I now have an ending for 0 right. ' Version 1.0 -- I have not quite figured out what I should do with the results. So now the number right appear in the text ' box. Later, I might have something special like different endings according to points scored and maybe a list of the ' wrong answers. I am happier with my JavaScript quiz page. These quiz pages are best done in CGI, ASP or JavaScript. ' Java (AWT) seems to be a bit limited. ' '---------------------------------------------------------------- */ import java.applet.*; import java.awt.*; import java.net.*; public class Quiz extends Applet { final int Q_NUM = 10; //number of questions int iRight = 0; //number of questions right int i = 0; //number for loop Integer intRight; //class wrapper for intRight TextField txtBox; //text box (barely used) Button bSubmit; //the submit button Panel p1; Label q1Label, q2Label, q3Label, q4Label, q5Label, q5Label2; Label q6Label, q7Label, q8Label, q8Label2, q9Label, q10Label; CheckboxGroup q[] = new CheckboxGroup[Q_NUM]; Checkbox qA[] = new Checkbox[Q_NUM]; Checkbox qB[] = new Checkbox[Q_NUM]; Checkbox qC[] = new Checkbox[Q_NUM]; Checkbox qD[] = new Checkbox[Q_NUM]; public void init() { setBackground (Color.white); p1 = new Panel(); p1.setLayout( new GridLayout( 0, 1 ) ); q1Label = new Label( "1: Hikaru Sulu was born on planet Earth. What city was he born in?" ); q1Label.setAlignment( Label.LEFT ); q1Label.setFont(new Font("Arial", Font.BOLD, 15)); q1Label.setForeground(Color.blue); p1.add (q1Label); q[0] = new CheckboxGroup(); qA[0]= new Checkbox (" A: Modesto",q[0], false); qB[0]= new Checkbox (" B: San Francisco",q[0], false); qC[0]= new Checkbox (" C: New York City",q[0], false); qD[0]= new Checkbox (" D: Lansing", q[0], false); p1.add (qA[0]); p1.add (qB[0]); p1.add (qC[0]); p1.add (qD[0]); // question two q2Label = new Label( "2: Who is Spock's father?" ); q2Label.setAlignment( Label.LEFT ); q2Label.setFont(new Font("Arial", Font.BOLD, 15)); q2Label.setForeground(Color.blue); p1.add (q2Label); q[1] = new CheckboxGroup(); qA[1] = new Checkbox (" A: Sarek",q[1], false); qB[1] = new Checkbox (" B: Amanda Grayson",q[1], false); qC[1] = new Checkbox (" C: Mark Leonard",q[1], false); qD[1] = new Checkbox (" D: Mot",q[1], false); p1.add (qA[1]); p1.add (qB[1]); p1.add (qC[1]); p1.add (qD[1]); // question three q3Label = new Label( "3: Who was the first to command the USS Enterprise? (Constitution-Class)" ); q3Label.setAlignment( Label.LEFT ); q3Label.setFont(new Font("Arial", Font.BOLD, 15)); q3Label.setForeground(Color.blue); p1.add (q3Label); q[2] = new CheckboxGroup(); qA[2] = new Checkbox (" A: Captain Kirk",q[2], false); qB[2] = new Checkbox (" B: Captain Ahab",q[2], false); qC[2] = new Checkbox (" C: Captain Christopher Pike",q[2], false); qD[2] = new Checkbox (" D: Captain Robert April",q[2], false); p1.add (qA[2]); p1.add (qB[2]); p1.add (qC[2]); p1.add (qD[2]); // question four q4Label = new Label( "4: Benjamin Finney's daughter Jamie was names after who? Episode -- Court Martial" ); q4Label.setAlignment( Label.LEFT ); q4Label.setFont(new Font("Arial", Font.BOLD, 15)); q4Label.setForeground(Color.blue); p1.add (q4Label); q[3] = new CheckboxGroup(); qA[3] = new Checkbox (" A: Captain Bleigh ",q[3], false); qB[3] = new Checkbox (" B: Jack the Ripper",q[3], false); qC[3] = new Checkbox (" C: James T. Kirk",q[3], false); qD[3] = new Checkbox (" D: Kyril Finn",q[3], false); p1.add (qA[3]); p1.add (qB[3]); p1.add (qC[3]); p1.add (qD[3]); // question five q5Label = new Label( "5: Commodore Matt Decker and his ship was attacked by a robotic planet killer in 2267." ); q5Label2 = new Label(" What was the name of the ship he commanded?"); q5Label.setAlignment( Label.LEFT ); q5Label.setFont(new Font("Arial", Font.BOLD, 15)); q5Label.setForeground(Color.blue); q5Label2.setAlignment( Label.LEFT ); q5Label2.setFont(new Font("Arial", Font.BOLD, 15)); q5Label2.setForeground(Color.blue); p1.add (q5Label); p1.add (q5Label2); q[4] = new CheckboxGroup(); qA[4] = new Checkbox (" A: USS Devoras ",q[4], false); qB[4] = new Checkbox (" B: USS Constellation",q[4], false); qC[4] = new Checkbox (" C: USS Pequod",q[4], false); qD[4] = new Checkbox (" D: USS Monitor",q[4], false); p1.add (qA[4]); p1.add (qB[4]); p1.add (qC[4]); p1.add (qD[4]); // question six q6Label = new Label( "6: What species was the reptilian creature that Captain Kirk fought in the episode Arena?" ); q6Label.setAlignment( Label.LEFT ); q6Label.setFont(new Font("Arial", Font.BOLD, 15)); q6Label.setForeground(Color.blue); p1.add (q6Label); q[5] = new CheckboxGroup(); qA[5] = new Checkbox (" A: Vulcan",q[5], false); qB[5] = new Checkbox (" B: Klingon",q[5], false); qC[5] = new Checkbox (" C: Borg",q[5], false); qD[5] = new Checkbox (" D: Gorn",q[5], false); p1.add (qA[5]); p1.add (qB[5]); p1.add (qC[5]); p1.add (qD[5]); // question seven q7Label = new Label( "7: Name the actor who plays Uhura." ); q7Label.setAlignment( Label.LEFT ); q7Label.setFont(new Font("Arial", Font.BOLD, 15)); q7Label.setForeground(Color.blue); p1.add (q7Label); q[6] = new CheckboxGroup(); qA[6] = new Checkbox (" A: Renee Jones",q[6], false); qB[6] = new Checkbox (" B: Nichelle Nichols",q[6], false); qC[6] = new Checkbox (" C: Natasha Bolkonsky",q[6], false); qD[6] = new Checkbox (" D: Kay Elliot",q[6], false); p1.add (qA[6]); p1.add (qB[6]); p1.add (qC[6]); p1.add (qD[6]); // question eight q8Label = new Label( "8: Diana Muldaur played Dr. Katherine Pulaski in Star Trek: TNG. In what episode" ); q8Label2 = new Label(" did she play the character named Dr. Miranda Jones?"); q8Label.setAlignment( Label.LEFT ); q8Label.setFont(new Font("Arial", Font.BOLD, 15)); q8Label.setForeground(Color.blue); q8Label2.setAlignment( Label.LEFT ); q8Label2.setFont(new Font("Arial", Font.BOLD, 15)); q8Label2.setForeground(Color.blue); p1.add (q8Label); p1.add (q8Label2); q[7] = new CheckboxGroup(); qA[7] = new Checkbox (" A: Return to Tomorrow ",q[7], false); qB[7] = new Checkbox (" B: Is There in Truth No Beauty",q[7], false); qC[7] = new Checkbox (" C: Allegiance",q[7], false); qD[7] = new Checkbox (" D: Shadows of Gray",q[7], false); p1.add (qA[7]); p1.add (qB[7]); p1.add (qC[7]); p1.add (qD[7]); // question nine q9Label = new Label( "9: What character does James Doohan play in the Original Series?" ); q9Label.setAlignment( Label.LEFT ); q9Label.setFont(new Font("Arial", Font.BOLD, 15)); q9Label.setForeground(Color.blue); p1.add (q9Label); q[8] = new CheckboxGroup(); qA[8] = new Checkbox (" A: James T. Kirk",q[8], false); qB[8] = new Checkbox (" B: Spock",q[8], false); qC[8] = new Checkbox (" C: Dr. McCoy",q[8], false); qD[8] = new Checkbox (" D: Montogomery Scott",q[8], false); p1.add (qA[8]); p1.add (qB[8]); p1.add (qC[8]); p1.add (qD[8]); // question ten q10Label = new Label( "10: What planet were Khan Noonien Singh and his followers exiled too?" ); q10Label.setAlignment( Label.LEFT ); q10Label.setFont(new Font("Arial", Font.BOLD, 15)); q10Label.setForeground(Color.blue); p1.add (q10Label); q[9] = new CheckboxGroup(); qA[9] = new Checkbox (" A: Cet. Alpha V",q[9], false); qB[9] = new Checkbox (" B: Earth",q[9], false); qC[9] = new Checkbox (" C: Manark IV",q[9], false); qD[9] = new Checkbox (" D: DM/HR VI",q[9], false); p1.add (qA[9]); p1.add (qB[9]); p1.add (qC[9]); p1.add (qD[9]); /* for some reason, Internet Explorer has a different background color for the checkbox items than Netscape. So this for loop corrects that problem. */ for (i = 0; i < Q_NUM; ++i) { qA[i].setBackground(Color.white); qB[i].setBackground(Color.white); qC[i].setBackground(Color.white); qD[i].setBackground(Color.white); } GridBagLayout PPLayout = new GridBagLayout(); GridBagConstraints itemConstraint = new GridBagConstraints(); setLayout( PPLayout ); itemConstraint.fill = GridBagConstraints.HORIZONTAL; itemConstraint.gridx = 0; itemConstraint.gridy = 0; itemConstraint.gridwidth = 1; itemConstraint.gridheight = 1; itemConstraint.weightx = 1.0; itemConstraint.weighty = 1.0; PPLayout.setConstraints( p1, itemConstraint ); add (p1); txtBox = new TextField ("How many do you think you have right?"); itemConstraint.gridx = 0; itemConstraint.gridy = 1; itemConstraint.gridwidth = 1; itemConstraint.gridheight = 1; itemConstraint.weightx = 1.0; itemConstraint.weighty = 1.0; PPLayout.setConstraints( txtBox, itemConstraint ); add (txtBox); bSubmit = new Button("Submit"); itemConstraint.gridx = 1; itemConstraint.gridy = 1; itemConstraint.gridwidth = 1; itemConstraint.gridheight = 1; itemConstraint.weightx = 1.0; itemConstraint.weighty = 1.0; itemConstraint.fill = GridBagConstraints.NONE; PPLayout.setConstraints( bSubmit, itemConstraint ); add (bSubmit); } // end of init method // the action method is a deprecated method. A listener should be used instead. public boolean action(Event buttonEvent, Object buttonObject) { if (buttonEvent.target.equals(bSubmit)) { iRight = 0; // this initializes iRight when the action method is invoked. if (qB[0].getState()) ++iRight; if (qA[1].getState()) ++iRight; if (qD[2].getState()) ++iRight; if (qC[3].getState()) ++iRight; if (qB[4].getState()) ++iRight; if (qD[5].getState()) ++iRight; if (qB[6].getState()) ++iRight; if (qB[7].getState()) ++iRight; if (qD[8].getState()) ++iRight; if (qA[9].getState()) ++iRight; //these two lines convert an int into Integer and then print results. intRight = new Integer (iRight); txtBox.setText("You got " + intRight.toString() + " right!"); goToPage (intRight); // goTo the correct page } // end of buttonEvent if statement return true; } // end of action method /*--------------------------------------------------------------- ' goToPage() ' ' DESC: This method is passed the number correct on the quiz and ' opens to the corresponding (URL) web page. Depending on ' the number right -- (better score, better ending). ' ' ACCESS MODIFIER: public ' PARAMETER(S): Integer intRight ' RETURNS: VOID ' DATE MODIFIED: 09/27/2000 '---------------------------------------------------------------- */ public void goToPage (Integer intRight) { try { getAppletContext().showDocument(new URL ("http://www.softcom.net/users/chuckmc/startrek/results/results"+ intRight.toString() + ".html")); } //end of try catch (MalformedURLException me) { } //end of catch } //end of method goToPage } // end of Quiz class