Your First Java program - Part 2 - Console

Thursday, December 13, 2007 | Labels: | |

This series is not yet finished. Stay tuned!
A console is where people can test code and run simple programs. It is mostly made for the sake of finding bugs from a program. Thus, it is not the most versatile way to display things, but it will do for now.

In most video games (half life, sims, etc), cheating requires you to toggle the "console", which is usually accomplished by the "~" key. If you remember this, you'll know that a console appears to be mostly lines... and yes, it is, most of the time, lines.

That's the command "println", as you saw on part 1.

Ok, now let's make our own console, continuing from where we left off in part 1.
import hsa.Console;

public class HelloWorld
{
public static void main (String[] args)
{
Console c = new Console ("My Console");
c.println ("Hello World! Again...");
}
}
As we can see here, we have imported a package from the built in library of Java commands. In fact, all these programming languages (C#, C++, delphi, java, etc) are all competing to have the largest library so coders don't have to write everything! Honestly, if I were to teach you how to write the Console class, I would probably get carpal tunnel.

Inside the main method, we have made a console named "c". Notice, a console is an object by itself, so we are using an object (console) inside an object (HelloWorld). The point of OOP is to reuse objects to make our life easier.

For now, you must, when defining any object, use the proper syntax of

Object objectName = new Object;

Or else it won't work.

You must also understand Camel Notation, which is what the godly Java community came up with the make Java code readable. The rules are simple, for names of objects, start each word with a capital letter, like "Console" or "HelloWorld". For names of primitives, start each word with a lower cased letter, and any preceding words with capitals, ie. "thisIsAnInteger" or "costPerMillion".

And since there is a method called "println" in the class "Console", we can use

c.println("text");

to print out a line on the console.

By the way, notice how there are ";" after some lines? The semicolon is to indicate that a statement is finished. I will talk more about this when we do loops.

Stay tuned for Part 3 - Primitives and Loops!

3 comments:

  1. Kakashi266 says:

    Interesting. I have never used a console like that. I have always used the System.out.println() and the standard console(not the import hsa.Console thing).

    Also, for your next tutorial, are you going to do all the types of loops (for, while, dowhile, and the enhanced for loop)? If you do, please include the enhanced for loop. I haven't seen that in use too much and I'm to lazy to go look it up.

  2. Kakashi266 says:

    Oh, and, by the way, I don't know why my comments on your first post have not shown up (or maybe they did and I'm retarded), but check out my blog about making a game in Java.

    Pokemon Java (PC)

  3. SW says:

    am i doing it correctly because whenever i execute it, it gives me this....
    http://i89.photobucket.com/albums/k210/tonster91/problem.jpg

    is it correct? appreciate your help.

 
Free URL Redirection @ .co.nr