Saturday, June 13, 2009

Java Basics Using Qt Jambi Tutorial

QT Jambi

Now that we have discussed the basics of the Java Language, we move on to using Qt Framework in our Java Programs.

We are going to use an example project that would explain on how Qt works.

The program will be a photo slideshow with these requirements:

  1. Import photos and show it.

  2. Import music.

  3. Play the slideshow.


Using Qt Designer

Qt designer is an interface that helps you design your application. Just like GUI designing in other IDE such as Visual Studio or Eclipse, Netbeans, etc.

When designing the GUI (Graphical User Interface), you can use the Qt Jambi Designer included in the Qt Jambi SDK or the Eclipse Qt Integration plugin. Since we are learning the fundamentals, it is better to use the Qt Designer that comes with Qt Jambi SDK, then convert the GUI you designed to Java code.


Since we already made our first application using the steps in “The Basics,” “Using Qt Designer,” we'll just tell you what to add in our photo slideshow.


  1. List Widget

  2. Text Label

  3. Horizontal Slider

  4. (3) Push buttons

  5. Menu with these:

File->Import Photos->Import Music->Exit


It is recommended to rename these “widgets” using the Object Inspector Located on the right side of the designer by clicking the tab below. This will aid you on your coding for it is easier to remember because it will show up in your produced Java code and avoid variable name clashes by making it unique.


I won't write the source code here since it is not efficient, instead go to this http://javaqt.blogspot.com/2008/11/java-using-qt-jambi-tutorial-example.html and copy it.


The program works as it should, but it has “bugs” (errors) in it as to teach you Java using Qt Framework. Read the comments the ones after the “//” or “/*” and “*/”.

In the program or code, there are lines of code which I used String Builder, but a fixed array should be used instead.

In debugging (fixing) the code, I would suggest that you print out the variables or objects to see if the values it contain is correct (although, you can also see the values of variables on some decent IDEs like Eclipse, etc.) like,

System.out.println(variable); throughout the program.

In Java development, I always encounter problems on using class variables, like in a GUI in Qt, therefore add the keyword “static” so it would have one copy of that variable to use (this is also called static fields).

It is recommended that you read APIs (Application Programming Interface) documentation (and I know you would) and study it on how to use it. Search on the Internet for these or go to: http://java.sun.com/docs/books/tutorial/index.html download it, and the documentation included on the Qt Jambi SDK.

When creating a program, always think on solving one problem at a time, it is good practice that you design your program first on paper, but of course in reality, this isn't so.

Always make sure that you test your programs thoroughly before giving it to your boss because results matter.

So, I leave you with an unfinished program so you could experiment on. Happy Programming!


Friday, June 12, 2009

Java Basics Using Qt Jambi Tutorial

Generics

Generics makes it easy to catch runtime bugs such as wrong argument types. It tells the compiler to inform an error when there are wrong types, when a method call passes a string when it is expecting an integer.

Ex.

public class Vehicle{

private Object object;

public void start(Object object){

this.object = object;

}

public Object speed(){

return object;

}

public static void main (String[] args){

Vehicle v1 = new Vehicle(); created by different programmer

vi.start(“100”);


Integer speedofv = (Integer)v1.speed();

System.out.println(“speed of car is “ + speedofv);

}

} created by another programmer


This will result an error because it passes string where it expects an integer (this is at runtime not compile time).


Example on how to use generics:

