473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

illegal start of expression public void itemStateChanged(ItemEvent e)

This is my code that keeps on having the illegal start of expression error. Have included error messages at the bottom.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4.  
  5. public class Prog extends Applet implements ItemListener
  6. {
  7.     Label alabel = new Label("MDAS Game Program");
  8.     Label slabel = new Label("Choose the answer");
  9.  
  10.     CheckboxGroup qq = new CheckboxGroup();
  11.  
  12.     Label qlabel = new Label("1.) 60 * 3 / 5 + 4 - 38 = ??");
  13.     Checkbox aone = new Checkbox("A. 2",false,qq);
  14.     Checkbox bone = new Checkbox("B. 3",false,qq);
  15.     Checkbox cone = new Checkbox("C. 4",false,qq);
  16.     Checkbox hone = new Checkbox("",true,qq);
  17.  
  18.     Label wlabel = new Label("2.) 5 * 100 / 5 + 400 - 499 = ?? ");
  19.     Checkbox atwo = new Checkbox("A. 1",false,qq);
  20.     Checkbox btwo = new Checkbox("B. 2",false,qq);
  21.     Checkbox ctwo = new Checkbox("C. 3",false,qq);
  22.     Checkbox htwo = new Checkbox("",true,qq);
  23.  
  24.     Label elabel = new Label("3.) 10 * 5 - 10 / 2 + 3 = ??");
  25.     Checkbox athree = new Checkbox("A. 18",false,qq);
  26.     Checkbox bthree = new Checkbox("B. 19",false,qq);
  27.     Checkbox cthree = new Checkbox("C. 17",false,qq);
  28.     Checkbox hthree = new Checkbox("",true,qq);
  29.  
  30.     Label rlabel = new Label("4.) 50 * 5 / 10 + 300 - 10 = ??");
  31.     Checkbox afour = new Checkbox("A. 315",false,qq);
  32.     Checkbox bfour = new Checkbox("B. 316",false,qq);
  33.     Checkbox cfour = new Checkbox("C. 317",false,qq);
  34.     Checkbox hfour = new Checkbox("",true,qq);
  35.  
  36.     Label tlabel = new Label("5.) 10 * 2 / 10 - 300 + 100 = ??");
  37.     Checkbox afive = new Checkbox("A. -198",false,qq);
  38.     Checkbox bfive = new Checkbox("B. 198",false,qq);
  39.     Checkbox cfive = new Checkbox("C. 199",false,qq);
  40.     Checkbox hfive = new Checkbox("",true,qq);
  41.  
  42.     Label ylabel = new Label("6.) 10 * 5 / 50 + 1 - 2 = ??");
  43.     Checkbox asix = new Checkbox("A. 1",false,qq);
  44.     Checkbox bsix = new Checkbox("B. 0",false,qq);
  45.     Checkbox csix = new Checkbox("C. 2",false,qq);
  46.     Checkbox hsix = new Checkbox("",true,qq);
  47.  
  48.     Label ulabel = new Label("7.) 8 * 3 / 3 + 450 - 400 = ??");
  49.     Checkbox aseven = new Checkbox("A. 59",false,qq);
  50.     Checkbox bseven = new Checkbox("B. 58",false,qq);
  51.     Checkbox cseven = new Checkbox("C. 57",false,qq);
  52.     Checkbox hseven = new Checkbox("",true,qq);
  53.  
  54.     Label ilabel = new Label("8.) 5 + 4 * 30 / 10 - 2 = ??");
  55.     Checkbox aeight = new Checkbox("A. 14",false,qq);
  56.     Checkbox beight = new Checkbox("B. 15",false,qq);
  57.     Checkbox ceight = new Checkbox("C. 16",false,qq);
  58.     Checkbox height = new Checkbox("",true,qq);
  59.  
  60.     Label olabel = new Label("9.) 5 + 100 * 1 - 2 / 100  = ??");
  61.     Checkbox anine = new Checkbox("A. 3",false,qq);
  62.     Checkbox bnine = new Checkbox("B. 4",false,qq);
  63.     Checkbox cnine = new Checkbox("C. 6",false,qq);
  64.     Checkbox hnine = new Checkbox("",true,qq);
  65.  
  66.  
  67.     Label plabel = new Label("10.) 7 * 4 / 2 - 10 + 300 = ??");
  68.     Checkbox aten = new Checkbox("A. 303",false,qq);
  69.     Checkbox bten = new Checkbox("B. 304",false,qq);
  70.     Checkbox cten = new Checkbox("C. 604",false,qq);
  71.     Checkbox hten = new Checkbox("",true,qq);
  72.  
  73.  
  74.     public void init()
  75.     {
  76.         add(alabel);
  77.         add(slabel);
  78.  
  79.         add(qlabel);
  80. add(aone);
  81.     aone.addItemListener(this);
  82. add(bone);
  83.     bone.addItemListener(this);
  84. add(cone);
  85.     cone.addItemListener(this);
  86. add(hone);
  87.     hone.addItemListener(this);
  88.  
  89. add(wlabel);
  90. add(atwo);
  91.     atwo.addItemListener(this);
  92. add(btwo);
  93.     btwo.addItemListener(this);
  94. add(ctwo);
  95.     ctwo.addItemListener(this);
  96. add(htwo);
  97.     htwo.addItemListener(this);
  98.  
  99.     add(elabel);
  100. add(athree);
  101.     athree.addItemListener(this);
  102. add(bthree);
  103.     bthree.addItemListener(this);
  104. add(cthree);
  105.     cthree.addItemListener(this);
  106. add(hthree);
  107.     hthree.addItemListener(this);
  108.  
  109.     add(rlabel);
  110. add(afour);
  111.     afour.addItemListener(this);
  112. add(bfour);
  113.     bfour.addItemListener(this);
  114. add(cfour);
  115.     cfour.addItemListener(this);
  116. add(hfour);
  117.     hfour.addItemListener(this);
  118.  
  119.     add(tlabel);
  120. add(afive);
  121.     afive.addItemListener(this);
  122. add(bfive);
  123.     bfive.addItemListener(this);
  124. add(cfive);
  125.     cfive.addItemListener(this);
  126. add(hfive);
  127.     hfive.addItemListener(this);
  128.  
  129. add(ylabel);
  130. add(asix);
  131.     asix.addItemListener(this);
  132. add(bsix);
  133.     bsix.addItemListener(this);
  134. add(csix);
  135.     csix.addItemListener(this);
  136. add(hsix);
  137.     hsix.addItemListener(this);
  138.  
  139. add(ulabel);
  140. add(aseven);
  141.     aseven.addItemListener(this);
  142. add(bseven);
  143.     bseven.addItemListener(this);
  144. add(cseven);
  145.     cseven.addItemListener(this);
  146. add(hseven);
  147.     hseven.addItemListener(this);
  148.  
  149.     add(ilabel);
  150. add(aeight);
  151.     aeight.addItemListener(this);
  152. add(beight);
  153.     beight.addItemListener(this);
  154. add(ceight);
  155.     ceight.addItemListener(this);
  156. add(height);
  157.     height.addItemListener(this);
  158.  
  159.     add(olabel);
  160. add(anine);
  161.     anine.addItemListener(this);
  162. add(anine);
  163.     bnine.addItemListener(this);
  164. add(cnine);
  165.     cnine.addItemListener(this);
  166. add(hnine);
  167.     hnine.addItemListener(this);
  168.  
  169.     add(plabel);
  170. add(aten);
  171.     aten.addItemListener(this);
  172. add(bten);
  173.     bten.addItemListener(this);
  174. add(cten);
  175.     cten.addItemListener(this);
  176. add(hten);
  177.     hten.addItemListener(this);
  178.  
  179.  
  180.  
  181.  
  182. public void itemStateChanged(ItemEvent e)
  183. {
  184. if (aone.getState()) 
  185. answer1=1;
  186. if (atwo.getState())
  187. answer2=1;
  188. if (athree.getState())
  189. answer3=1;
  190. if (afour.getState())
  191. answer4=1;
  192. if (afive.getState())
  193. answer5=1;
  194. if (bsix.getState())
  195. answer6=1;
  196. if (bseven.getState())
  197. answer7=1;
  198. if (beight.getState())
  199. answer8=1;
  200. if (bnine.getState())
  201. answer9=1;
  202. if (bten.getState())
  203. answer10=1;
  204. totalscore=answer1+answer2+answer3+answer4+answer5+answer6+answer7+answer8+answer9+answer10;
  205. repaint();
  206.  
  207.  
  208. }
  209.  
  210. public void actionPerformed(ActionEvent e)
  211. {
  212. repaint();
  213.  
  214. score.setText("Total score is :" +totalscore);
  215. }
  216.  
  217.     }
  218.  
  219.  
  220.     }
  221.  


