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:

//"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:

//"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;

}
}

Recommended Answers

All 5 Replies

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.

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".

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.

that right there may solve my problem. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.