Greetings, Java-Nation!
Today, we are going to start exploring a concept that is at the very heart of JAVA: the class. Java, as they say, is class-based; everything in it is derived from a class that you create, or from a class that's been there from the beginning, one that's written into the language. Today, we are going to learn how to create two separate classes, each of which does something completely different, and then import these classes into a main program, create objects out of them, and do something with each one. So let's get going by downloading your starter program from here:
http://www.box.net/shared/inp8pt71ph
It should be a file called "FunWithClasses" Download it to a folder named after yourself, PLUS the phrase "fun with classes". Compile it and run it just to make sure its A-OK. I'll wait right here.
zzzz.......
zzzz............
zzz.....huh?
Oh, you're all set; GREAT! Now let's get down to business.
The first thing you will have to do is create a separate class file in TextPad by clicking File then New. Call it "CalcOne" like so:
class CalcOne
{
}//end CalcOne
then save it IN THE SAME FOLDER AS YOUR MAIN PROGRAM. make sure the filename matches the class name -- duh!
The next thing is to give this class something to do. Let's make it add two integers and return the result, like so:
int addTwo(int stuffOne, int stuffTwo)
{
return stuffOne + stuffTwo;
}//end int add method
You need to put this int add method ABOVE the closing line of this new class. SAVE, COMPILE, then get back into your main program
The next step is to make an object from this new class, and put this object into your program. We do it in the same way we've added other classes; let's sing along with Uncle Paulie, OK?
beneath this line
Button exit; //declare button
add this line
CalcOne myAdd; //a new object named myAdd based on CalcOne class
The next step is to actually put this new object into your program. Go to this line:
Valhalla.add(exit);
and add this line
myAdd = new CalcOne();//put this new object in memory
RE-COMPILE, then run the program. Hmmmmmmm...it doesn't LOOK any different! Well that's because this class has no visible components, just one method for adding numbers. So let's add some stuff that can make use of this method:
Below this line
Button exit; //declare button
type in this line to create a second button
Button AddEm; //a button to add stuff
Just ABOVE this line:
myAdd = new CalcOne();//put this new object in memory
create this new button you've just declared, give it an actionListener, add it to the Frame
Now for the good stuff: you are going to add an "else if" to your actionPerformed. This else if will make use of the one method found in your new class to add two ints together. Here's what it looks like:
else if (e.getSource() == AddEm)
{
System.out.println(myAdd.addTwo(3,3));
}// lets add
This uses the "addTwo" method from the class called "CalcOne". Since myAdd is based on that class, your new "myAdd" object can use ANY METHOD IN THAT CLASS.
After that, we pass this method two integers, and print the result to the System console. Hint: you should see the number "6"
Cheers,
Uncle Paulie
Friday, February 20, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment