Help with math program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Help with math program

 
0
  #1
Jan 7th, 2009
Hey, I am tutoring this kid in java and he has to use TurtleGraphics and BreezySwing, neither of which I have used in a while. I am having trouble figuring out some of his problems. I will post both his code and how I altered his code. I am wondering about a couple of things, one is he uses the method addLabel(), which I cant get to work and am wondering if that is really a method?

Also, he need a main method and I'm not sure what to put in the main method to make it all run.
Here is his code, below this will be how I altered the code:
  1. //"Math is our Game" - A math testing program for First through Fourth grade students
  2.  
  3. //Import GUI packages
  4.  
  5.  
  6. import javax.swing.*;
  7. import BreezySwing.*;
  8. import java.awt.*;
  9. import java.util.Random;
  10. import TurtleGraphics.*;
  11.  
  12. public class Math extends GBFrame
  13.  
  14. {
  15.  
  16. //declare variables for the window objects
  17.  
  18. private JLabel greetingLabel;
  19. private JLabel additionLabel;
  20. private JLabel subtractionLabel;
  21. private JLabel multiplicationLabel;
  22. private JLabel divisionLabel;
  23. private JLabel randomLabel;
  24. private JLabel grade1Label;
  25. private JLabel grade2Label;
  26. private JLabel inputLabel;
  27. private JLabel gradeLabel;
  28. private JLabel questionLabel;
  29.  
  30. private JTextField greetingField;
  31. private JTextField additionField;
  32. private JTextField subtractionField;
  33. private JTextField multiplicationField;
  34. private JTextField divisionField;
  35. private JTextField randomField;
  36. private JTextField grade1Field;
  37. private JTextField grade2Field;
  38. private JTextField inputField;
  39. private JTextField questionField;
  40.  
  41. private JButton greetingButton;
  42. private JButton additionButton;
  43. private JButton subtractionButton;
  44. private JButton multiplicationButton;
  45. private JButton divisionButton;
  46. private JButton randomButton;
  47. private JButton grade1Button;
  48. private JButton grade2Button;
  49. private JButton grade3Button;
  50. private JButton grade4Button;
  51. private JButton inputButton;
  52. private JButton quitButton;
  53. private JButton questionButton;
  54. int generator1=0;
  55. int generator2=0;
  56. int generator3=0;
  57. int generator4=0;
  58.  
  59.  
  60. //constructor
  61.  
  62. public Math()
  63.  
  64. {
  65.  
  66. //Open greeting screen with welcome and Start button
  67.  
  68. greetingLabel = addLabel ("Greeting" ,1,1,1,1);
  69. greetingField = addJTextField ("Welcome to Math is our Game, a math skills program by Nine Fingers and Ten Toes Software, Inc. ,2,1,1,1);
  70. greetingButton = addButton ("Start" ,1,3,1,1);
  71.  
  72. //Button clicked to start game
  73.  
  74. public void buttonClicked (JButton buttonObj)
  75.  
  76. //Select grade level - Instantiate and add window objects to grade level window
  77.  
  78. gradeLabel = addlabel ("Please select grade level of math problem." ,1,1,1,1);
  79. grade1 = addButton ("First Grade" ,1,2,1,1);
  80. grade2 = addButton ("Second Grade" ,1,3,1,1);
  81. grade3 = addButton ("Third Grade" ,1,4,1,1);
  82. grade4 = addButton ("Fourth Grade" ,1,5,1,1);
  83. quitButton = addButton ("Quit" ,1,6,1,1);
  84.  
  85. //First grade selected
  86.  
  87. public void buttonClicked (JButton buttonObj)
  88. if (buttonObj == grade1)
  89.  
  90. {
  91.  
  92. //Generate numbers for First Grade problem
  93.  
  94. generator1 = generator.nextInt(10);
  95. generator2 = generator.nextInt(10);
  96.  
  97. //Select type of problem
  98.  
  99. //instantiates and add window objects to the window
  100.  
  101. additionLabel = addLabel ("Addition" ,1,1,1,1);
  102. subtractionLabel = addLabel ("Subtraction" ,1,2,1,1);
  103. multiplicationLabel = addLabel ("Multiplication" ,1,3,1,1);
  104. divisionLabel = addLabel ("Division" ,1,4,1,1);
  105. randomLabel = addLabel ("Random" ,1,5,1,1);
  106.  
  107. additionField = addJTextField ("0" ,2,1,1,1);
  108. subtractionField = addJTextField ("0" ,3,1,1,1);
  109. multiplicationField = addJTextField ("0" ,4,1,1,1);
  110. divisionField = addJTextField ("0" ,5,1,1,1);
  111. randomField = addJTextField ("0" ,6,1,1,1);
  112.  
  113. additionButton = addButton ("1+1" ,3,1,1,1);
  114. subtractionButton = addButton ("1-1" ,4,1,1,1);
  115. multiplicationButton = addButton ("1*1" ,5,1,1,1);
  116. divisionButton = addButton ("1/1" ,6,1,1,1);
  117. randomButton = addButton ("Random Equation" ,7,1,1,1);
  118.  
  119. }
  120.  
  121. public void buttonClicked (JButton buttonObj)
  122.  
  123.  
  124. {
  125.  
  126. //local variables
  127.  
  128. Math1 Math = new Math1();
  129.  
  130. //determine which button was clicked
  131.  
  132. if (buttonObj == grade1Button)
  133.  
  134. {
  135.  
  136. //setting and getting the numbers for the equations
  137.  
  138. Math.setaddition(additionField.getNumber());
  139. subtractionField.setNumber (Math.getsubtraction());
  140. Math.setmultiplication(multiplicationField.getNumber());
  141. divisionField.setNumber (Math.getdivision());
  142. Math.setrandom(randomField.getNumber());
  143. }
  144.  
  145.  
  146. else
  147.  
  148. {
  149.  
  150. Math.setsubtraction(subtractionField.getNumber());
  151. additionField.setNumber(Math.getaddition());
  152. Math.setdivision(divisionField.getNumber());
  153. multiplicationField.setNumber (Math.getmultiplication());
  154. randomField.setNumber (Math.getrandom());
  155.  
  156. //1st grade
  157.  
  158. generator1 = generator.nextInt(10);
  159. generator2 = generator.nextInt(10);
  160. if (input == additionButton)
  161. additionLabel = addLabel ("Addition Problem" ,1,1,1,1);
  162. additionField = addJTextField ("Please Input the two numbers to add together:" ,1,2,1,1);
  163. additionButton = addButton ("+" ,1,3,1,1);
  164.  
  165. if (input == generator1 && input == generator2)
  166. {
  167. questionLabel = addLabel ("Addition Problem" ,1,1,1,1);
  168. questionField = addJTextField ("You got the problem right, CONGRATULATIONS!!" ,1,2,1,1);
  169. questionButton = addButton ("1+1=2" ,1,3,1,1);
  170.  
  171.  
  172. }
  173. }
  174. }
  175.  
  176. public class Math2
  177.  
  178. {
  179.  
  180. private double Mathsubtraction;
  181.  
  182. public void setaddition(double Math)
  183.  
  184. {
  185.  
  186. Mathsubtraction = Math;
  187.  
  188. }
  189.  
  190. public double setsubtraction(double math)
  191.  
  192. {
  193.  
  194. return Mathsubtraction;
  195.  
  196. }
  197.  
  198. public double getsubtraction()
  199.  
  200. {
  201.  
  202. return Mathsubtraction;
  203.  
  204. }
  205.  
  206. public double getaddition()
  207.  
  208. {
  209. double Mathaddition = 0;
  210.  
  211. return Mathaddition;
  212.  
  213. }
  214. }
  215.  

Here is how I altered the code:
  1. //"Math is our Game" - A math testing program for First through Fourth grade students
  2.  
  3. //Import GUI packages
  4.  
  5.  
  6. import javax.swing.*;
  7. import BreezySwing.*;
  8. import java.awt.*;
  9. import java.util.Random;
  10. import TurtleGraphics.*;
  11.  
  12. public class Math extends GBFrame
  13.  
  14. {
  15.  
  16. //declare variables for the window objects
  17.  
  18. private JLabel greetingLabel;
  19. private JLabel additionLabel;
  20. private JLabel subtractionLabel;
  21. private JLabel multiplicationLabel;
  22. private JLabel divisionLabel;
  23. private JLabel randomLabel;
  24. private JLabel grade1Label;
  25. private JLabel grade2Label;
  26. private JLabel inputLabel;
  27. private JLabel gradeLabel;
  28. private JLabel questionLabel;
  29.  
  30. private JTextField greetingField;
  31. private JTextField additionField;
  32. private JTextField subtractionField;
  33. private JTextField multiplicationField;
  34. private JTextField divisionField;
  35. private JTextField randomField;
  36. private JTextField grade1Field;
  37. private JTextField grade2Field;
  38. private JTextField inputField;
  39. private JTextField questionField;
  40.  
  41. private JButton greetingButton;
  42. private JButton additionButton;
  43. private JButton subtractionButton;
  44. private JButton multiplicationButton;
  45. private JButton divisionButton;
  46. private JButton randomButton;
  47. private JButton grade1Button;
  48. private JButton grade2Button;
  49. private JButton grade3Button;
  50. private JButton grade4Button;
  51. private JButton inputButton;
  52. private JButton quitButton;
  53. private JButton questionButton;
  54. int generator1=0;
  55. int generator2=0;
  56. int generator3=0;
  57. int generator4=0;
  58.  
  59. Random generator = new Random(5);
  60. //constructor
  61. public Math()
  62.  
  63. {
  64.  
  65. //Open greeting screen with welcome and Start button
  66.  
  67. greetingLabel = new JLabel("Greeting");//greetingLabel.setText("Greeting"); //= addLabel ("Greeting" ,1,1,1,1);
  68. greetingField = new JTextField("Welcome to Math is our Game, a math skills program by Nine Fingers and Ten Toes Software, Inc.");// ,2,1,1,1);
  69. greetingButton = addButton ("Start" ,1,3,1,1);
  70.  
  71. //Button clicked to start game
  72. }
  73. public void buttonClicked(JButton buttonObj)
  74. {
  75. //Select grade level - Instantiate and add window objects to grade level window
  76.  
  77. gradeLabel = new JLabel("Please select grade level of math problem.");// ,1,1,1,1);
  78. grade1Button = addButton ("First Grade" ,1,2,1,1);
  79. grade2Button = addButton ("Second Grade" ,1,3,1,1);
  80. grade3Button = addButton ("Third Grade" ,1,4,1,1);
  81. grade4Button = addButton ("Fourth Grade" ,1,5,1,1);
  82. quitButton = addButton ("Quit" ,1,6,1,1);
  83.  
  84. //First grade selected
  85.  
  86. //public void buttonClicked (JButton buttonObj)
  87. //{
  88. if (buttonObj == grade1Button)
  89. {
  90.  
  91. //Generate numbers for First Grade problem
  92.  
  93. generator1 = generator.nextInt(10);
  94. generator2 = generator.nextInt(10);
  95.  
  96. //Select type of problem
  97.  
  98. //instantiates and add window objects to the window
  99.  
  100. additionLabel = addLabel ("Addition" ,1,1,1,1);
  101. subtractionLabel = addLabel ("Subtraction" ,1,2,1,1);
  102. multiplicationLabel = addLabel ("Multiplication" ,1,3,1,1);
  103. divisionLabel = addLabel ("Division" ,1,4,1,1);
  104. randomLabel = addLabel ("Random" ,1,5,1,1);
  105.  
  106. additionField = new JTextField("0");
  107. subtractionField = new JTextField("0");
  108. multiplicationField = new JTextField("0");
  109. divisionField = new JTextField("0");
  110. randomField = new JTextField("0");
  111.  
  112. additionButton = addButton ("1+1" ,3,1,1,1);
  113. subtractionButton = addButton ("1-1" ,4,1,1,1);
  114. multiplicationButton = addButton ("1*1" ,5,1,1,1);
  115. divisionButton = addButton ("1/1" ,6,1,1,1);
  116. randomButton = addButton ("Random Equation" ,7,1,1,1);
  117.  
  118. }
  119.  
  120. //public void buttonClicked (JButton buttonObj)
  121.  
  122.  
  123. //{
  124.  
  125. //local variables
  126.  
  127. Math2 math1 = new Math2();
  128.  
  129. //determine which button was clicked
  130.  
  131. if (buttonObj == grade1Button)
  132.  
  133. {
  134.  
  135. //setting and getting the numbers for the equations
  136.  
  137. math1.setaddition(Double.parseDouble(additionField.getText()));
  138. //subtractionField.setNumber(math1.getsubtraction());
  139. //math1.setmultiplication(Double.parseDouble(multiplicationField.getText()));
  140. //divisionField.setNumber (math1.getdivision());
  141. //math1.setrandom(randomField.getText());
  142. }
  143.  
  144.  
  145. else
  146.  
  147. {
  148.  
  149. /*Math.setsubtraction(subtractionField.getNumber());
  150.   additionField.setNumber(Math.getaddition());
  151.   Math.setdivision(divisionField.getNumber());
  152.   multiplicationField.setNumber (Math.getmultiplication());
  153.   randomField.setNumber (Math.getrandom());*/
  154.  
  155. //1st grade
  156.  
  157. generator1 = generator.nextInt(10);
  158. generator2 = generator.nextInt(10);
  159. /* if (input == additionButton)
  160.   additionLabel = addLabel ("Addition Problem" ,1,1,1,1);
  161.   additionField = addJTextField ("Please Input the two numbers to add together:" ,1,2,1,1);
  162.   additionButton = addButton ("+" ,1,3,1,1);
  163.  
  164.   if (input == generator1 && input == generator2)
  165.   {
  166.   questionLabel = addLabel ("Addition Problem" ,1,1,1,1);
  167.   questionField = addJTextField ("You got the problem right, CONGRATULATIONS!!" ,1,2,1,1);
  168.   questionButton = addButton ("1+1=2" ,1,3,1,1); */
  169.  
  170.  
  171. }
  172. }
  173. }
  174.  
  175. class Math2
  176.  
  177. {
  178.  
  179. private double Mathsubtraction;
  180.  
  181. public void setaddition(double math)
  182.  
  183. {
  184.  
  185. Mathsubtraction = math;
  186.  
  187. }
  188.  
  189. public double setsubtraction(double math)
  190.  
  191. {
  192.  
  193. return Mathsubtraction;
  194.  
  195. }
  196.  
  197. public double getsubtraction()
  198.  
  199. {
  200.  
  201. return Mathsubtraction;
  202.  
  203. }
  204.  
  205. public double getaddition()
  206.  
  207. {
  208. double Mathaddition = 0;
  209.  
  210. return Mathaddition;
  211.  
  212. }
  213. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Help with math program

 
0
  #2
Jan 7th, 2009
I figured out the addLabel thing. The main problem I am having now is wth the main method, I dont know what to put in it to make the program run.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with math program

 
0
  #3
Jan 7th, 2009
How have you tested anything at all in order to alter it if you do not know what should go into main() to make it run?
No one here is going to be able to tell you for certain, since you are extending some "GBFrame" class which is not a standard JDK class, but you probably need to create an instance of Math, set a few properties on it, and set it to be visible.

Fixing up the formatting couldn't hurt as well, since the indentation is all screwed up in what is posted above. That would make it a lot easier for you to follow the logical blocks within the code.

Edit: And rename the classes to something more meaningful than "Math" and "Math2". "Math" certainly doesn't denote any logical relationship to a "GBFrame".
Last edited by Ezzaral; Jan 7th, 2009 at 5:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Help with math program

 
0
  #4
Jan 7th, 2009
Ok I'll try that. The code isn't mine, if I had written it I would have definitely formatted it better and named stuff better.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with math program

 
0
  #5
Jan 7th, 2009
It looks like GBFrame is one of the BreezySwing components. If you don't have any API docs for it, take a look at this link: http://faculty.cs.wwu.edu/martin/sof...I/GBFrame.html
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Help with math program

 
0
  #6
Jan 7th, 2009
that right there may solve my problem. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC