Wednesday, April 25, 2012

Variables and Assignments

"One man's constant is another man's variable" - Alan Perlis


Today we will be going over how to use variables in Java. Let's start a new class within Eclipse called Variables. Firstly, we must go over what exactly a variable is. For those of you who are math prodigies, math majors, or simply understand basic Algebra, a variable is a letter or group of letters that define something within your function. That's the math way of explaining what a variable is, and pretty much is a prime definition of what a variable is. Though in the programming world we take pride in using complex words to explain what a variable is relative to the machine.


 In the programming world a variable is a named memory location capable of storing data of a specified type. 

What that means is that a variable is something that stores data of a particular type. A type would be something like byte, short, int, and long. Which we will go into much detail sooner or later in this tutorial. But for the time being let's focus on expanding the definition of a variable.

In math we have, X=2, solve for x-5. 

For the normal mathematician, this is quite simple x = 3 right? Well let's hope that you resolved the equation above to 3 since that's the correct answer. When the question says x=2, x is the variable and in the programming world the 2 is the initialized value of x.

An initialized value is when we give a variable a specific value. For example x=2. 

When programming we must remember that: A variable must be declared before it can be used. For those of you who are wondering what that means, well we have to type out the variable at the beginning of our program (for the time being) before we use it.

An example of declaring a variable would be:

int x=2;

In this segment of code we declared the variable x which has a value of 2. We also gave x the type int which refers to integer. You don't always have to specify a value for x.

 You can simply declare a variable by typing out:

int x;

Now let's go over what the basic types are: 

byte: 8 bits (1 byte), which ranges from -128 to 127.
short: 16 bits(2 bytes), which ranges from -32,768 to 32,767.
int: 32 bits(4 bytes), which ranges from -2,147,483,468 to 2,147,483,467.
long: 64 bits(8 bytes), which ranges from -922,337,203,685,475,808 to 922,337,203,685,475,807.

We use numerous types for different reasons, but for numerical values you should memorize what these four types are and what their ranges are.

Let's write a simple program that uses variables shall we?

public static void main(String [] args){

int x = 2;  
int y = 5;  
int product = x*y;

 System.out.println("The value of 2x5 is: " + sum);
 }

Now as most of you guessed from that simply program we implemented three declared variables. Which we gave each variable a specific value. For x we gave it the value of 2 and for y we gave it the value of 5. Why did we do such a thing? Well in the print statement we said that the value of 2x5 is something right? So basically we used basic algebra skills to come up with the code. Which happens to be x*y. Since x has the value 2 and y has the value 5 we should get 10 as our sum. In my next post we will dive deeper into variables and their use, as well as, concatenation, and float and double types.

For today's assignment, your job is to write a program that multiplies 5x5. Send me your source code in the comments and if you have any questions feel free to ask.

Monday, April 23, 2012

HELLO WORLD!


Today we'll be diving into how to create your very first program. Now I'm hoping that by the time I posted this post you have installed the necessary software to compile your code. I'll be researching alternative compilers if Eclipse is to complex of a IDE for you guys to use. Remember the only way I can find that out is if you comment on this post. So in we shall go!

The first thing you want to do is under the Java package you created in Eclipse you want to right click that package and create a new package.


Now that we've created a appropriate class, for the Hello World program that we are about to write. There are a few concepts and key terms that we must familiar ourselves with before we go any further.

First lets go over what a method is. A method by definition is a section of a class that performs a given task specified by you the programmer. A group of statements or instructions enclosed by curly braces is called a block. You will hear me constantly referring to blocks of code in the following posts. By normal conventions, a class name begins with an uppercase letter (Though I have used a lower case letter and found that it doesn't make the bit of difference but then again I could be wrong). When a Java program starts, the main method is automatically executed. That is, the statements of the main method are executed first, and the main method is the starting point of every program.

So now that we've gotten those terms out of the way let's get started on coding. Firstly we must create a main method in the class. Maybe if your a bit smarter than I you can pre-create the main method in the New Java Class window when it prompts you to enter your information. All you have to do is click the box right next to the public static void main(String [] args). And the beautiful thing about Eclipse is that it does this for you! But if you didn't don't sweet it. We'll just type it all out mainly after the class. Just type in public static void main(String [] args). Don't forget the curly brackets. Eclipse makes life so much easier by adding the other part of your code for you. So memorizing stuff to program in Java is kept at a minimum.

After we've done that now it's our turn to write your first segment of code, System.out.println("Hello World!");

Now that we've done that we can finally compile our code by clicking the green play button at the top of eclipse, and it should run in the console by saying Hello World!

Congrats you wrote your first program!

Here's a link for my source code completely going into further detail if you find yourself lost.

Hello World Program!

Introduction to Eclipse


Here we are the beginning of your programming adventure. Hopefully by now you installed this wonderful program called Eclipse. If you are worried about compatibility of your machine and this IDE, don't worry! Eclipse is a free open source program that runs on Linux, Mac OS X, and Windows XP/7 alike. There is no bias when it comes to Eclipse which is another reason why I love using it so much. If you have not downloaded Eclipse, I will post a download link in the section below, after we are done talking about how to use Eclipse. Without pictures this part of the tutorial can prove to be somewhat difficult but I luckily came across a very good tutorial on YouTube which will help me show you how to use Eclipse efficiently and properly. So to start with this tutorial you must have the following installed on your computer:

  1. Eclipse - The IDE that we are going to use throughout this blog to compile and run our code. 
  2. Java-SDK: The piece of software that allows us to compile and run our code in the Java language. 

If your worried about your budget, don't be all of the following software that I ask you to download will always be free and open-source. I believe that all software for at least teaching purposes should be allowed to be distributed for free. Without the risk of piracy, hopefully we don't go down that trail ;).

To begin our tutorial we must answer the very first pop-up that we receive when running this program, "Select a workspace", this prompt is simply asking you the user where do you want me to place your programming files. This decision is for you to decide not me. I suggest that you put your workplace somewhere safe and sound so that no one else can touch your workplace.

Now you must create a new Java Project, you can reach this by going up to file and selecting New...

Once you've done that we shall name our Java Project, Let's Learn Java. A Java Project is simply a folder that holds all our data in one nice and neat folder. Now we must create a Java Package which is a sub-folder of the Java Project folder. Name the Java Package, Learn to Java 1. As this will be the first package of all the basic Java programs we will be writing. This simply holds all your programs in a package, simple enough right?

Now that we've done that you've taken your first steps to becoming an Ace Java Programmer. In my next post we will write our first program and go over some basic terminology before hand so that you can understand what exactly we are doing with our Java Code.



If you find yourself confused, here's a helpful video, teaching you how to use Eclipse properly: 



Download Links

Sunday, April 22, 2012

Welcome


Welcome to the first series of blogs that will help you learn how to program. For the first series, I wanted to start out with Java, since I'm currently learning it myself this is a way for me to review and teach Java. So as I further progress and understand complex programming concepts and such, the blog will be updated. To start with we should get familiar with a few IDE's (Integrated Development Environments) that will help us program. My personal favorite so far will have to be Eclipse (Download ---> Link). An IDE is what programmers use to program. I'll look on youtube for tutorials to teach you guys how to use Eclipse. By far the best IDE I've used so far and it is FREE! So in my next post we will talk about how to use Eclipse but until then keep on chugging that code!