Showing posts with label Java Generics. Show all posts
Showing posts with label Java Generics. Show all posts

Tuesday, June 30, 2015

Wednesday, June 24, 2015

PDF File Generation example in Java using iText Library

made an example where reports can be generated in PDF file format using iText library for Java. I made a swing app and we enter the data and then click the create pdf button to generate the pdf. Its not a tutorial but more like an example with a demo. Use the code, and I hope its readable enough.

[Image: 2Ssa87i.png]


Continue Reading →

Friday, June 19, 2015

Learning About the Stack Data Structure

Simple representation of a stack
Simple representation of a stack (Photo credit: Wikipedia)
Hello Everyone,

Today we will study about the data structure called Stack. But before roceeding with the tutorial I would like to read the following readers note.

Reader's Note :
Now we are good to start, So, Let's Begin.


Stack : A stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection. The operations that are performed on the the stack are push which means to insert an element at the top of the stack and pop which means to remove the top element from the stack.
Stack follows LIFO (Last In First Out) Principle, that is the element which is inserted into a collection last is taken out first from the collection as well.
The concept of stack data structure is from from development point of view and it has found numerous applications, include system level design and making efficient processes.

Applications of Stack
 
1. Data Parsing
2. Compiler Designing.
3. Recursion
4. Solving numerous Classical problems like Tower of Hanoi.
5. System Memory Management.
6. Reversing String.
7. Mathematical Expression evaluation. (Converting them from infix to postfix and then manipulating evaluating them)
8. Backtracking.
etc...
 
Stack as an Abstract datatype


StackADT {

push(item) : insert the item into the stack


pop() : removes the top element.
size() : Returns the size of the stack.
isEmpty() : checks whether the stack is empty or not.
isFull() : checks whether the stack is full or not.
}
Now let us understand the concept of stack better by taking an example :-
 
Suppose we consider an array of the following elements :-

5 <--- Top element
6
7
8
9
 
Now we push another number onto the stack, say 34, then our stack has the following structure :-

34 <--- Top element.
5
6
7
8
9
 
So it is evident that the most recently inserted items are push onto the top of the stack following the LIFO principle. Again we push another number into the stack, say 2, just following the above concept we have the following :-

2 <--- Top element.
34
5
6
7
8
9
 
We have understood the push operation and so lets see how does pop works. Lets pop once from the above array and the array becomes :-

34 <--- Top element.
5
6
7
8
9
 
As you can see the top element was poped from the stack and the next element that is 34 becomes the top element. Okay so lets pop two more elements and then the stack has the following structure :-

6 <--- Top element.
7
8
9
 
What happened above ?
 
First 34 is poped and then 5 becomes the top element and then another element i.e. 5 is poped and 6 becomes the top element.
I hope you have understood the concept of stack. Since we have understood the concept of stack and so lets write some code now. I will use Java to write the code. We create an interface to understand the concept of our ADT.

StackInterface.java


JStackArray.java

A Visual or Animated Representation of Stack can be found here

Okay, We are now ready to study some applications of Stack. So lets begin.

Example of an Application of Stack

String Reverse.

A Stack can be used to reverse a list, integer, string etc. Lets Look at the following example to understand the concept better :-

Suppose we have a string variable NAME and a string value of "Firun" assigned to it.

String NAME = "Firun";

Now since Stack follows the LIFO principle so if we insert every character from NAME into the stack then we will have 'n' as the top element. Then we can pop the elements we will have our Strings reversed. Look at the following working :-

1. Insert 'F' into Stack. We have

'F' <--- Top

2. Insert 'i' into Stack. We have

'i' <--- Top
'F'

3. Subsequently we insert the remaining characters as well and we get the following Stack structure.

'n' <--- Top
'u'
'r'
'i'
'F'

Now we pop all the elements from the stack and we have the following sequence :-

'u' <--- Top
'r'
'i'
'F'

We get 'n'

'r' <--- Top
'i'
'F'

We get 'u'. Also initially we had 'n' and so now we add 'u' to it and we get "nu". Proceeding in a similar way we get the final string as "nuriF".

I hope you understood the logic. So lets peep at the function code :-

private String reverseString(){
       String m="";
       for(int i=0;i<str.length();i++){
           stack.push(str.charAt(i));
       }
       for(int i=0;i<str.length();i++){
          m=m+stack.pop();
       }
       return m;
   }

