Saturday, February 28, 2009

3 - Class Structure

Lets begin our discussion by refreshing our memory with hello world application. A simple one class program :
public class HelloJava // The class declaration
{

public static void main(String args[]) //The main method declaration
{
System.out.println("Hello Java : a cup of arabica and robusta blend");
}

}

Inside the program we have :
1.class declaration.
2.method declaration
3.Some comments

Every class inside our application must be declared. A Declaration is like telling the world that this class exists. By convention, class names begin with a capital letter, and usually I use uppercase letter for every word that is part of a class name. Lets disect our HelloJava class :
1.syntax for class declaration is [access_modifier] class class_identifier
2.access_modifier determines the accessibility of other classes to this class. Our access modifier is public so that this class is visible and accessible to other class
3.class identifier or better known as class name is HelloJava. Our class name is HelloJava.
4.Every java program must have one main() method. Our method is declared inside HelloJava class. Our main method comprises only one statement : System.out.println("Hello Java : a cup of arabica and robusta blend"). This statement will be executed during runtime.
5.In HelloJava we haven't got any variables yet.

Comments is good for documenting our logic of programming. Comments inside a java program takes the form of :
1.//. This type of comment may spans only one line of comments
2.Traditional comment just like in c language : begin with /* end with */. This type of comments may span several lines of comments.

Until now we haven't got any variable declaration. Now lets have a look at another examples : Triangle. We have two variables, which are public. base and height are public. It means they can be accessed and manipulated by another class.

public class Triangle
{
public float base = 0;
public float height = 0;

public void showTriangle()
{
System.out.println("Base : " + base);
System.out.println("Height : " + height);
System.out.println("Area : " + base*height/2);
}

}

Another interesting declaration is the method : showTriangle. It is public but it has a key word void. It means ON COMPLETION IT DOES NOT RETURN ANY VALUE. It has 3 statements that will be executed during run time.

2 - Multiclass Java Program

Now it is time for us to write application (simple one) with more than one class. The specification for our application is that we have a class that create an instance of another class. Our main class is called TriangleCreator, and the other class is called Triangle. TriangleCreator will create an instance of a Triangle.

The scenario for building such program is that we need to have a class that will be used by another class. The first class we need to write is Triangle class, the class that creates another class is called TriangleCreator. Now we have class Triangle that is depicted bellow :

public class Triangle

{


public float base = 0;
public float height = 0;

public void showTriangle()
{
System.out.println("Base : " + base);
System.out.println("Height : " + height);
System.out.println("Area : " + base*height/2);
}

}

what you have to do is the same with the other exercises we have done before :
1.Copy the source code.
2.Paste it into your text editor.
3.Save the program as Triangle.java IN YOUR WORKING DIRECTORY.

Next we need to have TriangleCreator class.

public class TriangleCreator
{

public static void main(String args[])
{
Triangle oTriangle;
oTriangle = new Triangle();
oTriangle.base = 10;
oTriangle.height = 10;
oTriangle.showTriangle();
}

}

Here is a list of action you need to do :
1.Copy the source code.
2.Paste it into your text editor.
3.Save the program as TriangleCreator.java IN YOUR WORKING DIRECTORY.
4.Compile the program using this command :javac TriangleCreator.java
5.After completing compilation process, a bytecode is produced : TriangleCreator.class
6.You run your program by executing this command : java TriangleCreator

In the next section we are going to discuss what we have done, discussing the syntax of making class.

Tuesday, February 24, 2009

1 - Introduction to Java Programming

Java is Object Oriented Programming Language. You can find many explanation of java environment and architecture somewhere else. This page is focusing on the programming example. If you see the examples in this page you will notice that a java program at least consist of a class declaration, method declaration, comments (if required/optional), and variable declaration (usually a class has several variables).

Our first program is hello java. The source code is presented below. All you have to do is copying and pasting it into your text editor, compile, and run the program :

public class HelloJava // The class declaration
{

public static void main(String args[]) //The main method declaration
{
System.out.println("Hello Java : a cup of arabica and robusta blend");
}

}


1. Copy the source code.
2. Paste it into your text editor.
3. Save the program as HelloJava.java IN YOUR WORKING DIRECTORY.
4. Compile the program using this command :javac HelloJava.java
5. After completing compilation process, a bytecode is produced : HelloJava.class
6. You run your program by executing this command : java HelloJava

We have more programming work to do. We are going to have another program that will print multiple lines of output. The source code is presented below. In case you are wondering what kopi luwak is Kopi luwak is coffe beans fermented inside the digestion system of a weasel like animal called luwak.

public class HelloKopiLuwak
{

public static void main(String args[])
{
System.out.println("Kopi Luwak : Exquisite Coffe produced by digestive system of ");
System.out.println("Paradoxurus hermaphroditusa, raccon-like animal." );
System.out.println("Perhaps it costs 300 or 400 dollars per kilo ");
}

}

1. Copy the source code.
2. Paste it into your text editor.
3. Save the program as HelloKopiLuwak.java IN YOUR WORKING DIRECTORY.
4. Compile the program using this command :javac HelloKopiLuwak.java
5. After completing compilation process, a bytecode is produced : HelloJavaKopiLuwak.class
6. You run the program by executing this command : java HelloKopiLuwak

We can also modify the program so that instead of having 3 println statements. We only need one.

public class HelloKopiLuwakModified
{

public static void main(String args[])
{
System.out.println("Kopi Luwak : Exquisite Coffe produced by digestive system of\nParadoxurus hermaphroditusa, raccon-like animal\nPerhaps it costs 300 or 400 dollars per kilo ");
}

}

