Thursday, May 14, 2009

Queue Using Stack


Tis post is the continues of my previous post and there will be two complete java program, the java version of my senior post and the revision version that I made in case in my opinion his program still have an error.

Queue as if in a line of waiting ticket for cinema has front and rear, the front is the the first person in that line who will get the ticket the first time, while the rear is the last person in that line that will get the ticket if he/she is lucky because he will get the ticket the last one.

If the first person already got his ticket, he will out from the line, and then the person behind him/her all person in the line move forward, I assume that got ticket person's named Imam, and the person behind him is Baihaqi, so Imam is no longer in the line, while Baihaqi is in the front now.

If we indexing the line by 0, 1, 2, 3, etc. Imam is no longer in the index, he already out for buying coke or chocolate milk for the movie, While Baihaqi is in index 0, before that he was in index 1. So do the last person named Ahmad, he is the rear or the last person in line, his index is decremented, from index number 30 now he is in index 29. And then there is a new person that also want to buy the ticket named Ali, now he is the rear or the last person in line, his index is 30, etc etc.

In queue there are enqueue and dequeue, enqueue is just like Ali when enter the line, he will be as the rear of the queue, his index is rear plus one, I mean in enqueue the rear is incremented while the front remains unchanged. While dequeue is just when Imam out from the line because of he already got the ticket, all the person moving forwards, now the person who is in the front now is change, the front person is Baihaqi not Imam anymore, but still the index of the front is always 0, and the rear is decremented.

In my senior program's there is a mistake about this, specifically in dequeue, I guess he thought that the front is the person, not the index, the first is Imam in front, and then after dequeue, Baihaqi is the front, but I think is a mistake, front and rear is refer to index, front is always 0, and rear is always change when enqueue or dequeue, if enqueue its increment, if dequeue its decrement.

I already capture the output of both program, and sign it when doing dequeue, after dequeueing it also print the index of the front and the rear, so it will more clear about them. Here is the full code of my senior program in java version:

package stack.queue;

import java.util.Scanner;

public class QueueUsingStack {
static final int MAXQUEUE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[] queue = new int[MAXQUEUE];
int front = -1;
int rear = -1;
int choice;

do {
System.out.println("*******************************");
System.out.println("Menu:");
System.out.println("1. ENQUEUE\t2. DEDQUEUE\t3. VIEW\t4. EXIT");
System.out.println("*******************************");
System.out.print("Please Choose Menu : ");
choice = scanner.nextInt();
switch (choice) {
case 1://enqueue
if (rear < MAXQUEUE - 1) { //is queue not full yet?
System.out.print("Data enter = ");
queue[rear + 1] = scanner.nextInt();
rear++;
System.out.println();
if(rear == 0) front = 0;
} else {
System.err.println("Queue is Full");
}
break;
case 2: //dequeue
if (front <= rear) {//
System.out.println("Data out: " + queue[front]);
front++;

System.out.println("front: " + front + ", rear: " + rear);
} else {
System.err.println("Queue is Empty");
}
break;
case 3:
for (int i = front; i <= rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
break;
case 4:
System.out.println("Exit...");
}
} while (choice != 4);
System.err.println("Program terminated");
}
}


I mark bold for the front declaration and statements when dequeue, the front is a variable that can change, the first initial is -1 and it increment when dequeue, while in dequeue, it only increment the front, and the data of queue is not moving, all the remain data should move forwards.

Below is my program, the diferent is I made the front as constant, because it always 0, and while dequeue, all of the data is moving forwards, and the rear is decremented. And I also comment the checking if rear == 0 then front = 0, I make the font bold. About the initialization of rear as -1, is because I follow my senior, It only initialization, you can initialize rear as 0, or whatever the number is doesn't problem because it will checked inside the program.

package stack.queue;

import java.util.Scanner;

public class MyQueueUsingStack {
static final int MAXQUEUE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[] queue = new int[5];
final int FRONT = 0;
int rear = -1;
int choice;

do {
System.out.println("*******************************");
System.out.println("Menu:");
System.out.println("1. ENQUEUE\t2. DEDQUEUE\t3. VIEW\t4. EXIT");
System.out.println("*******************************");
System.out.print("Please Choose Menu : ");
choice = scanner.nextInt();
switch (choice) {
case 1://enqueue
if (rear < MAXQUEUE - 1) { //is queue not full yet?
System.out.print("Data enter = ");
queue[rear + 1] = scanner.nextInt();
rear++;
System.out.println();
// if(rear == 0) front = 0;
} else {
System.err.println("Queue is Full");
}
break;
case 2: //dequeue
if (rear >= FRONT) {//is queue not empty yet?
System.out.println("Data out: " + queue[FRONT]);
//queue is moving
for (int i = FRONT; i < rear; i++) {
queue[i] = queue[i+1];
}
rear--;

System.out.println("front: " + FRONT + ", rear: " + rear);
} else {
System.err.println("Queue is Empty");
}
break;
case 3:
for (int i = FRONT; i <= rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
break;
case 4:
System.out.println("Exit...");
}
} while (choice != 4);
System.err.println("Program terminated");
}
}

