Thursday, January 5, 2012

Programming (in Java): Adding more classes and methods to Hello World

Mmnyes, Money.

Happy Rich Man Thursday today, everyone:

Now that we finished "Hello World", we are almost done with using the "Hello World" program. Open Eclipse and go into the project we called "Hello World". Now, right click your source code folder and create a new class, name it HelloWorld or something other than what you named the first.

The good stuff we learn today:
1. Constructor
2. Data Member
3. Method

What the new class should look like
Now, in between the squiggles (Again, {}), type in:
public HelloWorld()
and put a new set of squiggles after that. This is your constructor, which allows you to call it from another class, thing, etc.etc. Above your constructor, you will need to create a string. Now, in order to do this, above the constructor, type in
String 
and then type in the name. Remember to end the line with a semicolon! Really important to do that. It is like having periods after your sentences. You miss one and your professor in english or teacher in language arts gets upset, or something along those lines.

What is a string, you ask? Well, a String is a thread of text. It is a Data Member, which are like "int"s, "double"s, and "char"s. Data members contain your basic data, such as numbers, text, a true/false result, or just a single letter, depending on the type.

Now, inside the squiggles, you need to initialize the string. Before, you declared it. Initializing is to set something to a specific value. For this example, inside the squiggles, you will type the name of the string you assigned it, set it equal to, in quotes, whatever you want the string to be. For example, my name for my string is "helloWorld", so I would initialize it by saying: helloWorld = "Hello, World!";

Underneath the squiggles, we need to create a new method. Methods are a chunk of code you want to be repeated, in which you can call it from another method or class and allow it to run. For example, if there was something that I wanted to code that took a number, multiplied it by 2, and it was to be used multiple times, then I would make it into a method.

So, now that we know what a method is, type in "public void helloWorldPrint()" or something along those lines. You need the public, the void, and the two parentheses, but you can change the name of the method. Create a new set of squiggles underneath the method and, inside the squiggles, you want to use the same code we used like last time. That's right, "System.out.println". This time, however, we do something different. Type in the following code:
System.out.println(helloWorld);
Or whatever you called your string name. When this class is called, it will output whatever your string was, which was preferably "Hello World!". Yes, I know this is repetitive, but we only need 2 more lines of code until we are done forever! In fact, with finishing this, you should have a class that looks like this:
(Looks in the oven) and this is what it should look like
 Please note that the package name things should only be there if you created a new package. Otherwise, this class is done. Now, we need to go to our original. Delete the
System.out.println("Hello, World!");
and add these lines of code:
HelloWorld worldlyHello = new HelloWorld();
worldlyHello.helloWorldPrint();
Now, what you are doing is creating a new instance of the class and calling the method from the class you created.
Now, this is what it should look like:
(Opens a conveniently placed oven right by the one currently opened) And this is how your other one should look like.
 Now, press run and you should be done.

Please comment with any concerns or problems, and I will try to respond.
Mmnyes, so long, young ones, and I will see you next time.
-The Rich Man who is called Thursday (Not really rich, again)
 

No comments:

Post a Comment