Then what you have to do is :
1. Copy the source code.
2. Paste it into your text editor.
3. Save the program as HelloKopiLuwakModified.java IN YOUR WORKING DIRECTORY.
4. Compile the program using this command :javac HelloKopiLuwakModified.java
5. After completing compilation process, a bytecode is produced : HelloKopiLuwakModified.class
6. You run the program by executing this command : java HelloKopiLuwakModified

Until now, we only have one class. A one class program is fine inside classroom. But in actual world you need to have several classes. How to make such construct will be discussed on the next section of Java Programming.

Sunday, February 8, 2009

Serial Port Programming : Sender and Receiver

The required equipment required consists of :
1. DB-9 null modem. The null modem that you need to use is loop type null modem.
2. USB to serial port converter.

Now we are going to write a program that act as a data sender. Data is sent through the serial port. Basically program consists of several activities :
1.turning on a null modem.
2.setting up program's action on incoming package.
3.setting up program's output mode.
4.setting up program's connection control flag :
1.Baudrate is 4800 BPS (B4800) .
2.Enable RTS/CTS (hardware) flow control (CRTSCTS) .
3.Ignore modem control lines (CLOCAL) .
4.Character size mask is 8 (CS8).
5.flushing the modem line.
6.initiating the modem.
7.sending data through the modem line. the data is system's time.

Of course you need to have a receiver. wich is also a program. What makes the receiver different from the sender is the loop that reads data input :
for(;;)
{
read(fd,&c,1); /* reads the null modem */
write(1, &c,1); /* prints input to the screen */
}

before running any program you really need to alter the status of your null modem device (after you plug in it)), so that it can be read and written on. REMEMBER ALL SERIAL COMMUNICATION DEVICES IS TREATED AS FILE. You have to make it readable and writable. Beside that you need to have a null modem :
1. For changing the permission of ttyS0 you need to execute : #sudo chmod 777 /dev/ttyS0
2. For changing the permission of ttyUSB0 you need to execute : #sudo chmod 777 /dev/ttyUSB0

Now first we have a sender's source code. We assume program below uses Serial Port 0.

#include "termios.h" /*this file contains declaration of required functions and types*/
#include "stdio.h" /*you know what this is*/
#include "fcntl.h" /*this file constains declaration for O_RDWR and O_NOCTTY)*/
#include "time.h" /*this file contains time's operatives*/


int main(void)
{
int i, j, s;
char t[9];
time_t ttrs;
struct tm *ti;
int fd; /*variable of type integer used as descriptor to a serial port*/
char c;
struct termios termiosA; /*declaring a variable of type termios*/
/* Turn on the null modem */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); /* I have done : sudo chmod 777 /dev/ttyS0 */


/* We don't care about incoming bytes with parity on data input */
termiosA.c_iflag = IGNPAR;

/*
Raw output.
*/
termiosA.c_oflag = CR0; /* output modes */

/*
we are going to set a connection with serial port to the receiver (this computer is a sender)
1. Baudrate is 4800 BPS (B9600)
2. Enable RTS/CTS (hardware) flow control (CRTSCTS)
3. Ignore modem control lines (CLOCAL)
4. Character size mask is 8 (CS8)
*/
termiosA.c_cflag = B4800 | CRTSCTS | CLOCAL | CS8; /* control flags */


/* flush the modem line */
tcflush(fd, TCIFLUSH);

/* initiate the modem */
tcsetattr(fd,TCSANOW,&termiosA);

/* Now its time to send something. */
i = 0;
for (;;)
{
time(&ttrs);
ti=localtime(&ttrs);
if (s != ti->tm_sec)
{
printf("Sending time data : %s", t);
sprintf(t, "%d:%d:%d\n", ti->tm_hour, ti->tm_min, ti->tm_sec);
s = ti->tm_sec;
for (j = 0; j < 9;j++)
{
write(fd, t[j], 1);
}
return 0;
}

Next we have a receiver program. The receiver uses USB port 0. :

#include "termios.h" /*this file contains declaration of required functions and types*/
#include "stdio.h" /*you know what this is*/
#include "fcntl.h" /*this file constains declaration for O_RDWR and O_NOCTTY)*/

int main()
{
int fd; /*variable of type integer used as descriptor to a serial port*/
char c;
struct termios termiosA; /*declaring a variable of type termios*/
/* Turn on the null modem */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); /* I have done : sudo chmod 777 /dev/ttyUSB0 */


/* We don't care about incoming bytes with parity on data input */
termiosA.c_iflag = IGNPAR;

/*
Raw output.
*/
termiosA.c_oflag = CR0; /* output modes */

/*
we are going to set a connection with serial port to the sender (this computer is a receiver)
1. Baudrate is 4800 BPS (B4800)
2. Enable RTS/CTS (hardware) flow control (CRTSCTS)
3. Ignore modem control lines (CLOCAL)
4. Character size mask is 8 (CS8)
*/

termiosA.c_cflag = B4800 | CRTSCTS | CLOCAL | CS8; /* control flags */

/*The change to the line's state takes effect straight away*/
/* flush the modem line and initiate the modem */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&termiosA);
/* input is handled by the loop below */

for(;;)
{
read(fd,&c,1); /* reads the null modem */
write(1, &c,1); /* prints input to the screen */
}

}

Now what you need to do is to compile and run each program and see the result. Good luck