These are the errors that pops up when compiled
C:\Users\caburnay\Desktop\Prog.java:182: error: illegal start of expression
public void itemStateChanged(ItemEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:182: error: illegal start of expression
public void itemStateChanged(ItemEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:182: error: ';' expected
public void itemStateChanged(ItemEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:182: error: ';' expected
public void itemStateChanged(ItemEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:210: error: illegal start of expression
public void actionPerformed(ActionEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:210: error: illegal start of expression
public void actionPerformed(ActionEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:210: error: ';' expected
public void actionPerformed(ActionEvent e)
^
C:\Users\caburnay\Desktop\Prog.java:210: error: ';' expected
public void actionPerformed(ActionEvent e)
^
8 errors
Mar 17 '13 #1
16 3199
r035198x
13,262 8TB
You are missing a closing brace for your init method before the line with public void itemStateChanged ...
Mar 18 '13 #2
and here's another error can you help me?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     import java.awt.*;
  4.     import java.applet.*;
  5.     import java.awt.event.*;
  6.  
  7.     public class t1 extends Applet implements ItemListener
  8.     {
  9.         Label alabel = new Label("MDAS Game Program");
  10.         Label slabel = new Label("Choose the answer");
  11.  
  12.         CheckboxGroup qq = new CheckboxGroup();
  13.  
  14.         Label qlabel = new Label("1.) 60 * 3 / 5 + 4 - 38 = ??");
  15.         Checkbox aone = new Checkbox("A. 2",false,qq);
  16.         Checkbox bone = new Checkbox("B. 3",false,qq);
  17.         Checkbox cone = new Checkbox("C. 4",false,qq);
  18.         Checkbox hone = new Checkbox("",true,qq);
  19.  
  20.         Label wlabel = new Label("2.) 5 * 100 / 5 + 400 - 499 = ?? ");
  21.         Checkbox atwo = new Checkbox("A. 1",false,qq);
  22.         Checkbox btwo = new Checkbox("B. 2",false,qq);
  23.         Checkbox ctwo = new Checkbox("C. 3",false,qq);
  24.         Checkbox htwo = new Checkbox("",true,qq);
  25.  
  26.         Label elabel = new Label("3.) 10 * 5 - 10 / 2 + 3 = ??");
  27.         Checkbox athree = new Checkbox("A. 18",false,qq);
  28.         Checkbox bthree = new Checkbox("B. 19",false,qq);
  29.         Checkbox cthree = new Checkbox("C. 17",false,qq);
  30.         Checkbox hthree = new Checkbox("",true,qq);
  31.  
  32.         Label rlabel = new Label("4.) 50 * 5 / 10 + 300 - 10 = ??");
  33.         Checkbox afour = new Checkbox("A. 315",false,qq);
  34.         Checkbox bfour = new Checkbox("B. 316",false,qq);
  35.         Checkbox cfour = new Checkbox("C. 317",false,qq);
  36.         Checkbox hfour = new Checkbox("",true,qq);
  37.  
  38.         Label tlabel = new Label("5.) 10 * 2 / 10 - 300 + 100 = ??");
  39.         Checkbox afive = new Checkbox("A. -198",false,qq);
  40.         Checkbox bfive = new Checkbox("B. 198",false,qq);
  41.         Checkbox cfive = new Checkbox("C. 199",false,qq);
  42.         Checkbox hfive = new Checkbox("",true,qq);
  43.  
  44.         Label ylabel = new Label("6.) 10 * 5 / 50 + 1 - 2 = ??");
  45.         Checkbox asix = new Checkbox("A. 1",false,qq);
  46.         Checkbox bsix = new Checkbox("B. 0",false,qq);
  47.         Checkbox csix = new Checkbox("C. 2",false,qq);
  48.         Checkbox hsix = new Checkbox("",true,qq);
  49.  
  50.         Label ulabel = new Label("7.) 8 * 3 / 3 + 450 - 400 = ??");
  51.         Checkbox aseven = new Checkbox("A. 59",false,qq);
  52.         Checkbox bseven = new Checkbox("B. 58",false,qq);
  53.         Checkbox cseven = new Checkbox("C. 57",false,qq);
  54.         Checkbox hseven = new Checkbox("",true,qq);
  55.  
  56.         Label ilabel = new Label("8.) 5 + 4 * 30 / 10 - 2 = ??");
  57.         Checkbox aeight = new Checkbox("A. 14",false,qq);
  58.         Checkbox beight = new Checkbox("B. 15",false,qq);
  59.         Checkbox ceight = new Checkbox("C. 16",false,qq);
  60.         Checkbox height = new Checkbox("",true,qq);
  61.  
  62.         Label olabel = new Label("9.) 5 + 100 * 1 - 2 / 100  = ??");
  63.         Checkbox anine = new Checkbox("A. 3",false,qq);
  64.         Checkbox bnine = new Checkbox("B. 4",false,qq);
  65.         Checkbox cnine = new Checkbox("C. 6",false,qq);
  66.         Checkbox hnine = new Checkbox("",true,qq);
  67.  
  68.  
  69.         Label plabel = new Label("10.) 7 * 4 / 2 - 10 + 300 = ??");
  70.         Checkbox aten = new Checkbox("A. 303",false,qq);
  71.         Checkbox bten = new Checkbox("B. 304",false,qq);
  72.         Checkbox cten = new Checkbox("C. 604",false,qq);
  73.         Checkbox hten = new Checkbox("",true,qq);
  74.  
  75.         Button done = new Button("Done");
  76.  
  77.  
  78.         public void init()
  79.         {
  80.             add(alabel);
  81.             add(slabel);
  82.  
  83.             add(qlabel);
  84.     add(aone);
  85.         aone.addItemListener(this);
  86.     add(bone);
  87.         bone.addItemListener(this);
  88.     add(cone);
  89.         cone.addItemListener(this);
  90.     add(hone);
  91.         hone.addItemListener(this);
  92.  
  93.     add(wlabel);
  94.     add(atwo);
  95.         atwo.addItemListener(this);
  96.     add(btwo);
  97.         btwo.addItemListener(this);
  98.     add(ctwo);
  99.         ctwo.addItemListener(this);
  100.     add(htwo);
  101.         htwo.addItemListener(this);
  102.  
  103.         add(elabel);
  104.     add(athree);
  105.         athree.addItemListener(this);
  106.     add(bthree);
  107.         bthree.addItemListener(this);
  108.     add(cthree);
  109.         cthree.addItemListener(this);
  110.     add(hthree);
  111.         hthree.addItemListener(this);
  112.  
  113.         add(rlabel);
  114.     add(afour);
  115.         afour.addItemListener(this);
  116.     add(bfour);
  117.         bfour.addItemListener(this);
  118.     add(cfour);
  119.         cfour.addItemListener(this);
  120.     add(hfour);
  121.         hfour.addItemListener(this);
  122.  
  123.         add(tlabel);
  124.     add(afive);
  125.         afive.addItemListener(this);
  126.     add(bfive);
  127.         bfive.addItemListener(this);
  128.     add(cfive);
  129.         cfive.addItemListener(this);
  130.     add(hfive);
  131.         hfive.addItemListener(this);
  132.  
  133.     add(ylabel);
  134.     add(asix);
  135.         asix.addItemListener(this);
  136.     add(bsix);
  137.         bsix.addItemListener(this);
  138.     add(csix);
  139.         csix.addItemListener(this);
  140.     add(hsix);
  141.         hsix.addItemListener(this);
  142.  
  143.     add(ulabel);
  144.     add(aseven);
  145.         aseven.addItemListener(this);
  146.     add(bseven);
  147.         bseven.addItemListener(this);
  148.     add(cseven);
  149.         cseven.addItemListener(this);
  150.     add(hseven);
  151.         hseven.addItemListener(this);
  152.  
  153.         add(ilabel);
  154.     add(aeight);
  155.         aeight.addItemListener(this);
  156.     add(beight);
  157.         beight.addItemListener(this);
  158.     add(ceight);
  159.         ceight.addItemListener(this);
  160.     add(height);
  161.         height.addItemListener(this);
  162.  
  163.         add(olabel);
  164.     add(anine);
  165.         anine.addItemListener(this);
  166.     add(anine);
  167.         bnine.addItemListener(this);
  168.     add(cnine);
  169.         cnine.addItemListener(this);
  170.     add(hnine);
  171.         hnine.addItemListener(this);
  172.  
  173.         add(plabel);
  174.     add(aten);
  175.         aten.addItemListener(this);
  176.     add(bten);
  177.         bten.addItemListener(this);
  178.     add(cten);
  179.         cten.addItemListener(this);
  180.     add(hten);
  181.         hten.addItemListener(this);
  182.  
  183.     add(done);
  184.         done.addActionListener(this);
  185.  
  186.         }
  187.  
  188.  
  189.     public void itemStateChanged(ItemEvent e)
  190.     {
  191.  
  192.         int answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10;
  193.         int totalscore;
  194.         totalscore = 0;
  195.  
  196.     if (aone.getState()) 
  197.     answer1=1;
  198.     if (atwo.getState())
  199.     answer2=1;
  200.     if (athree.getState())
  201.     answer3=1;
  202.     if (afour.getState())
  203.     answer4=1;
  204.     if (afive.getState())
  205.     answer5=1;
  206.     if (bsix.getState())
  207.     answer6=1;
  208.     if (bseven.getState())
  209.     answer7=1;
  210.     if (beight.getState())
  211.     answer8=1;
  212.     if (bnine.getState())
  213.     answer9=1;
  214.     if (bten.getState())
  215.     answer10=1;
  216.     totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
  217.     repaint();
  218.  
  219.  
  220.     }
  221.  
  222.     public void actionPerformed(ActionEvent e)
  223.     {
  224.     repaint();
  225.  
  226.  
  227.  
  228.     if ("done".equals(e.getActionCommand())) 
  229.         {
  230.  
  231.          done.setText("Total score is :" + totalscore);
  232.  
  233.            }
  234.  
  235.     }
  236.  
  237.         }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
and the error is

--------------------Configuration: <Default>--------------------
C:\Users\caburnay\Desktop\t1.java:184: error: method addActionListener in class Button cannot be applied to given types;
done.addActionListener(this);
^
required: ActionListener
found: t1
reason: actual argument t1 cannot be converted to ActionListener by method invocation conversion
C:\Users\caburnay\Desktop\t1.java:231: error: cannot find symbol
done.setText("Total score is :" + totalscore);
^
symbol: variable totalscore
location: class t1
2 errors

Process completed.
Mar 18 '13 #3
r035198x
13,262 8TB
Try to deduce the problem from the error message given.
e.g It says method "addActionListener in class Button cannot be applied to given types" and says later that "t1 cannot be converted to ActionListener". So you are trying to add an action listener but not passing in an action listener because your class t1 is not an action listener.
You can make it an action listener by adding ActionListener to it's implemented interfaces

Expand|Select|Wrap|Line Numbers
  1. public class t1 extends Applet implements ItemListener,ActionListener {
Mar 18 '13 #4
This is the LAST. I dunno how to remove this. XD Thanks for your help. I know you're a good guy! :D


Expand|Select|Wrap|Line Numbers
  1.  
  2.     import java.awt.*;
  3.     import java.applet.*;
  4.     import java.awt.event.*;
  5.  
  6.     public class t1 extends Applet implements ItemListener, ActionListener
  7.     {
  8.         Label alabel = new Label("MDAS Game Program");
  9.         Label slabel = new Label("Choose the answer");
  10.  
  11.         CheckboxGroup qq = new CheckboxGroup();
  12.  
  13.         Label qlabel = new Label("1.) 60 * 3 / 5 + 4 - 38 = ??");
  14.         Checkbox aone = new Checkbox("A. 2",false,qq);
  15.         Checkbox bone = new Checkbox("B. 3",false,qq);
  16.         Checkbox cone = new Checkbox("C. 4",false,qq);
  17.         Checkbox hone = new Checkbox("",true,qq);
  18.  
  19.         Label wlabel = new Label("2.) 5 * 100 / 5 + 400 - 499 = ?? ");
  20.         Checkbox atwo = new Checkbox("A. 1",false,qq);
  21.         Checkbox btwo = new Checkbox("B. 2",false,qq);
  22.         Checkbox ctwo = new Checkbox("C. 3",false,qq);
  23.         Checkbox htwo = new Checkbox("",true,qq);
  24.  
  25.         Label elabel = new Label("3.) 10 * 5 - 10 / 2 + 3 = ??");
  26.         Checkbox athree = new Checkbox("A. 18",false,qq);
  27.         Checkbox bthree = new Checkbox("B. 19",false,qq);
  28.         Checkbox cthree = new Checkbox("C. 17",false,qq);
  29.         Checkbox hthree = new Checkbox("",true,qq);
  30.  
  31.         Label rlabel = new Label("4.) 50 * 5 / 10 + 300 - 10 = ??");
  32.         Checkbox afour = new Checkbox("A. 315",false,qq);
  33.         Checkbox bfour = new Checkbox("B. 316",false,qq);
  34.         Checkbox cfour = new Checkbox("C. 317",false,qq);
  35.         Checkbox hfour = new Checkbox("",true,qq);
  36.  
  37.         Label tlabel = new Label("5.) 10 * 2 / 10 - 300 + 100 = ??");
  38.         Checkbox afive = new Checkbox("A. -198",false,qq);
  39.         Checkbox bfive = new Checkbox("B. 198",false,qq);
  40.         Checkbox cfive = new Checkbox("C. 199",false,qq);
  41.         Checkbox hfive = new Checkbox("",true,qq);
  42.  
  43.         Label ylabel = new Label("6.) 10 * 5 / 50 + 1 - 2 = ??");
  44.         Checkbox asix = new Checkbox("A. 1",false,qq);
  45.         Checkbox bsix = new Checkbox("B. 0",false,qq);
  46.         Checkbox csix = new Checkbox("C. 2",false,qq);
  47.         Checkbox hsix = new Checkbox("",true,qq);
  48.  
  49.         Label ulabel = new Label("7.) 8 * 3 / 3 + 450 - 400 = ??");
  50.         Checkbox aseven = new Checkbox("A. 59",false,qq);
  51.         Checkbox bseven = new Checkbox("B. 58",false,qq);
  52.         Checkbox cseven = new Checkbox("C. 57",false,qq);
  53.         Checkbox hseven = new Checkbox("",true,qq);
  54.  
  55.         Label ilabel = new Label("8.) 5 + 4 * 30 / 10 - 2 = ??");
  56.         Checkbox aeight = new Checkbox("A. 14",false,qq);
  57.         Checkbox beight = new Checkbox("B. 15",false,qq);
  58.         Checkbox ceight = new Checkbox("C. 16",false,qq);
  59.         Checkbox height = new Checkbox("",true,qq);
  60.  
  61.         Label olabel = new Label("9.) 5 + 100 * 1 - 2 / 100  = ??");
  62.         Checkbox anine = new Checkbox("A. 3",false,qq);
  63.         Checkbox bnine = new Checkbox("B. 4",false,qq);
  64.         Checkbox cnine = new Checkbox("C. 6",false,qq);
  65.         Checkbox hnine = new Checkbox("",true,qq);
  66.  
  67.  
  68.         Label plabel = new Label("10.) 7 * 4 / 2 - 10 + 300 = ??");
  69.         Checkbox aten = new Checkbox("A. 303",false,qq);
  70.         Checkbox bten = new Checkbox("B. 304",false,qq);
  71.         Checkbox cten = new Checkbox("C. 604",false,qq);
  72.         Checkbox hten = new Checkbox("",true,qq);
  73.  
  74.         Button done = new Button("Done");
  75.  
  76.  
  77.         public void init()
  78.         {
  79.             add(alabel);
  80.             add(slabel);
  81.  
  82.             add(qlabel);
  83.     add(aone);
  84.         aone.addItemListener(this);
  85.     add(bone);
  86.         bone.addItemListener(this);
  87.     add(cone);
  88.         cone.addItemListener(this);
  89.     add(hone);
  90.         hone.addItemListener(this);
  91.  
  92.     add(wlabel);
  93.     add(atwo);
  94.         atwo.addItemListener(this);
  95.     add(btwo);
  96.         btwo.addItemListener(this);
  97.     add(ctwo);
  98.         ctwo.addItemListener(this);
  99.     add(htwo);
  100.         htwo.addItemListener(this);
  101.  
  102.         add(elabel);
  103.     add(athree);
  104.         athree.addItemListener(this);
  105.     add(bthree);
  106.         bthree.addItemListener(this);
  107.     add(cthree);
  108.         cthree.addItemListener(this);
  109.     add(hthree);
  110.         hthree.addItemListener(this);
  111.  
  112.         add(rlabel);
  113.     add(afour);
  114.         afour.addItemListener(this);
  115.     add(bfour);
  116.         bfour.addItemListener(this);
  117.     add(cfour);
  118.         cfour.addItemListener(this);
  119.     add(hfour);
  120.         hfour.addItemListener(this);
  121.  
  122.         add(tlabel);
  123.     add(afive);
  124.         afive.addItemListener(this);
  125.     add(bfive);
  126.         bfive.addItemListener(this);
  127.     add(cfive);
  128.         cfive.addItemListener(this);
  129.     add(hfive);
  130.         hfive.addItemListener(this);
  131.  
  132.     add(ylabel);
  133.     add(asix);
  134.         asix.addItemListener(this);
  135.     add(bsix);
  136.         bsix.addItemListener(this);
  137.     add(csix);
  138.         csix.addItemListener(this);
  139.     add(hsix);
  140.         hsix.addItemListener(this);
  141.  
  142.     add(ulabel);
  143.     add(aseven);
  144.         aseven.addItemListener(this);
  145.     add(bseven);
  146.         bseven.addItemListener(this);
  147.     add(cseven);
  148.         cseven.addItemListener(this);
  149.     add(hseven);
  150.         hseven.addItemListener(this);
  151.  
  152.         add(ilabel);
  153.     add(aeight);
  154.         aeight.addItemListener(this);
  155.     add(beight);
  156.         beight.addItemListener(this);
  157.     add(ceight);
  158.         ceight.addItemListener(this);
  159.     add(height);
  160.         height.addItemListener(this);
  161.  
  162.         add(olabel);
  163.     add(anine);
  164.         anine.addItemListener(this);
  165.     add(anine);
  166.         bnine.addItemListener(this);
  167.     add(cnine);
  168.         cnine.addItemListener(this);
  169.     add(hnine);
  170.         hnine.addItemListener(this);
  171.  
  172.         add(plabel);
  173.     add(aten);
  174.         aten.addItemListener(this);
  175.     add(bten);
  176.         bten.addItemListener(this);
  177.     add(cten);
  178.         cten.addItemListener(this);
  179.     add(hten);
  180.         hten.addItemListener(this);
  181.  
  182.     add(done);
  183.         done.addActionListener(this);
  184.  
  185.         }
  186.  
  187.  
  188.     public void itemStateChanged(ItemEvent e)
  189.     {
  190.  
  191.         int answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10;
  192.         int totalscore;
  193.  
  194.  
  195.     if (aone.getState()) 
  196.     answer1=1;
  197.     if (atwo.getState())
  198.     answer2=1;
  199.     if (athree.getState())
  200.     answer3=1;
  201.     if (afour.getState())
  202.     answer4=1;
  203.     if (afive.getState())
  204.     answer5=1;
  205.     if (bsix.getState())
  206.     answer6=1;
  207.     if (bseven.getState())
  208.     answer7=1;
  209.     if (beight.getState())
  210.     answer8=1;
  211.     if (bnine.getState())
  212.     answer9=1;
  213.     if (bten.getState())
  214.     answer10=1;
  215.     totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
  216.     repaint();
  217.  
  218.  
  219.     }
  220.  
  221.     public void actionPerformed(ActionEvent e)
  222.     {
  223.     repaint();
  224.  
  225.     int totalscore;
  226.  
  227.     if (done.equals(e.getActionCommand())) 
  228.         {
  229.  
  230.          done.setText("Total score is :" + totalscore);
  231.  
  232.            }
  233.  
  234.     }
  235.  
  236.         }
  237.  
  238.  
  239.  
  240.  
  241.  
THIS IS THE ERROR

C:\Users\caburnay\Desktop\t1.java:231: error: cannot find symbol
done.setText("Total score is :" + totalscore);
^
symbol: method setText(String)
location: variable done of type Button
1 error
Mar 18 '13 #5
r035198x
13,262 8TB
Button doesn't have a setText method. It has a setLabel method.
Mar 18 '13 #6
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     import java.awt.*;
  4.     import java.applet.*;
  5.     import java.awt.event.*;
  6.  
  7.     public class t1 extends Applet implements ItemListener, ActionListener
  8.     {
  9.         Label alabel = new Label("MDAS Game Program");
  10.         Label slabel = new Label("Choose the answer");
  11.  
  12.         CheckboxGroup qq = new CheckboxGroup();
  13.  
  14.         Label qlabel = new Label("1.) 60 * 3 / 5 + 4 - 38 = ??");
  15.         Checkbox aone = new Checkbox("A. 2",false,qq);
  16.         Checkbox bone = new Checkbox("B. 3",false,qq);
  17.         Checkbox cone = new Checkbox("C. 4",false,qq);
  18.         Checkbox hone = new Checkbox("",true,qq);
  19.  
  20.         Label wlabel = new Label("2.) 5 * 100 / 5 + 400 - 499 = ?? ");
  21.         Checkbox atwo = new Checkbox("A. 1",false,qq);
  22.         Checkbox btwo = new Checkbox("B. 2",false,qq);
  23.         Checkbox ctwo = new Checkbox("C. 3",false,qq);
  24.         Checkbox htwo = new Checkbox("",true,qq);
  25.  
  26.         Label elabel = new Label("3.) 10 * 5 - 10 / 2 + 3 = ??");
  27.         Checkbox athree = new Checkbox("A. 18",false,qq);
  28.         Checkbox bthree = new Checkbox("B. 19",false,qq);
  29.         Checkbox cthree = new Checkbox("C. 17",false,qq);
  30.         Checkbox hthree = new Checkbox("",true,qq);
  31.  
  32.         Label rlabel = new Label("4.) 50 * 5 / 10 + 300 - 10 = ??");
  33.         Checkbox afour = new Checkbox("A. 315",false,qq);
  34.         Checkbox bfour = new Checkbox("B. 316",false,qq);
  35.         Checkbox cfour = new Checkbox("C. 317",false,qq);
  36.         Checkbox hfour = new Checkbox("",true,qq);
  37.  
  38.         Label tlabel = new Label("5.) 10 * 2 / 10 - 300 + 100 = ??");
  39.         Checkbox afive = new Checkbox("A. -198",false,qq);
  40.         Checkbox bfive = new Checkbox("B. 198",false,qq);
  41.         Checkbox cfive = new Checkbox("C. 199",false,qq);
  42.         Checkbox hfive = new Checkbox("",true,qq);
  43.  
  44.         Label ylabel = new Label("6.) 10 * 5 / 50 + 1 - 2 = ??");
  45.         Checkbox asix = new Checkbox("A. 1",false,qq);
  46.         Checkbox bsix = new Checkbox("B. 0",false,qq);
  47.         Checkbox csix = new Checkbox("C. 2",false,qq);
  48.         Checkbox hsix = new Checkbox("",true,qq);
  49.  
  50.         Label ulabel = new Label("7.) 8 * 3 / 3 + 450 - 400 = ??");
  51.         Checkbox aseven = new Checkbox("A. 59",false,qq);
  52.         Checkbox bseven = new Checkbox("B. 58",false,qq);
  53.         Checkbox cseven = new Checkbox("C. 57",false,qq);
  54.         Checkbox hseven = new Checkbox("",true,qq);
  55.  
  56.         Label ilabel = new Label("8.) 5 + 4 * 30 / 10 - 2 = ??");
  57.         Checkbox aeight = new Checkbox("A. 14",false,qq);
  58.         Checkbox beight = new Checkbox("B. 15",false,qq);
  59.         Checkbox ceight = new Checkbox("C. 16",false,qq);
  60.         Checkbox height = new Checkbox("",true,qq);
  61.  
  62.         Label olabel = new Label("9.) 5 + 100 * 1 - 2 / 100  = ??");
  63.         Checkbox anine = new Checkbox("A. 3",false,qq);
  64.         Checkbox bnine = new Checkbox("B. 4",false,qq);
  65.         Checkbox cnine = new Checkbox("C. 6",false,qq);
  66.         Checkbox hnine = new Checkbox("",true,qq);
  67.  
  68.  
  69.         Label plabel = new Label("10.) 7 * 4 / 2 - 10 + 300 = ??");
  70.         Checkbox aten = new Checkbox("A. 303",false,qq);
  71.         Checkbox bten = new Checkbox("B. 304",false,qq);
  72.         Checkbox cten = new Checkbox("C. 604",false,qq);
  73.         Checkbox hten = new Checkbox("",true,qq);
  74.  
  75.         Button done = new Button("Done");
  76.  
  77.  
  78.         public void init()
  79.         {
  80.             add(alabel);
  81.             add(slabel);
  82.  
  83.             add(qlabel);
  84.     add(aone);
  85.         aone.addItemListener(this);
  86.     add(bone);
  87.         bone.addItemListener(this);
  88.     add(cone);
  89.         cone.addItemListener(this);
  90.     add(hone);
  91.         hone.addItemListener(this);
  92.  
  93.     add(wlabel);
  94.     add(atwo);
  95.         atwo.addItemListener(this);
  96.     add(btwo);
  97.         btwo.addItemListener(this);
  98.     add(ctwo);
  99.         ctwo.addItemListener(this);
  100.     add(htwo);
  101.         htwo.addItemListener(this);
  102.  
  103.         add(elabel);
  104.     add(athree);
  105.         athree.addItemListener(this);
  106.     add(bthree);
  107.         bthree.addItemListener(this);
  108.     add(cthree);
  109.         cthree.addItemListener(this);
  110.     add(hthree);
  111.         hthree.addItemListener(this);
  112.  
  113.         add(rlabel);
  114.     add(afour);
  115.         afour.addItemListener(this);
  116.     add(bfour);
  117.         bfour.addItemListener(this);
  118.     add(cfour);
  119.         cfour.addItemListener(this);
  120.     add(hfour);
  121.         hfour.addItemListener(this);
  122.  
  123.         add(tlabel);
  124.     add(afive);
  125.         afive.addItemListener(this);
  126.     add(bfive);
  127.         bfive.addItemListener(this);
  128.     add(cfive);
  129.         cfive.addItemListener(this);
  130.     add(hfive);
  131.         hfive.addItemListener(this);
  132.  
  133.     add(ylabel);
  134.     add(asix);
  135.         asix.addItemListener(this);
  136.     add(bsix);
  137.         bsix.addItemListener(this);
  138.     add(csix);
  139.         csix.addItemListener(this);
  140.     add(hsix);
  141.         hsix.addItemListener(this);
  142.  
  143.     add(ulabel);
  144.     add(aseven);
  145.         aseven.addItemListener(this);
  146.     add(bseven);
  147.         bseven.addItemListener(this);
  148.     add(cseven);
  149.         cseven.addItemListener(this);
  150.     add(hseven);
  151.         hseven.addItemListener(this);
  152.  
  153.         add(ilabel);
  154.     add(aeight);
  155.         aeight.addItemListener(this);
  156.     add(beight);
  157.         beight.addItemListener(this);
  158.     add(ceight);
  159.         ceight.addItemListener(this);
  160.     add(height);
  161.         height.addItemListener(this);
  162.  
  163.         add(olabel);
  164.     add(anine);
  165.         anine.addItemListener(this);
  166.     add(anine);
  167.         bnine.addItemListener(this);
  168.     add(cnine);
  169.         cnine.addItemListener(this);
  170.     add(hnine);
  171.         hnine.addItemListener(this);
  172.  
  173.         add(plabel);
  174.     add(aten);
  175.         aten.addItemListener(this);
  176.     add(bten);
  177.         bten.addItemListener(this);
  178.     add(cten);
  179.         cten.addItemListener(this);
  180.     add(hten);
  181.         hten.addItemListener(this);
  182.  
  183.     add(done);
  184.         done.addActionListener(this);
  185.  
  186.         }
  187.  
  188.  
  189.     public void itemStateChanged(ItemEvent e)
  190.     {
  191.  
  192.     int answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, totalscore;
  193.  
  194.  
  195.  
  196.     if (aone.getState()) 
  197.     answer1=1;
  198.     if (atwo.getState())
  199.     answer2=1;
  200.     if (athree.getState())
  201.     answer3=1;
  202.     if (afour.getState())
  203.     answer4=1;
  204.     if (afive.getState())
  205.     answer5=1;
  206.     if (bsix.getState())
  207.     answer6=1;
  208.     if (bseven.getState())
  209.     answer7=1;
  210.     if (beight.getState())
  211.     answer8=1;
  212.     if (bnine.getState())
  213.     answer9=1;
  214.     if (bten.getState())
  215.     answer10=1;
  216.     totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
  217.     repaint();
  218.  
  219.  
  220.     }
  221.  
  222.     public void actionPerformed(ActionEvent e)
  223.     {
  224.     repaint();
  225.  
  226.  
  227.  
  228.  
  229.     if (done.equals(e.getActionCommand())) 
  230.         {
  231.  
  232.          done.setLabel("Total score is: " + totalscore);
  233.            }
  234.  
  235.     }
  236.  
  237.         }
  238.  
  239.  
  240.  
  241.  
  242.  
--------------------Configuration: <Default>--------------------
C:\Users\caburnay\Desktop\t1.java:232: error: cannot find symbol
done.setLabel("Total score is: " + totalscore);
^
symbol: variable totalscore
location: class t1
1 error

Process completed.

and IF I PUT "int totalscore;"
it gets 11 errors

--------------------Configuration: <Default>--------------------
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer1 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer2 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer3 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer4 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer5 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer6 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer7 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer8 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer9 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:216: error: variable answer10 might not have been initialized
totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
^
C:\Users\caburnay\Desktop\t1.java:232: error: variable totalscore might not have been initialized
done.setLabel("Total score is: " + totalscore);
^
11 errors

Process completed.
Mar 18 '13 #7
r035198x
13,262 8TB
So initialize all those variables that may not have been intialized.
Mar 18 '13 #8
i have declared it all at (nothing happened)


Expand|Select|Wrap|Line Numbers
  1. public void itemStateChanged(ItemEvent e)
  2.     {
  3.  
  4.     int answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, totalscore;
  5.  
Mar 18 '13 #9
r035198x
13,262 8TB
declaring is not the same as initializing.

declaring is
Expand|Select|Wrap|Line Numbers
  1. int answer1;
initializing is
Expand|Select|Wrap|Line Numbers
  1. int answer1 = 0;
Mar 18 '13 #10
how to put spaces between numbers?

for example

1.) 2 + 23+25+ 5+
a. 23 b. 23 c. 1

2.) 2- 4- 4- 2
a. 20 b. 19 c. 10

Expand|Select|Wrap|Line Numbers
  1.  
  2.     import java.awt.*;
  3.     import java.applet.*;
  4.     import java.awt.event.*;
  5.  
  6.     public class t1 extends Applet implements ItemListener, ActionListener
  7.     {
  8.         Label alabel = new Label("MDAS Game Program");
  9.         Label slabel = new Label("Choose the answer");
  10.  
  11.         CheckboxGroup qq = new CheckboxGroup();
  12.  
  13.         Label qlabel = new Label("1.) 60 * 3 / 5 + 4 - 38 = ??");
  14.         Checkbox aone = new Checkbox("A. 2",false,qq);
  15.         Checkbox bone = new Checkbox("B. 3",false,qq);
  16.         Checkbox cone = new Checkbox("C. 4",false,qq);
  17.         Checkbox hone = new Checkbox("",true,qq);
  18.  
  19.         CheckboxGroup ww = new CheckboxGroup();
  20.  
  21.         Label wlabel = new Label("2.) 5 * 100 / 5 + 400 - 499 = ?? ");
  22.         Checkbox atwo = new Checkbox("A. 1",false,qq);
  23.         Checkbox btwo = new Checkbox("B. 2",false,qq);
  24.         Checkbox ctwo = new Checkbox("C. 3",false,qq);
  25.         Checkbox htwo = new Checkbox("",true,qq);
  26.  
  27.         CheckboxGroup ee = new CheckboxGroup();
  28.  
  29.         Label elabel = new Label("3.) 10 * 5 - 10 / 2 + 3 = ??");
  30.         Checkbox athree = new Checkbox("A. 18",false,qq);
  31.         Checkbox bthree = new Checkbox("B. 19",false,qq);
  32.         Checkbox cthree = new Checkbox("C. 17",false,qq);
  33.         Checkbox hthree = new Checkbox("",true,qq);
  34.  
  35.          CheckboxGroup rr = new CheckboxGroup();
  36.  
  37.         Label rlabel = new Label("4.) 50 * 5 / 10 + 300 - 10 = ??");
  38.         Checkbox afour = new Checkbox("A. 315",false,qq);
  39.         Checkbox bfour = new Checkbox("B. 316",false,qq);
  40.         Checkbox cfour = new Checkbox("C. 317",false,qq);
  41.         Checkbox hfour = new Checkbox("",true,qq);
  42.  
  43.         CheckboxGroup tt = new CheckboxGroup();
  44.  
  45.         Label tlabel = new Label("5.) 10 * 2 / 10 - 300 + 100 = ??");
  46.         Checkbox afive = new Checkbox("A. -198",false,qq);
  47.         Checkbox bfive = new Checkbox("B. 198",false,qq);
  48.         Checkbox cfive = new Checkbox("C. 199",false,qq);
  49.         Checkbox hfive = new Checkbox("",true,qq);
  50.  
  51.         CheckboxGroup yy = new CheckboxGroup();
  52.  
  53.         Label ylabel = new Label("6.) 10 * 5 / 50 + 1 - 2 = ??");
  54.         Checkbox asix = new Checkbox("A. 1",false,qq);
  55.         Checkbox bsix = new Checkbox("B. 0",false,qq);
  56.         Checkbox csix = new Checkbox("C. 2",false,qq);
  57.         Checkbox hsix = new Checkbox("",true,qq);
  58.  
  59.         CheckboxGroup uu = new CheckboxGroup();
  60.  
  61.         Label ulabel = new Label("7.) 8 * 3 / 3 + 450 - 400 = ??");
  62.         Checkbox aseven = new Checkbox("A. 59",false,qq);
  63.         Checkbox bseven = new Checkbox("B. 58",false,qq);
  64.         Checkbox cseven = new Checkbox("C. 57",false,qq);
  65.         Checkbox hseven = new Checkbox("",true,qq);
  66.  
  67.         CheckboxGroup ii = new CheckboxGroup();
  68.  
  69.         Label ilabel = new Label("8.) 5 + 4 * 30 / 10 - 2 = ??");
  70.         Checkbox aeight = new Checkbox("A. 14",false,qq);
  71.         Checkbox beight = new Checkbox("B. 15",false,qq);
  72.         Checkbox ceight = new Checkbox("C. 16",false,qq);
  73.         Checkbox height = new Checkbox("",true,qq);
  74.  
  75.         CheckboxGroup oo = new CheckboxGroup();
  76.  
  77.         Label olabel = new Label("9.) 5 + 100 * 1 - 2 / 100  = ??");
  78.         Checkbox anine = new Checkbox("A. 3",false,qq);
  79.         Checkbox bnine = new Checkbox("B. 4",false,qq);
  80.         Checkbox cnine = new Checkbox("C. 6",false,qq);
  81.         Checkbox hnine = new Checkbox("",true,qq);
  82.  
  83.  
  84.         Label plabel = new Label("10.) 7 * 4 / 2 - 10 + 300 = ??");
  85.         Checkbox aten = new Checkbox("A. 303",false,qq);
  86.         Checkbox bten = new Checkbox("B. 304",false,qq);
  87.         Checkbox cten = new Checkbox("C. 604",false,qq);
  88.         Checkbox hten = new Checkbox("",true,qq);
  89.  
  90.         Button done = new Button("Done");
  91.  
  92.  
  93.         public void init()
  94.         {
  95.             add(alabel);
  96.             add(slabel);
  97.  
  98.             add(qlabel);
  99.     add(aone);
  100.         aone.addItemListener(this);
  101.     add(bone);
  102.         bone.addItemListener(this);
  103.     add(cone);
  104.         cone.addItemListener(this);
  105.     add(hone);
  106.         hone.addItemListener(this);
  107.  
  108.     add(wlabel);
  109.     add(atwo);
  110.         atwo.addItemListener(this);
  111.     add(btwo);
  112.         btwo.addItemListener(this);
  113.     add(ctwo);
  114.         ctwo.addItemListener(this);
  115.     add(htwo);
  116.         htwo.addItemListener(this);
  117.  
  118.         add(elabel);
  119.     add(athree);
  120.         athree.addItemListener(this);
  121.     add(bthree);
  122.         bthree.addItemListener(this);
  123.     add(cthree);
  124.         cthree.addItemListener(this);
  125.     add(hthree);
  126.         hthree.addItemListener(this);
  127.  
  128.         add(rlabel);
  129.     add(afour);
  130.         afour.addItemListener(this);
  131.     add(bfour);
  132.         bfour.addItemListener(this);
  133.     add(cfour);
  134.         cfour.addItemListener(this);
  135.     add(hfour);
  136.         hfour.addItemListener(this);
  137.  
  138.         add(tlabel);
  139.     add(afive);
  140.         afive.addItemListener(this);
  141.     add(bfive);
  142.         bfive.addItemListener(this);
  143.     add(cfive);
  144.         cfive.addItemListener(this);
  145.     add(hfive);
  146.         hfive.addItemListener(this);
  147.  
  148.     add(ylabel);
  149.     add(asix);
  150.         asix.addItemListener(this);
  151.     add(bsix);
  152.         bsix.addItemListener(this);
  153.     add(csix);
  154.         csix.addItemListener(this);
  155.     add(hsix);
  156.         hsix.addItemListener(this);
  157.  
  158.     add(ulabel);
  159.     add(aseven);
  160.         aseven.addItemListener(this);
  161.     add(bseven);
  162.         bseven.addItemListener(this);
  163.     add(cseven);
  164.         cseven.addItemListener(this);
  165.     add(hseven);
  166.         hseven.addItemListener(this);
  167.  
  168.         add(ilabel);
  169.     add(aeight);
  170.         aeight.addItemListener(this);
  171.     add(beight);
  172.         beight.addItemListener(this);
  173.     add(ceight);
  174.         ceight.addItemListener(this);
  175.     add(height);
  176.         height.addItemListener(this);
  177.  
  178.         add(olabel);
  179.     add(anine);
  180.         anine.addItemListener(this);
  181.     add(anine);
  182.         bnine.addItemListener(this);
  183.     add(cnine);
  184.         cnine.addItemListener(this);
  185.     add(hnine);
  186.         hnine.addItemListener(this);
  187.  
  188.         add(plabel);
  189.     add(aten);
  190.         aten.addItemListener(this);
  191.     add(bten);
  192.         bten.addItemListener(this);
  193.     add(cten);
  194.         cten.addItemListener(this);
  195.     add(hten);
  196.         hten.addItemListener(this);
  197.  
  198.     add(done);
  199.         done.addActionListener(this);
  200.  
  201.         }
  202.  
  203.  
  204.     public void itemStateChanged(ItemEvent e)
  205.     {
  206.  
  207.     int answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, totalscore;
  208.  
  209.  
  210.  
  211.  
  212.     if (aone.getState()) 
  213.     answer1=1;
  214.     if (atwo.getState())
  215.     answer2=1;
  216.     if (athree.getState())
  217.     answer3=1;
  218.     if (afour.getState())
  219.     answer4=1;
  220.     if (afive.getState())
  221.     answer5=1;
  222.     if (bsix.getState())
  223.     answer6=1;
  224.     if (bseven.getState())
  225.     answer7=1;
  226.     if (beight.getState())
  227.     answer8=1;
  228.     if (bnine.getState())
  229.     answer9=1;
  230.     if (bten.getState())
  231.     answer10=1;
  232.  
  233.  
  234.  
  235.     }
  236.  
  237.     public void actionPerformed(ActionEvent e)
  238.     {
  239.     repaint();
  240.  
  241.            int totalscore = 0; 
  242.         int answer1 = 0; 
  243.         int answer2 = 0;
  244.         int answer3 = 0;
  245.         int answer4 = 0;
  246.         int answer5 = 0;
  247.         int answer6 = 0;
  248.         int answer7 = 0;
  249.         int answer8 = 0;
  250.         int answer9 = 0;
  251.         int answer10 = 0;
  252.  
  253.  
  254.     if (done.equals(e.getActionCommand())) 
  255.         {
  256.             totalscore = answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7 + answer8 + answer9 + answer10;
  257.     repaint();    
  258.          done.setLabel("Total score is: " + totalscore);
  259.            }
  260.  
  261.     }
  262.  
  263.         }
  264.  
  265.  
  266.  
  267.  
  268.  
because my code looks like this (output)

http://tinypic.com/r/2vmhkzk/6
Mar 18 '13 #11
r035198x
13,262 8TB
Before you do that, how do you know if the user has answered the question correctly or not? Is there a list of answers provided or does the program calculate the correct answer from the question text?
Mar 18 '13 #12
There is a list of answers provided

ex.

1.) 1 + 1 = ??

A.) 2 B.) 3 C.) 4
Mar 18 '13 #13
r035198x
13,262 8TB
But how is the correct answer provided? i.e How do you know if the user has selected the correct answer or not?
Mar 18 '13 #14
The program just count the correct answers

Expand|Select|Wrap|Line Numbers
  1. if (aone.getState()) 
  2.     answer1=1;
  3.     if (atwo.getState())
  4.     answer2=1;
  5.     if (athree.getState())
  6.     answer3=1;
  7.     if (afour.getState())
  8.     answer4=1;
  9.     if (afive.getState())
  10.     answer5=1;
  11.     if (bsix.getState())
  12.     answer6=1;
  13.     if (bseven.getState())
  14.     answer7=1;
  15.     if (beight.getState())
  16.     answer8=1;
  17.     if (bnine.getState())
  18.     answer9=1;
  19.     if (bten.getState())
  20.     answer10=1;
Mar 18 '13 #15
r035198x
13,262 8TB
Ok. There are some fundamentals that you still need to learn and apply in your code.

1.) Don't repeat similar code. Put the repeating logic into a method and call that many times rather than repeat the whole thing. Imagine if you were asked to make 100 questions instead of ten. What would your code look like then?
2.) Read about LayoutManagers if you want to write programs with a GUI.
3.) Separate data from how it is formatted and presented to the user. e.g Make it easy to change from using A,B, C as labels of your options to using (i), (ii), (iii)