Algorithm

PROCEDURE REVERSE_STRING(M) {
    INITIALISE LEN = LENGTH(M)
    FOR I TO LEN:
        STACK.PUSH(M[I])
    FOR I TO LEN:        
        N = N + STACK.POP()
    RETURN N
}

If you are able to make it to this end then it probably means that you have read the tutorial. I hope you enjoyed it. If you have any questions or doubts then feel free to ask.

Stacks are very useful and they reduce your workload, but it solely depends on the programmer how efficiently he uses these concepts. More applications of Stack like Conversion to different notations and Tower of Hanoi will be covered separately.

Feedback would be appreciated. Comment in the comment section below.


Thank you,
Sincerely,
Psycho_Coder.
Continue Reading →

Monday, June 15, 2015

Bag Data Structure Implementation in Java


A Bag is a collection where removing items is not supported. It purpose is to provide users with the ability to collect items and then to iterate through the collected items. At the end you have an option to empty the complete bag.

In this implementation you have an additional method to increase the Bag storage.

You can get the eclipse project and the project jar from here.

You can simply run the jar file from the terminal as :- 

java -jar Bag.jar

It will simply show you the output the sample test file for the Bag DS. I will provide the source for the test file which shows how to use it. Here's the code :-


Output  :- 

Initial Size of Bag (No of elements): 0
Capacity (No. of items the bag can hold) :5
Size of Bag after inserting items : 4
Capacity of Bag after extending the size :10
Size of Bag after inserting more items : 8
Elements present in the Bag :- 

12
212
1
123
142
12
19
131
No. of elements in the Bag (When used counter) :8
Size of Bag after clearing : 0


I hope you liked this post. Share it in your social networks.

Thank you.

Continue Reading →

Image to ASCII ART converter in Java

English: ascii art example. "Block" ...
English: ascii art example. "Block" or "High ASCII" style, cf. ANSI art. (Photo credit: Wikipedia)

ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII). The term is also loosely used to refer to text based visual art in general.
-- From Wikipedia

Ascii Art is pretty cool and there are several online services available which allow you to use convert an image to its ASCII ART form. These are often used in image boards or online communities as Avatar images. So I made a little java program which takes an image and converts it to its ASCII form.

How to Use ?

This code doesn't takes command line input so you need to pass the image name with extension in the main function.

Example :-

obj.convertToAscii("Absolute_Path_To_Your_Image.jpg");

When you run the code a text file "asciiart.txt" is created which has the Ascii Art of the image you gave as input. For better vision of the converted image set the font size to 2 or 1. 
Source Code
Sample Run :-

The image I used is :-

[Image: 2YpkRjy.png]

The output is (See the text in notepad)

[Image: i0J2jdp.png]
Sample Demo 2
Input Image
 
Output
I hope you learned something new and will be using this code to make your own ASCII art from images. See you later :) 
Continue Reading →

Read Character Files line by line in Java version 6 or less

Duke, the Java Mascot, in the waving pose. Duk...
Duke, the Java Mascot, in the waving pose. Duke images are now Free Graphics see duke:Project Home Page for more details. (Photo credit: Wikipedia)

In this post I am sharing a code on how to read character files line by line for Java version 6 or less. The current java version being 8 and the way to read the files is a bit different, like we use Java NIO primarily. Still for the sake of knowledge this post will help you clear some concepts.

The following source shows you the way to read a file in Java version 6 or less (compatible with JDK 5 but not 4)

It covers most of the error handling cases possible and note that we should close BufferedReader when not in use. The following prints the time taken by the code to read the file and total number of the contents read (In this case I read a file containing all the words of a dictionary)



Related articles
Continue Reading →

Saturday, June 13, 2015

Java Code to implement a Stack using Java Generics


In this post I have implemented Stack using Java Generics and the LinkedList class. This post isn't a tutorial rather an implementation of Stack Data Structure.

Code for Stack Interface : StackInterface.java



Code for Stack Interface : JStack.java


Demo Run of Above Implementation

Output


Print All Elements in Stack : 

4
3
2
1

All Elements in Stack are : 
2 1 

Top Element : 2

Size of Stack : 4
Continue Reading →

Follow Me!

Followers

Visitor Map