Monday, May 11, 2009

Queue Using Double Linked List


This post is the continue of my previous post, and it is the java version of my senior post about queue using double link list, he use C, it implementing the data structure book by yedidyah langsam and andrew s. tanenbaum's book, a subject in bachelor of computer science.

In case in my senior post there is a comment that asked about double linked list, I want to describe about double linked list on my own word. it's a data structure which each object has three components:
- value, information that object had, it can be integer, or some other things, just like array of integer, or array of other things
- link to object in front of it. which is the same object type
- link to object in previous of it.

the head (the most front of list) did not have link to front object, and the rear did not have link to previous object, if both of those object have links, it called circular doubly linked list. fiuhhh feels like lecture when I write it, and I think I am a good lecture ^_^

By the way after I run the program which is the java version of my senior program, in case there's plenty example of double link list on net such as from java2s, I guess there still a question for my senior algorithm, I already capture the console of java version, and I think that program is not too queueing as properly.

Queue is just like when we want to buy ticket at the XII cinema, the first body in the head (front) will get his ticket first and then out from the list (FIFO). While stack is just like the order of oreo cake in a tuppleware, we put oreo one by one in one of thin tuppleware, and then we take out one by one also, the last oreo we put, the first one we take and eat with a glass of milk (LIFO).

When I run the program and then I insert the list in order like 10, 20, 30, and so on. The head (front) of the list it should be 10, the first one, not the last one, but when we choose to view the list, it say that 10 is the rear, and I think that the first mistake. The second mistake is when we choose Delete Front, It will delete the last input, I think it also a mistake, in the queue of XII ticketing, the Front is the first person in the line, so he/she is the first person will get the ticket, not the last one.

I will tell my senior about this mistake, and this is the full code of the program, just copy paste and run it in your environment as you want:

package stack.queue;

import java.util.Scanner;

class Node {
Node previous;
int info;
Node next;

public Node(int d) {
info = d;
}
}

public class QueueUsingDLL {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

Node newNode = null;
Node head = null;
Node tail = null;
Node tempNode = null;

int choice = 0;
do {
System.out.println("\n***************************************************************");
System.out.println("Menu:");
System.out.println("1. Insert Front\t2. View\t3. Search\t4. Delete Front\t5. Exit");
System.out.println("***************************************************************");
System.out.print("Please Choose Menu : ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Data entry: ");
int data = scanner.nextInt();
System.out.println("You're entering " + data);
newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head.previous = newNode;
head = newNode;
}
break;
case 2: // view
System.out.print("From HEAD: ");
tempNode = head;
while (tempNode != null) {
System.out.print(tempNode.info + " ");
tempNode = tempNode.next; // move to back
}
System.out.print("\nFrom TAIL: ");
tempNode = tail;
while (tempNode != null) {
System.out.print(tempNode.info + " ");
tempNode = tempNode.previous; // move to front
}
System.out.println("");
break;
case 3: // search
System.out.print("Searching number: ");
int search = scanner.nextInt();
tempNode = head;
while ((tempNode != null) && (tempNode.info != search)) {
tempNode = tempNode.next;
}
if ((tempNode != null) && (tempNode.info == search)) {
System.out.println("Data found");
} else {
System.err.println("Data not found");
}
break;
case 4: // delete front
// tempNode = head;
head = head.next;
if (head != null) {
head.previous = null;
System.out.println("Head deleted");
}
if (head == null)
tail = null;
break;
case 5:
System.err.println("Exit...\n");
break;
}
} while (choice != 5);
System.err.println("Program terminated");
}

}

Wednesday, May 6, 2009

Stack Using Array, Java Implementation of Langsam & Tanenbaum's Data Structure

I read my senior's post whom a lecturer in surabaya about stack using array, he is using C. I remember that problem arise in Data Structure when I was in bachelor, taken from Yedidyah Langsam and Andrew S. Tanenbaum's book. So I rewrite his code in java version. Feels great that you try to remembering what you learn for past years, and in fact at that time I donot understand at all about the subject.

package stack.queue;

import java.io.IOException;
import java.util.Scanner;

public class StackFromArray {
final static int MAX = 5;
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);