Here is an example structure of applying these principles to your program. The calculate method is just a trick to use Java's Javascript engine to calculate the value of an expression.


Expand|Select|Wrap|Line Numbers
  1. public class Prog extends Applet implements ItemListener, ActionListener {
  2.     private static final long serialVersionUID = 1L;
  3.     int totalscore;
  4.     Button done = new Button("Done");
  5.  
  6.     public void createQuestionBox(String question, String[] options, Panel panel) {
  7.         CheckboxGroup qq = new CheckboxGroup();
  8.         Label label = new Label(question + " = ??");
  9.         // Each question gets its own panel
  10.         Panel p = new Panel(new FlowLayout(FlowLayout.LEFT));
  11.         p.add(label);
  12.         for (int i = 0; i < options.length; i++) {
  13.             Checkbox ch = new Checkbox(options[i], false, qq);
  14.             ch.setName(question);
  15.             ch.addItemListener(this);
  16.             p.add(ch);
  17.         }
  18.         // each question's panel is added to the main panel
  19.         panel.add(p);
  20.     }
  21.  
  22.     public void init() {
  23.         setLayout(new BorderLayout());
  24.         setSize(400, 400);
  25.         Label alabel = new Label("MDAS Game Program");
  26.         add(alabel, BorderLayout.PAGE_START);
  27.         Label slabel = new Label("Choose the answer");
  28.         add(slabel, BorderLayout.NORTH);
  29.         // This panel will hold all the questions
  30.         Panel panel = new Panel();
  31.         panel.setLayout(new GridLayout(3, 1));
  32.         createQuestionBox("60 * 3 / 5 + 4 - 38", new String[] { "2", "3", "4" }, panel);
  33.         createQuestionBox("5 * 100 / 5 + 400 - 499", new String[] { "1", "2", "3" }, panel);
  34.         createQuestionBox("10 * 5 - 10 / 2 + 3", new String[] { "18", "19", "17" }, panel);
  35.         add(panel, BorderLayout.CENTER);
  36.         done.addActionListener(this);
  37.         add(done, BorderLayout.SOUTH);
  38.     }
  39.  
  40.     public void itemStateChanged(ItemEvent e) {
  41.         Checkbox ch = (Checkbox) e.getSource();
  42.         // calculate answer and save result somewhere, e.g in a Map
  43.         System.out.println("Correct is "+ calculate(ch.getName()) + ", user answer is " + e.getItem());
  44.     }
  45.  
  46.     public void actionPerformed(ActionEvent e) {
  47.         // add the answers from where you were saving them to
  48.     }
  49.  
  50.  
  51.     /**
  52.      * @param question
  53.      * @return result of evaluating an expression
  54.      */
  55.     public String calculate(String question) {
  56.         try {
  57.             ScriptEngineManager factory = new ScriptEngineManager();
  58.             // create a JavaScript engine
  59.             ScriptEngine engine = factory.getEngineByName("JavaScript");
  60.             // evaluate JavaScript code from String
  61.             return engine.eval(question).toString();
  62.         } catch (Exception ex) {
  63.             ex.printStackTrace();
  64.             throw new RuntimeException(ex);
  65.         }
  66.     }
  67. }