public class Vehicle{

private T t; //T stands for type

public void start(T t){

this.t = t;

}

public T speed(){

return t;

}

//we just replaced Object with T.

public static void main(String[] args){

Vehiclev1 = new Vehicle();

v1.start(“100”);

Integer speedofv = v1.speed(); //no explicit casting

System.out.println(speedofv);

This will result an error in compile time having wrong types.


Type Convention:

  1. T-type

  2. N-Number

  3. E-Element

  4. V-Value

  5. K-Key


You can also use generics in methods. The difference is it only accessed inside the method.


public<u>void direction(U u){

System.out.println(“T:” + t.getClass().getName());

System.out.println(“U:” + u.getClass().getName());

}


public static void main (String[] args){

Vehicle<integer>v1 = new Vehicle();

v1.start(100);

v1.speed();

v1.direction(“North”);

}

prints out: T: java.lang.Integer

U:java.lang.String

to be continued...

Thursday, June 11, 2009

Java Basics Using Qt Jambi Tutorial

Packages

Packages is used to group related types which are classes, interfaces, enumerations and annotation types.

Ex.

//in UseCar.java

package com.cars.engine;

public interface UseCar{}

//in Vehicle.java

package com.cars.engine;

public abstract class Vehicle{}



3 Ways to access members:

  1. Full qualified name. Ex. com.cars.engine.Vehicle;

  2. Import package member. Ex. Vehicle v1= new Vehicle();

  3. Import members entire package.

Ex.

import com.cars.engine.*;

Vehicle v1 = new Vehicle();

Car car1 = new Car();

Things you must remember with packages:

  1. You must name packages with correct convention to make it unique and avoid name clashes.

Ex. package com.cars.engine;

  1. Your class files should be located in the directories with names using the package name.

$javac -classpath /path/to/jar:/path/to/jar -d /path/to/folder/containing/java/files/ MyCar.java Vehicle.java

$jar cfm MyCar.jar Manifest.txt /com/cars/engine/*.class

$java -jar Main.jar

to be continued...

Tuesday, June 9, 2009

Java Basics Using Qt Jambi Tutorial

Interface

Since we cannot have multiple instances using a class, we can use interfaces. Interfaces can implement from other interfaces.

Ex.

public interface UseCar{

int start (boolean ismoving,boolean key);

int stop (boolean ismoving,boolean breaks);

}

public class BMWCar implements UseCar{ //is used to provide methods declared from the interface.

int start (boolean ismoving, boolean key){

}

}

//is used so it can be available to any package to any classes. If omitted, it can be accessed to classes with same package.


Note: It can also contain constant declarations.


You must remember these rules in using Interfaces:

  1. All the methods in interface should be declared on your source code.

  2. Use public if you want to access the interface to any classes in any package.


When using or to access the interface methods through class instantiation implementing the interface.

Ex.

InterfaceVehicle.java


import java.lang.*;


public class InterfaceVehicle implements UseCar{


public int whichVehicleRunsFaster(int v1, int v2){

if (v1 > v2){

return v1;

}

else{

return v2;

}

}

public static void main(String[] args){

int car1 = 100;

int car2 = 200;

int cars;

InterfaceVehicle vehicle = new InterfaceVehicle();

cars = vehicle.whichVehicleRunsFaster(car1,car2);

System.out.println(cars);

}

}

UseCar.java


import java.lang.*;


public interface UseCar{

public int whichVehicleRunsFaster(int vehicle1, int vehicle2);

}

Note: Classes that implements this interface “inherit” the constants and must implement methods.


If an abstract class contains only abstract method declarations, it should be interface.


to be continued...

Monday, June 8, 2009

Java Basics Using Qt Jambi Tutorial

Classes

We've already seen classes from previous examples like class Vehicle. In object-oriented programming (OOP), we always create the most common or generalized class.

Ex.

public class Vehicle{

}

We created the class as Vehicle and from that, we put methods that is common to all vehicles.

Ex.

public class Vehicle{

public void accelerate(){

}

public void decelerate(){

}

public void start(){

}

public void stop(){

}

public void turnDirection(){

}

}

Then we create a “sub-class” called “Car”:

public class Car extends Vehicle{

public void turnOnRadio(){

}

public void turnOnWiper(){

}

}

And, also inheriting from Vehicle class we create:

public class Bicycle extends Vehicle{

public void adjustMirror(){

}

public void soundRinger(){

}

}

The Car and Bicycle are called sub-classes and Vehicle is the super class. We call this “inheritance.”

Then we can access the super class methods in our code.

Ex.

public static void main(String[] args){

Car car1 = new Car();

car1.start(); //since we inherited the methods in super class Vehicle, we can call its methods.

car1.accelerate();

car1.turnOnRadio(); //we can also use Car's own methods.

car1.stop();

Note: We can also use Vehicle's properties such as variables, constructors by using the super keyword, and even the super class' overriding methods.

Ex.

public class Vehicle{

public void start(){

System.out.println(“Start your engines”);

}

}

public class Car extends Vehicle(){

public void start(){

super.start();

}

}

Example of executing constructors in super class:

public class Vehicle{

public Vehicle(){

}

}

public class Car extends Vehicle{

public Car(){

super();

}

}

It can also pass in arguments:

public Car(int wheels, int doors){

super(wheels,doors)

}

to be continued...

Sunday, June 7, 2009

Java Basics Using Qt Jambi Tutorial

Parameters and Arguments – Parameters are what we pass in variables in methods. We already seen this in previous examples:

Ex.

import java.lang.*;


public class VehicleMoving{

public static int gear;

public static int speed;

public VehicleMoving(int gear){

this.gear = gear;

}

public void accelerate(){

switch(gear){

case 1:

speed = 10;

System.out.println("the car is moving at " + speed);break;

case 2:

speed = 20;

System.out.println("the car is moving at " + speed);break;

}

}

public static void main(String[] args){

VehicleMoving car = new VehicleMoving(1);

car.accelerate();

VehicleMoving car2 = new VehicleMoving(2);

car.accelerate();

}

}


Vehicle constructor expects one argument which is (1) with the parameter gear. 1 is an integer, so it must have data types int vehicle (int gear).


Remember these Rules:

  1. The method call or class instance of constructors must have the same number of arguments or parameters.

  2. The arguments passed-in should have the same data type.

to be continued...

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...