int stack[] = new int[MAX];
int top = -1;
int choice = 0;
do {
System.out.println("\n*******************************");
System.out.println("Menu:");
System.out.println("1. PUSH\t2. POP\t3. VIEW\t4. EXIT");
System.out.println("*******************************");
System.out.print("Please Choose Menu : ");
choice = scanner.nextInt();
switch (choice) {
case 1: //push
if (top >= MAX - 1)
System.err.println("Stack is full");
else {
System.out.print("Data You want to push : ");
int data = scanner.nextInt();
System.out.println("You pushed " + data);
stack[top + 1] = data;
top++;
}
break;
case 2: //pop
if (top < 0)
System.out.println("Stack is empty");
else {
System.out.print("Data out = " + stack[top]);
top--;
}
break;
case 3: //view
for (int i = 0; i < stack.length; i++)
System.out.print(stack[i] + " ");
System.out.println("\n");
break;
case 4: //view
System.out.println("Exit...\n");
break;
default:
break;
}
} while (choice != 4);
System.err.println("Program terminated");
}
}

Wednesday, April 29, 2009

Trip@Bandung




Last weekend me and office mates had trip to bandung, we had great time spend everything together.

Friday night at 1 am, we went from office, in case we wait panji doing his work on uploading production, actually he plan to go in saturday morning and I will go with him because in that time many works to do. It's about 00.30 am, and I already from home to take some clothes so I went back to the office, and we wait for rahmat that also joining us after had futsal. We went by two cars, Anita's and Kamil's car. One is go to Hendri's home first to take some clothes, and one again went to Herman's home to take clothes and Nintendo Wii.

And then we meet again in alfamart for buy something like soap, bread, and beverage. We then change our sits in car, there are 16 people of us, I was in Kamil's car with Kamil, Rere, Herman, Erna, mas Roy, Rahmat and Danik. While Anita's car there were Eka, Iqbal, Anita, Yanti, Mulan, Meli, Panji, and Hendri. It's about 3 hours until we were in Bandung in vila Istana Bunga. So it's about 4 am. The place is on the hill, and its so cold.

I cannot sleep while Im in the car, after in the place also cannot sleep, in the morning we doing many things, some of us were playing wii, me and rahmat take a walk near the house, some of us trying to get sleep, some others just talk each other in the tv room. we wait until about 9 am to take a shower, there are three bathroom one is in the second floor, and the other is in the ground. and there four sleeping room, three is in second floor, and one is in the ground floor, each of them has double bed so it's fit all of us.

When the sun is rising most of us take a walk to get a lot of pictures with a lot of styles. and some others is locked in house for a nap. And then Eka and some others buy some meals for breakfast.

In the day we went to cihampelas (or some place like that with full of stores sorrounding), and then we lunch in mcD in there. I buy panas special, rice, slice of fried chicken, scrumbled egg and a glass of coke. And then we back home in the afternoon, some of us take a bath, but I did not, because I choose take nap because we gonna had trip in midnight and Im not take a sleep yet.

In about 7 pm, we go to kampung daun, a beautiful places with full of eating places. I bought fried banana cheese and hot tea and then we go to Brebes restaurant to buy chicken barbeque. And then we go to ciater, a place with warm water for soaking. We were in ciater in about 1 am, the place is open for 24 hours and the funny thing is it's peak hour is in the midnight.

We were back to home when dawn. we take sleep for tomorrow agenda, actually we had plenty of schedule in the last day, like visiting gedung sate and to strawberry garden, but the fact is we spent last day happily in home by play wii, or cards, or watching motoGP.

In the midle of the day we eat padang food, I eat chicken. and then we went home, in the middle of the way we stop in burengreng for eating batagor, and then we stop again in primarasa store to buy goodies, I buy a box of brownies, and I take it to the office in monday.

Tuesday, April 21, 2009

Using Windows With Sun VirtualBox



Now Im not idle anymore, I was assigned in new project, but this project require me develop in windows environment, so I must install windows on my inspiron, first in my thought, there are two alternatives: install windows inside my Ubuntu, or dual booting. I already ask id-ubuntu mailist, here's my post link for this problem.

In case Robbie, an office mate, already face the same problem before, and he choose the first way (install windows inside ubuntu) so I did the same thing. I need two installer:
- virtualbox-2.1_2.1.2-41885_Ubuntu_hardy_i386.deb
- XP installer (I have the SATA bundled one, in case inspiron harddisk is SATA).

I just double clicked the virtaulbox installer, because it's .deb filetipes so I didnot need run it from terminal. Actually there already newest version of virtualBox, but then I must download it first, so I just used that I already have. And there will be some usual install question such as how much memory and disk space that I spend for virtualBox, I give 200 MB memory and 10 GB of disk space. And also give the virtualBox a name. I give it "loswom" same as my project, and if I read that name again I said to myself: "what a name is that :D".

There will be one added item in the virtualBox explorer: "loswom". Then try to start it and it will be error because we still don't have the windows, we need to install it first (As seen in the picture, there's a picture in blogspot, I hope also seen in facebook). The explorer is in the left of the window, such as folder explorer, and the Start button and Setting button is on the top of that explorer.

