| | |
Help with math program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
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:
Here is how I altered the code:
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:
Java Syntax (Toggle Plain Text)
//"Math is our Game" - A math testing program for First through Fourth grade students //Import GUI packages import javax.swing.*; import BreezySwing.*; import java.awt.*; import java.util.Random; import TurtleGraphics.*; public class Math extends GBFrame { //declare variables for the window objects private JLabel greetingLabel; private JLabel additionLabel; private JLabel subtractionLabel; private JLabel multiplicationLabel; private JLabel divisionLabel; private JLabel randomLabel; private JLabel grade1Label; private JLabel grade2Label; private JLabel inputLabel; private JLabel gradeLabel; private JLabel questionLabel; private JTextField greetingField; private JTextField additionField; private JTextField subtractionField; private JTextField multiplicationField; private JTextField divisionField; private JTextField randomField; private JTextField grade1Field; private JTextField grade2Field; private JTextField inputField; private JTextField questionField; private JButton greetingButton; private JButton additionButton; private JButton subtractionButton; private JButton multiplicationButton; private JButton divisionButton; private JButton randomButton; private JButton grade1Button; private JButton grade2Button; private JButton grade3Button; private JButton grade4Button; private JButton inputButton; private JButton quitButton; private JButton questionButton; int generator1=0; int generator2=0; int generator3=0; int generator4=0; //constructor public Math() { //Open greeting screen with welcome and Start button greetingLabel = addLabel ("Greeting" ,1,1,1,1); greetingField = addJTextField ("Welcome to Math is our Game, a math skills program by Nine Fingers and Ten Toes Software, Inc. ,2,1,1,1); greetingButton = addButton ("Start" ,1,3,1,1); //Button clicked to start game public void buttonClicked (JButton buttonObj) //Select grade level - Instantiate and add window objects to grade level window gradeLabel = addlabel ("Please select grade level of math problem." ,1,1,1,1); grade1 = addButton ("First Grade" ,1,2,1,1); grade2 = addButton ("Second Grade" ,1,3,1,1); grade3 = addButton ("Third Grade" ,1,4,1,1); grade4 = addButton ("Fourth Grade" ,1,5,1,1); quitButton = addButton ("Quit" ,1,6,1,1); //First grade selected public void buttonClicked (JButton buttonObj) if (buttonObj == grade1) { //Generate numbers for First Grade problem generator1 = generator.nextInt(10); generator2 = generator.nextInt(10); //Select type of problem //instantiates and add window objects to the window additionLabel = addLabel ("Addition" ,1,1,1,1); subtractionLabel = addLabel ("Subtraction" ,1,2,1,1); multiplicationLabel = addLabel ("Multiplication" ,1,3,1,1); divisionLabel = addLabel ("Division" ,1,4,1,1); randomLabel = addLabel ("Random" ,1,5,1,1); additionField = addJTextField ("0" ,2,1,1,1); subtractionField = addJTextField ("0" ,3,1,1,1); multiplicationField = addJTextField ("0" ,4,1,1,1); divisionField = addJTextField ("0" ,5,1,1,1); randomField = addJTextField ("0" ,6,1,1,1); additionButton = addButton ("1+1" ,3,1,1,1); subtractionButton = addButton ("1-1" ,4,1,1,1); multiplicationButton = addButton ("1*1" ,5,1,1,1); divisionButton = addButton ("1/1" ,6,1,1,1); randomButton = addButton ("Random Equation" ,7,1,1,1); } public void buttonClicked (JButton buttonObj) { //local variables Math1 Math = new Math1(); //determine which button was clicked if (buttonObj == grade1Button) { //setting and getting the numbers for the equations Math.setaddition(additionField.getNumber()); subtractionField.setNumber (Math.getsubtraction()); Math.setmultiplication(multiplicationField.getNumber()); divisionField.setNumber (Math.getdivision()); Math.setrandom(randomField.getNumber()); } else { Math.setsubtraction(subtractionField.getNumber()); additionField.setNumber(Math.getaddition()); Math.setdivision(divisionField.getNumber()); multiplicationField.setNumber (Math.getmultiplication()); randomField.setNumber (Math.getrandom()); //1st grade generator1 = generator.nextInt(10); generator2 = generator.nextInt(10); if (input == additionButton) additionLabel = addLabel ("Addition Problem" ,1,1,1,1); additionField = addJTextField ("Please Input the two numbers to add together:" ,1,2,1,1); additionButton = addButton ("+" ,1,3,1,1); if (input == generator1 && input == generator2) { questionLabel = addLabel ("Addition Problem" ,1,1,1,1); questionField = addJTextField ("You got the problem right, CONGRATULATIONS!!" ,1,2,1,1); questionButton = addButton ("1+1=2" ,1,3,1,1); } } } public class Math2 { private double Mathsubtraction; public void setaddition(double Math) { Mathsubtraction = Math; } public double setsubtraction(double math) { return Mathsubtraction; } public double getsubtraction() { return Mathsubtraction; } public double getaddition() { double Mathaddition = 0; return Mathaddition; } }
Here is how I altered the code:
Java Syntax (Toggle Plain Text)
//"Math is our Game" - A math testing program for First through Fourth grade students //Import GUI packages import javax.swing.*; import BreezySwing.*; import java.awt.*; import java.util.Random; import TurtleGraphics.*; public class Math extends GBFrame { //declare variables for the window objects private JLabel greetingLabel; private JLabel additionLabel; private JLabel subtractionLabel; private JLabel multiplicationLabel; private JLabel divisionLabel; private JLabel randomLabel; private JLabel grade1Label; private JLabel grade2Label; private JLabel inputLabel; private JLabel gradeLabel; private JLabel questionLabel; private JTextField greetingField; private JTextField additionField; private JTextField subtractionField; private JTextField multiplicationField; private JTextField divisionField; private JTextField randomField; private JTextField grade1Field; private JTextField grade2Field; private JTextField inputField; private JTextField questionField; private JButton greetingButton; private JButton additionButton; private JButton subtractionButton; private JButton multiplicationButton; private JButton divisionButton; private JButton randomButton; private JButton grade1Button; private JButton grade2Button; private JButton grade3Button; private JButton grade4Button; private JButton inputButton; private JButton quitButton; private JButton questionButton; int generator1=0; int generator2=0; int generator3=0; int generator4=0; Random generator = new Random(5); //constructor public Math() { //Open greeting screen with welcome and Start button greetingLabel = new JLabel("Greeting");//greetingLabel.setText("Greeting"); //= addLabel ("Greeting" ,1,1,1,1); 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); greetingButton = addButton ("Start" ,1,3,1,1); //Button clicked to start game } public void buttonClicked(JButton buttonObj) { //Select grade level - Instantiate and add window objects to grade level window gradeLabel = new JLabel("Please select grade level of math problem.");// ,1,1,1,1); grade1Button = addButton ("First Grade" ,1,2,1,1); grade2Button = addButton ("Second Grade" ,1,3,1,1); grade3Button = addButton ("Third Grade" ,1,4,1,1); grade4Button = addButton ("Fourth Grade" ,1,5,1,1); quitButton = addButton ("Quit" ,1,6,1,1); //First grade selected //public void buttonClicked (JButton buttonObj) //{ if (buttonObj == grade1Button) { //Generate numbers for First Grade problem generator1 = generator.nextInt(10); generator2 = generator.nextInt(10); //Select type of problem //instantiates and add window objects to the window additionLabel = addLabel ("Addition" ,1,1,1,1); subtractionLabel = addLabel ("Subtraction" ,1,2,1,1); multiplicationLabel = addLabel ("Multiplication" ,1,3,1,1); divisionLabel = addLabel ("Division" ,1,4,1,1); randomLabel = addLabel ("Random" ,1,5,1,1); additionField = new JTextField("0"); subtractionField = new JTextField("0"); multiplicationField = new JTextField("0"); divisionField = new JTextField("0"); randomField = new JTextField("0"); additionButton = addButton ("1+1" ,3,1,1,1); subtractionButton = addButton ("1-1" ,4,1,1,1); multiplicationButton = addButton ("1*1" ,5,1,1,1); divisionButton = addButton ("1/1" ,6,1,1,1); randomButton = addButton ("Random Equation" ,7,1,1,1); } //public void buttonClicked (JButton buttonObj) //{ //local variables Math2 math1 = new Math2(); //determine which button was clicked if (buttonObj == grade1Button) { //setting and getting the numbers for the equations math1.setaddition(Double.parseDouble(additionField.getText())); //subtractionField.setNumber(math1.getsubtraction()); //math1.setmultiplication(Double.parseDouble(multiplicationField.getText())); //divisionField.setNumber (math1.getdivision()); //math1.setrandom(randomField.getText()); } else { /*Math.setsubtraction(subtractionField.getNumber()); additionField.setNumber(Math.getaddition()); Math.setdivision(divisionField.getNumber()); multiplicationField.setNumber (Math.getmultiplication()); randomField.setNumber (Math.getrandom());*/ //1st grade generator1 = generator.nextInt(10); generator2 = generator.nextInt(10); /* if (input == additionButton) additionLabel = addLabel ("Addition Problem" ,1,1,1,1); additionField = addJTextField ("Please Input the two numbers to add together:" ,1,2,1,1); additionButton = addButton ("+" ,1,3,1,1); if (input == generator1 && input == generator2) { questionLabel = addLabel ("Addition Problem" ,1,1,1,1); questionField = addJTextField ("You got the problem right, CONGRATULATIONS!!" ,1,2,1,1); questionButton = addButton ("1+1=2" ,1,3,1,1); */ } } } class Math2 { private double Mathsubtraction; public void setaddition(double math) { Mathsubtraction = math; } public double setsubtraction(double math) { return Mathsubtraction; } public double getsubtraction() { return Mathsubtraction; } public double getaddition() { double Mathaddition = 0; return Mathaddition; } }
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".
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.
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
![]() |
Similar Threads
- Math Program Help (Python)
- math program (C)
- Java math program (Java)
- CS & Applied Math (Computer Science)
- a program about fractions (C++)
- i have written this program but it has somekind of glitch any help can be welcomed (Java)
Other Threads in the Java Forum
- Previous Thread: TurtleGraphics package
- Next Thread: how to set boolean
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation awt binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer invokingapacheantprogrammatically j2me java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number object objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree webservices windows






