Friday, September 20, 2013

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.

No comments:

Post a Comment