I already have the .iso installer from my other friend, If you ever read my previous post when I first install ubuntu for this inspiron, I already copy the xp SATA bundled installer on my hard drive, So click Setting button left side of Start button, and find some option for install windows and locate the installer in the appropriate place, and restart again and the system will install windows as autobooting.

Install windows as usual, and we already have our windows, it is so easy far from my fear as I ask to id-Ubuntu mailist before.

Tuesday, April 14, 2009

Good Old Fashioned Lover Boy

"I can dim the lights and sing you songs full of sad things
We can do the tango just for two "

That is the first two lines of the lyrics of "Good Old Fashioned Lover Boy", song from Queen but sang again by Jason Mraz. I love the lyrics, I love the music, I also have the original one from Queen.

I remember that past few years ago I buy pirate cd from street when I work in my previous office when I live in Cilandak, I new in this town, alone, donot have computer, so I buy radio+cd player, and I bought the cd, it company me when Im sad, it company me when I feel alone.

Now I heard again that song, I have the mp3 of a lot of Jason Mraz songs, one of them is this song. Here is the full lyric, I copy paste from a site:

I can dim the lights and sing you songs full of sad things
We can do the tango just for two
I can serenade and gently play on your heart strings
Be your valentino just for you

Ooh love - ooh loverboy
What're you doin' tonight, hey boy
Set my alarm, turn on my charm
That's because I'm a good old-fashioned lover boy

Ooh let me feel your heartbeat (Grow faster, faster)
Ooh ooh can you feel my love heat
Come on and sit on my hot-seat of love
And tell me how do you feel right after-all
I'd like for you and I to go romancing
Say the word - your wish is my command

Ooh love - ooh loverboy
What're you doin' tonight, hey boy
Write my letter
Feel much better
And use my fancy patter on the telephone

When I'm not with you
I think of you always
(I miss those long hot summer nights)
I miss you
When I'm not with you
Think of me always
Love you - love you

Hey boy where do you get it from
Hey boy where did you go ?
I learned my passion in the good old
Fashioned school of loverboys

Dining at the Ritz we'll meet at nine precisely
One two three four five six seven eight nine o' clock
I will pay the bill, you taste the wine
Driving back in style, in my saloon will do quite nicely
Just take me back to yours that will be fine (Come on and get it)

Ooh love, (There he goes again just like a good old-fashioned lover boy)
Ooh loverboy
What're you doin' tonight, hey boy
Everything's all right
Just hold on tight
That's because I'm a good old-fashioned fashioned lover boy

Wednesday, April 1, 2009

Appfuse 2, Intro with Maven

I create new project using appfuse2, the main difference with appfuse1.9 is now we use maven rather than ant to build for example. The tutorial is already clear in appfuse site. But I will rewrite it in my own words.

1. Jdk and tomcat for the simple one.
2. Install maven, just download it, or apt-get it when if using ubuntu as me
3. Install mySql, I want to make the simple project one, so I use mySql because it is the default of appfuse, same as maven we can download it or apt-get from repository.
4. From the terminal type: mvn archetype:create and choose the framework, I choose the Spring basic from the options, actually I already try to make tapestry project, but fail in the midle. Here is the full syntax to create the project with basic SpringMVC:

mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-spring -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject

5. We must connect with internet incase we use the maven repository from internet, and it will download bunch of things from repository. And it should end with something like build successfully. and a folder named myproject is created.

6. Because it will be easier when uding IDE, so we can do as follow:
- From eclipse, create new java project
- Choose the radio: create project from existing source and browse it into myproject folder
- name it myproject

7. From terminal enter into myproject folder and type mvn or "mvn appfuse:full-source" for getting all the resources. if we only type mvn we will refer sources or classes into the repository, we cannot see or modify the code. This stage needs lot of times because download many things, but we can stop it and continuing next time because once it download, it is not need to download again. I remember actually I start with appfuse2 few weeks ago and meet some fails, and yesterday I continue my work.

And AFAIR we will meet some errors with the default source code, first we need to copy junit jar and refer it. I do this with copying junit.jar and add library to it, we must know it when using eclipse, so there are no error sign in our source explorer.

And we must change the password of the database in file jdbc.properties:
jdbc.password=ourPassword
hibernate.connection.password=ourPassword

and in the file pom.xml:
<jdbc.password>ourPassword</jdbc.password>

also in the pom.xml if we want to use our installed tomcat we comment this statement:
<zipUrlInstaller>
<url>${cargo.container.url}</url>
<installDir>${installDir}</installDir>
</zipUrlInstaller>

8. Type mvn jetty:run-war and it also start tomcat, so we can see our application in http://localhost:8080