Wednesday, July 9, 2014

20140709 Second Life RP game

So I have started making a game for role playing game on Second Life.  My plan is to introduce SL to PHP and MySQL where PHP will actually be the gaming engine. LSL will just be the medium to interface Second Life to the game engine.  Still have some figuring out to do, but I have started on the project.

Friday, September 20, 2013

Building a Simple Java Program Part 5: Overview of Definitions

Why am I pausing a moment and doing an overview?  This is where we will add some vocabulary to your knowledge so far.  We did a lot in the past few blogs and it is good if you know the definitions of what you did.  This makes it easier to talk to someone who also uses Java.  Let start by looking at definitions that create a Java program.

Source File - This is code that is readable by humans or hasn't been converted to binary.

Binary File - Code that has been compiled so processor can execute the code.

Declaration - Parts of the code that are organized. For example, this declares a class:
class example{
}
The ellipsis is what declares the group and class is what we are declaring.

Class - A body that includes code with objects, states, methods, and behaviors

Methods - A declaration that returns a results, even if the results is nothing. For example:
public int isThis(int x){
     return 1 + x;
}
This would return 1 if ran.  There are 6 parts to this see this for more info.

  1. Modifiers define how the Method can be used.  This is usually public or private.  No definition assumes private.
  2. Return Type defines what will be returned.  Defining this as void returns nothing.  If a return type is defined, it must have a return.
  3. Method Name is the name given to the method.  In my example, I used isThis.
  4. Parameter list that is contained in the parentheses.  This is how we send data to the method. In my example I have x provided.  When we call on this method, we can say isThis(1) and what will be returned is 2 because 1 + x where we replace x with 1.  Also, the variable x has to be define with is defined as an integer in my example.
  5. Exceptions list is how we handle errors in the code.  The example above doesn't have any defined.
  6. Body is enclosed with Ellipsis.  Everything within them are a part of the method.

Constructors - See this

Object - Defines a state and behavior.  Created from classes. More info here

Variables - See this for definition 

The next blog will be about adding more functionality and parts for our car.  (Next blog isn't published yet.)

Building a Simple Java Program Part 4: Making the Model

Lets overview what we have accomplished so far in my past blogs:
  1. Installed Java and Eclipse
  2. Created a Java Environment by creating a project and packages
  3. Created a Car class to serve as an object
In the last blog, we created a Car class.  This is a special class that defines a car.  We said that a standard car has four doors, four wheels, and calls it self "Car".  What we are going to do next is how we can organize Java code by creating instances of an objects.  We can look at it this way.  When a model is a car, it should have the same properties as a car.  We can either extend those properties to the model or inheritance them within an object.  We are going to do both.

First we will create some two Java files.  One is called BMW and the other we will call Series3.  The BMW class will extend the car properties and the Series3 will inheritance an object though the BMW class.  Lets start by creating the files.  In Eclipse, create two class files, one called BMW and one called Series3.  Look at the last blog for instruction on how to do this.  When you create the create the BMW file, don't select the main(String[] args) option.  However, select it for the Series3.  


Look at the above for reference.  In the BMW code, we are going to add a string variable called company, import Car, and extend Cars to BMW.  Before we move on, let me explain something.  Look at the code below.  Import and extends is new.  Import is how we tell a class where another is.  In this case, we tell class that Car is loaded in package test.example.car. You can also use test.example.car.*, but since we are only using one class, we will only import on that one.  Next is extends.  This is away to add the Car code to BMW.  It is like we wrote all the Car code in BMW.  This is a great way to reuse code.  Something you need to understand is extending code isn't flexible as we are just adding code.  All inherent code will follow suit.  Examine the completed code below before you move on.

package test.example.models;

import test.example.car.Car;

public class BMW extends Car{
public static String company = "BMW";
}

You are welcome to add some variable that are unique to a BMW.  Just remember the rules about variables and that you CANNOT have the same named variables in BMW as in Car.  Next, we are going to change the code in Series3.  Remember when we added a Main Method to this class through the class wizard?  This code will be the central point for putting it all together.  It's out execute code.  Lets examine the code and change it to this:

package test.example.models;

public class Series3 {

public static void main(String[] args) {
BMW series = new BMW();
System.out.println("I am a " + series.company + " Series 3, I have " + series.door + " doors and " + series.wheels + " wheels.");
}
}

Did you notice that we didn't need to import BMW?  This is because Series3 is in the same package.  Look at the code:

BMW series = new BMW();

Whats going on here?  This is how we create an object in Java.  We are saying that the variable "series" is going to hold an object called BMW, and to create an the object as a fresh, new object. Note that if the class has static methods, the next time the object is created, it'll basically be a duplicate of the first object.  Any changes done in one changes the other.  See the parentheses?  That is for constructors.  We can put stuff there to extend what BMW does.  Don't worry about that now as this is something to cover in a different lesson. 

Now that we have created a object called series, we can now access any variable that exist within it.  Note that Java can take different types of variables and convert them to strings so we can display them.  You can see this after the println.  If you notice, we can call on an object variables by object.variable.  So series.company will gives us "BMW".  Try it.  Save BMW and Series3 and press the green arrow in Eclipse to run the program.  You get this in Eclipse:

I am a BMW Series 3, I have 4 doors and 4 wheels.

Next blog, we are going to do an overview of what just went on and shine some light on a couple of things.

Next

Building a Simple Java Program Part 3: Lets Make a Car

         If you have made it this far into my blog, this means you should have installed Java and Eclipse.  This also means you have created a development environment for our program by creating three packages.  If not, please do part 1 and part 2 of the tutorials.

         Now it is time to get our hands dirty.  First, lets make sure that you have Eclipse running and that you have expanded the src folder. It should look like this:


Hover over the test.example.car package and right click the mouse.  Hover over new and left click Class.  The menu below will look different than this picture I provided, but new menu should look the same:



When Class is clicked, a new Class wizard will open (as shown above).  The source folder and the package will be filled out.  If not, you didn't right click on the test.example.car package.  You can either type it in manually or start the wizard over (Click Cancel).  Next, put Car in the Name text box.  Make sure the C is capitalized.  Eclipse will warn you if you don't, even though it'll let you complete the wizard.

Before we go on, let me explain something.  In Java, if there is a main method within the class file, it means that this is an run class.  This class can be ran when called on directly.  You can basically say that it is like an execute file.  For now, lets select the "public static void main(string[] args)".  The Inherited methods will be selected by default.  Unless told not to, leave it selected.  Everything else stays defaults.  Click finish.

You should end up like this:


Look at the Car.java file.  Let me explain somethings about this.

package - This is what package in Java that the class Car will exist.

public - Used to identify what type of access other code will have to this class.  Public means that anyone can use this class directly.

class - first class MUST match the file name.  Creates the environment for method, and states.

static - Tells any code that access to this class in the public that this class will not be unique.  If you make a duplicate of this class as an object, any changes you make in one will change the other.  Static is optional, but needs to be considered because it modifies the behavior of the program.

void - Tells the function that there will be nothing returned.  We will talk more on this later.

main(String[] args) - A function that allows the Class to be ran from command prompt (CMD)/terminal when running the Java command. String[] tells us that args is an array of Strings.  In CMD, we can run a class file by typing "Java Car".  We can also add some argument by doing "Java Car 2 4".  Maybe this would add 2 cars with 4 doors.  I'm not going to go into details on this because we are not going to use command prompt.

You'll notice the:
//**
*
**/
and
// TODO

This is how we create comments.  Also, see the @params args.  This is used if we wanted to use javadoc to create documents for the program.  We are not going to cover that here.  Ok, so now we have created our first virtual car, even though it will not do anything, this code will run.  Let's make the car say something.  In the main method in Eclipse, type System.out.println("Hello, I am a car");

package test.example.car;
public class Car {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello, I am a car");
}
}

Why System?  Because in Java, the console output is an object.  Don't stress over that to much.  Just know that if you want the console to output something, this is how you do it.  The println adds a carriage return on the console.  We can also use print, but it wont do a carriage return and the next output will print on the same line.

Now save your work, the push the green arrow in Eclipse.


At the bottom of Eclipse, you should see a line that says "Hello, I am a car"



This program is pointless but it shows that our program ran. Let's actually give the meaning to what a car is.  We are going to remove the run function of Car, because Car is actually going to become an object later and we don't need it to run by default.  In the Car file, erase:

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello, I am a car");
}

The Car file should should now look like this:

package test.example.car;

public class Car {
}

Now lets add some variables.  A variable is how we make a program remember stuff.  There are different types of variables and each have different memory requirements.  I'm not going to explain what each does.  You are welcome to pause a moment and look at Java variables at the Oracle site.  We are going to use two integers and a String in our program.  Here is what your file should look like.

package test.example.car;

public class Car {
public int door = 4;
public final int wheels = 4;
public static String type = "Car";
}

We know that all cars have to have 4 wheels so we are going to make an integer called wheels with a final modifier.  Final means that this variable cant be changed after it is stored.  We are going to allow the door integer to be changed because we want a sports Car to say 2 doors.  The last thing is the String variable called type.  It is Static so that if the name is changed, all object will accept the change.  Hopefully this will get clearer as we move on to making models of cars.  Save and move to the next blog where we make a class that will identify models of cars.

