OK, let's review what we just saw:
1) you can create a class, and give it a method
2) you can create an object in another class based on that first class
3) once you've done that, then use the method from the first class to do stuff in the second class
OK, so let's make this program a little more useful. Right now, all it does is print the number 6 to the screen!
Here's what we do:
A) right below this line
CalcOne myAdd; //a new object named myAdd based on CalcOne class
add these two lines to declare two TextFields and two ints
TextField numOne, numTwo;
int firstNum, secondNum;
B) in the constructor, just below this line:
myAdd = new CalcOne();//put this new object in memory
put both those TextFields into memory and add them to the Frame. Also, set both ints equal to zero (the number 0 that is)
C) Lastly, we are going to use the Integer.parseInt method to make an int out of whatever number is typed into those textFields, then add those two ints. To do that, we are going to need to change your "else if" statement from this:
System.out.println(myAdd.addTwo(3,3));
to this
firstNum = Integer.parseInt(numOne.getText());
secondNum = Integer.parseInt(numTwo.getText());
System.out.println(myAdd.addTwo(firstNum,secondNum));
This way, your new method from another class is going to use whatever integers you type into the two TextFields. Try it and see what you get!
Cheers,
Uncle Paulie
Friday, February 20, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment