Saturday, June 6, 2009

Java Basics Using Qt Jambi Tutorial

Constructors

Constructors are used to initialize our variables but this is optional. As I've said, there is already a default constructor created for us.

Ex.

public class Vehicle{

public Vehicle(int wheels){

this.wheels = wheels;

}

}

There are rules in creating constructors:

  1. It should have the same name as your class.

  2. It does not have a return type.

  3. You can create as many constructors provided you must have different number of parameters or different type.

Ex.

public class Vehicle{

public Vehicle(int wheels){

}

public Vehicle(String color, int doors){

}

public Vehicle(int gears, int steeringwheels){

}

}

Note: It is recommended you use different name for your parameters from the class variables.

Ex.

public class Vehicle{

public int trunk;

public String type;

public Vehicle(int wheels, int gears){

trunk = 1;

type = “car”;

wheels = 4;

gears = 6;

}

}

If you have same variable names for your parameters and class variables use the keyword “this.”

Ex.

public class Vehicle{

public int wheels;

public int gears;

public Vehicle(int wheels, int gears){

this.wheels = wheels;

this.gears = gears;

}

}

to be continued...

Friday, June 5, 2009

Java Basics Using Qt Jambi Tutorial

Functions

Functions are also called methods. Functions are the verb or action for which your program will do.

Ex.

public class Vehicle{

plublic void accelerate(){

}

public void turnDirection(){

}

public static void main(String[] args){

Vehicle car = new Vehicle();

car.accelerate();

}

}


Here, we created the class Vehicle and inside the class are three functions: accelerate,turnDirection and the required main method.


In the main method, we created an instance of a class (can also be called “creating object”) Vehicle with the variable car. The “new” keyword creates the instance and executes the default constructor, then we call the method accelerate like car.accelerate(). This executes the code inside the function we defined accelerate. For our car to turn direction, we call the turnDirection method like this: car.turnDirection();

to be continued...

Thursday, June 4, 2009

Java Basics Using Qt Jambi Tutorial

  1. Branching Statements

    1. break; - The break statement terminates the loop.

    2. continue; - when reached, it goes to the loop statement and evaluates the condition.

Ex. for (x=0; x<10;x++){

if (x==4){

continue;

}

System.out.println(x);

}

//this code prints from 0 to 3 and skips 4 and prints 5 to 9.

    1. return;

Ex.

import java.lang.*;


public class AddNumbers{

public int add(int x,int y){

return x + y; //passes 1 and 2 then adds them and return the result

}

public static void main(String[] args){

boolean addnum = true;

AddNumbers num = new AddNumbers();

while (addnum){

System.out.println(num.add(1,2)); //passes 1 and 2 to add method and prints the result.

addnum = false;

}

}

}

to be continued...

Java Basics Using Qt Jambi Tutorial

  1. Looping Statements

    1. while, do-while statements – The while statement executes a block while the condition is true.

Ex.

import java.lang.*;


public class MovingCar{

static boolean ismoving = true;

static boolean breaks = false;


public static void main(String[] args){

while(ismoving){

if(breaks){

ismoving = false;

System.out.println("breaks is applied...stopped.");

}

else{

System.out.println("The car is moving.");

}

breaks = true;

}

}

}


This code prints “The car is moving.” once and breaks is applied and goes out of the while loop.


Do-while statements' difference is hat the condition is placed at the bottom and executes the block of code once.

Ex.

do{

//statements

}while (condition);


  1. For Loop

Ex. for (x=0; x<10;>

//increment or decrement (x--)using unary operator

//initialization and executes once.


//Condition when met, or when it evaluates to false, it terminates.

System.out.println(x);

}

to be continued...

Tuesday, June 2, 2009

Java Basics Using Qt Jambi Tutorial

Control Flow

Your code is usually executed from top to bottom. Control flow statements changes the flow of a program depending on a condition. There are 3 control flow statements.


  1. Decision-making statements

    1. if statement

Ex.

int wheels = 4; //declare and assign wheels to 4.

if (wheels == 4) {

System.out.println(“It is a car”); //executes this if wheel is 4.


}else if (wheels == 2){

System.out.println(“It is a bicycle”); //executes this if wheel is 2.

}else if (wheels == 1){

System.out.println(“It is a unicycle”);// executes this if wheel is 1.

}else{

System.out.println(“It is another vehicle”); //executes this if no other conditions are met.

}


Note: else if and else are optional.


In this example, we used the == equality operator rather than the assignment operator = to test for conditions.


    1. Switch Statements – Switch statement works with:

      1. Primitive data types

      2. Enumerated types

      3. Few classes like Integers, etc.

Ex.

int wheels = 4;

switch (wheels){

case 1: System.out.println(“unicycle”); break;

case 2: System.out.println(“bicycle”); break;

case 3: System.out.println(“tri-cycle”);break;

case 4: System.out.println(“car”);break;

default: System.out.println(“another vehicle”);

}


Example of Enumerated Types, Switch Statement

import java.lang.*;

public class enumVehicle{

enum Wheels{

ONE,TWO,FOUR //assign fixed set of constants.

}


Wheels wheels; //declare wheels variable having Wheels enum.

public enumVehicle(Wheels wheels){ //create constructor

this.wheels = wheels; //initializes variable

}

public void whatVehicle(){

switch(wheels){

case ONE: System.out.println("unicycle");break; //prints unicycle since we pass Wheels.ONE to constructor and assign it to wheels.

case TWO: System.out.println("bicycle");break;

case FOUR: System.out.println("car");break;

default: System.out.println("another vehicle");

}

}

public static void main(String[] args){

enumVehicle vehicle1 = new enumVehicle(Wheels.ONE); //execute constructor enumVehicle and assign Wheels.ONE to wheels.

vehicle1.whatVehicle(); //call whatVehicle method and print “unicycle.”

enumVehicle vehicle2 = new enumVehicle(Wheels.FOUR); //execute constructor enumVehicle and assign Wheels.FOUR to wheels.

vehicle2.whatVehicle(); //call whatVehicle method and print “car.”

}

}

to be continued...

Java Basics Using Qt Jambi Tutorial

Operators, Expressions, Statements and Blocks

  1. Operators – Operators are symbols that do something on operands.

Ex. 1 + 2

operator +

operands 1, 2

Operands can be variables with values in them.

int a = 1; //assign a to 1 called, which = is called assignment operators.

int b = 2; //assign b to 2

int c = a + b; //add a and b and give the result to c which is also type int.

System.out.println(c); //print result which is 3.


There are two kinds of operators:

    1. Equality and Relational


== equal to

!= not equal to

> greater than

>= greater than or equal to

<>

<= less than or equal to


    1. Conditional


&& Conditional-AND

|| Conditional-OR


  1. Expressions – Expressions are made up of variables, operators or method calls.

Ex. int a = 1; //data type of a is int.

int b = 2; //data type of b is also int.

int c = a + b; //data type of c must be int.


  1. Statements – Statements are logical lines which ends with a semi-colon.

Ex. int a = 1; //one statement

System.out.println(“Hello World!”); //one statement.


Note: A Statement always ends with a semi-colon.


  1. Blocks – Blocks are made up of statements within curly braces:

Ex.

public static void main(String[] args){

int a = 1;

int b = 2; This is a block.

int c = a + b;

System.out.println(c);

}

or,

if (condition){

System.out.println(“Condition is true.”); This is a block.

}

else{

System.out.println(“Condition is false.”); else block

}


to be continued...

Monday, June 1, 2009

Java Basics Using Qt Jambi Tutorial

Identifier Naming

You just have to remember these rules in naming identifiers or variables.

  1. Variable names are case-sensitive.

  2. Begins with a letter or underscore.

  3. Whitespace are not permitted.

  4. Subsequent characters can be letters, digits, dollar sign or underscore.

  5. Must not be a reserved word or keyword.


Good Naming Practices

  1. Use full words like: wheels, speed, etc.

  2. Do not use underscores as the first character.

  3. If using more than one word, capitalize the first letter of the second word.

Ex. currentSpeed, currentDirections, etc.

  1. When using constants, capitalize all letters and separate the next word by an underscore.

Ex. NUM_WHEELS;

to be continued...