Mar 18 '13 #16
Rabbit
12,516 Expert Mod 8TB
@joelpawns, please read the FAQ before posting again. We only allow one question per thread on the forum.
Mar 18 '13 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: CHU Run-min | last post by:
The following code was written by me. It is used for evaluating constant expressions. I think it is good. Any criticism? namespace Churunmin.HelloWorld {
2
by: dobbouk | last post by:
I'm getting these 2 errors and haven't got a clue whats causing them please java 43: illegal start of type for (i=0;i< Number_of_squares; i++) java 215 identifier expected } ...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I have downloaded and installed the trial but the File New is greyed out so I can't get started! What have I not done that I need to do? --
1
by: biznitchil | last post by:
it is a bit jacked up, but copy and paste it into an IDE(i use jGRASP) plz help me...it worked just fine before I added the menu..you can e-mail me if you have any <Removed> import java.util.*; ...
4
by: rajesh619 | last post by:
I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors....
5
by: xirowei | last post by:
i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable? below is my code: import javax.servlet.*; import javax.servlet.http.*; import...
3
by: gokulagopal | last post by:
I am sending the code please help me in finding the error and correct it.The code is below: import java.awt.*; import java.awt.event.*; class CalcFrame extends Frame { ...
2
by: mole40 | last post by:
Hello! I'm very new to writing java. And I keep getting the "Illegal Start of Type" error. I've tried everything to fix it. can anyone help me? import java.util.*; class ClockTalk { public...
1
by: John Luttig | last post by:
import java.awt.*; import java.awt.event.* ; import java.awt.image.*; import java.awt.geom.* ; import javax.swing.* ; import javax.swing.event.* ; public class NPCObject { public void...
2
by: Christina Arena | last post by:
I'm just starting to learn java as part of a class I am required to take as a math major. I am feeling completely lost. I guess we're supposed to learn this as independently as possible. I was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.