Building a Simple Java Program Part 2: Setting Up the Java Enviroment

          In the last Blog, we looked at setting up Java in the OS and installing Eclipse.  Make sure you have gone through those instruction before you continue.  It will get frustrating when things wont work because something isn't set up correctly.  Now, let talk about what Eclipse is going to do for us.  Eclipse is going to color code and auto format the code.  It will also provide shortcuts for auto filling in code.  I will touch on some of that as I go.  Eclipse will provide the compiling of Java code and automatically separate the source from the java binaries.  There are a lot more it can do, but for our purposes, we will stick with just a few. The first thing we are going to do is decide what our program is going to do.

         What we are going to do is create a Car.  That's right, we are going to create a virtual car that is going to have properties.  This will give you an idea of why object oriented programming (OOP) is one of the best things that has happened to programming.  There is plenty on the internet about what OOP so I'm not going to go into details.  I will tell you when you are working with it.  Also, there is a lot of formatting that is required in Java.  I will explain when formatting is required.  Let's get started by setting up Java packages in Eclipse.

          Packages in Java help us by organize our code or objects.  To start, lets get to the Eclipse workbench.  If you haven't already, click on the workbench on the Eclipse welcome page.  If you already clicked it once before, you should be at the workbench already.  Eclipse should look like this:


To get started, right click the mouse in the 1 area on Eclipse shown as an example below:


Hover mouse over new and left click Java Project:



Within the Project name text box, give your project a name.  I named mine training, but you can name it whatever you want.  This name is so you can identify it.  It wont effect Java.  You can also chooses your JRE environment. However for what we are doing here, it's not needed.  Leave everything else at defaults.  When you open the new package, you'll see an src and JRE System Libary drop down.  If you don't, try clicking the small triangle to expand the project:


We are only going to work in the src folder.  Next, we are going to create some packages within the src folder.  These packages will effect how Java operates.  I will explain whats going on as we move through the tutorial. Let's create three packages.  They are going to be called test.example.car, test.example.car.parts, and test.example.models.  Do this by right clicking on the src folder, hovering over New, then selecting package:



Click finish and do this two more times until you get all three packages into the src folder (As shown below).


 Now we are ready to start getting into the meat of making a Java program.  These packages we just made here will help us organize our virtual car.  If your new to Java, this probably doesn't make sense yet.  Don't worry, it will once we get to the coding.





Building a Simple Java Program Part 1: Setting Up an IDE and the Operating Enviroment

          Java programming is a difficult programming language to learn.  It is also one of the most rewarding.  In this blog, we are going to discus how to set up an development environment in a operating system to allow us to program in Java.  We are going to look at the Eclipse Integrated Development Environment (IDE) and how to prep the Java Development Kit.  As we go through the blogs, we will also look at some reasons Eclipse does thing.  Lets get started with installing the JDK and Eclipse.

          First, lets install the JDK.  It can be found on the Oracle website.  http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html.  Make sure you choose the download that matches your operating system.  Also, you don't need the demo and samples download.  After it downloads, run the program and follow the instructions.  Onces installed, make sure that Java can run by opening a command prompt and typing "java -version".


          If this looks somewhat like the results you got, then you are good to go for installing eclipse.  If not, you need to update the window PATH variable.  This can be done by going into Control Panel->System->Advance System Settings->Environment  Variables (On Vista/7).  Find the PATH variable.  I recommend you look in the system variables.  You will need to add in the path to Java.  If you installed JDK 64, it will be located in the Program File folder.  If you installed JDK 32 on a 64 Bit OS, it will be located in program files (x86).  Copy the entire path.  Mine would look like: C:\Program Files\java\jdk7.0.03\bin. At the end of the PATH variable, start by adding a semicolon and add the path.  (NOTICE:  COPY THE PATH variable into notepad before you add anything to it.  ALT+A will select all and ALT+C to copy, ALT+P to paste.  You may need it if you make a mistake.)  You will have to reboot your computer after this is done.  After the restart, try running the above command again.  If all works, go on and start the instructions to install eclipse.

          When you go to the Eclipse website, you will notice that there are a several types of packages you can download. http://www.eclipse.org/downloads/.  The one I recommend is getting the one that is just for Java Developers.  You are welcome to get the Java EE version which will include a lot of Java libraries built for an Enterprise.  Make sure you choose what type of OS you have.  After the download, extract the files into a folder where you can locate it.  Eclipse doesn't need to be installed, it will run straight from the folder.   Eclipse.exe is the file that will start the program.

          You should get the welcome page.  Notice: I use Java EE; yours might say something else.  This welcome page is nice to get a feel for eclipse as it provides examples, but we are going to skip this part.  In the top left corner, you should see workbench.  Click that.  It'll take you to the location where we are going to work.  Eclispe from this point should be setup to start programing in Java, but if you are having issues, you can see http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-2.htm for further help.  You might want to review it to make sure the settings are correct before you begin.  Next blog we will look at how to create our first program and packages.