Search This Blog

Saturday, January 23, 2016

My Technical Memories - Memory 3

Ah! What is it? I could see a  lot of symbols. I had to ponder about it  a bit… Na.. I am joking. How can I ever forget Unix Shell Scripts? I have a long lasting relationship with it. It is really interesting to read my thoughts after a long… long… time. Here comes a small and simple snippet of thoughts….
Unix Shell Script to check every minute whether a user is logged in or not?
# usage is : scriptname.sh username
if [ $# -lt 1 ]; then
   echo improper usage
   echo Correct usage is : $0 username
   exit 1
fi
logname=$1
time=0
while true
do
    who | grep `$logname`  > /dev/null
    if [ $? –eq 0 ] ; then
       echo $logname has logged in
       if [ $time – ne 0 ]; then
         echo $logname is $time minutes late
       fi
      exit 0
    else
     time=`expre $time + 1`
     sleep 60
    fi
done

Unix and its look alike or derivatives are really amazing, may it be linux, solaris, venix, xenix, HP-unix, SCO- Unix. I hope I recollected my memory accurately. Why don’t you just execute and check… Lot more on its way… see you again later….

Thursday, January 21, 2016

My Technical Memories: Memory - 2

Here is what I found in the second memory. It looked like a random access to my thoughts. I wondered to see that right from IDENTIFICATION DIVISION it took me to COBOL data files. I thought of sharing it rather than putting it into the background. So here it comes. COBOL file or as a matter of fact any file is a collection of data stored on secondary storage device. In a broader perspective COBOL support three types of file organization.
  • Sequential File
  • Indexed File
  • Random File

Sequential File stores the data in the same order in which the data data is provided by the user. Sequential file can be Line, Record or Printer sequential file. Organization of the data in file can be specified in code as:
     SELECT MYFILE ASSIGN TO “MYFILE.DAT”
     ORGANIZATION IS LINE SEQUENTIAL.
Organization can be otherwise RECORD SEQUENTIAL or PRINTER SEQUENTIAL. By default it is RECORD SEQUENTIAL.
Indexed File  stores data using an Indexed key. Each record contains the primary key which is unique. In Indexed file you can store unique data. It can also be considered Master file or Master Data as we refer in COBOL. Separate file will be created with .idx extension to the data file. It will look something like this:
     SELECT INDFILE ASSIGN TO “INDFILE.DAT”
     ORGANIZTION IS INDEXED
     ACCESS MODE IS DYNAMIC
     RECORD KEY IS IND-FILE-REC-KEY.
By the way, you have a facility to define DUPLICATE KEYS, in that case file can contain redundant data, but hold on, there is a limit which varies depending on type of Index file. Indexed file can be accessed or in other words I can say that data in Indexed file can be organized in three different ways i.e. INDEXED SEQUENTIAL (default, records are accessed on Ascending/Descending Record Key), INDEXED RANDOM (Value of Record Key is used to access the Record) and INDEXED DYNAMIC (Program can switch between Sequential or Random mode by using appropriate I/O statements).
Relative File  identifies each record by ordinal position. This facilitates to access files sequentially or Randomly by using ordinal position in sequence or randomly. If you provide relative position while writing file as 1 and then 100, when you open this data file, can you imagine what will you see? Now, that it my question to you. I was fathomed by what I saw, it was interesting. Another interesting thing I noted while working on Relative files is that although I can create variable length records, System assume the size of largest record length and uses padding for unused spaces. The beneficial side is it brings speed. System can calculate the position of record to be accessed by using relative key and record length. So this is how it looks like:
     SELECT MYRELFILE ASSIGN TO “MYRELFILE.DAT”
     ORGANIZATION IS RELATIVE
     ACCESS MODE IS RANDOM
     RELATIVE KEY IS R-KEY.
Like Indexed file, Relative file can be accessed sequentially or randomly is ACCESS MODE is DYNAMIC.
Enjoy this read and look for more… Now what will be next…I have no idea, it depends, I need to decide whether I have to access my memory sequentially or randomly… have fun..


Monday, January 18, 2016

My Technical Memories : Memory - 1

I just opened my thoughts pot. Ah! I felt it was in a mess. So many different types of thoughts: Technical, Personal etc. It was just like if you have been downloading and saving a lot of information in a hard disk to manage it later. I realized I have been doing it. So I imposed a responsibility on myself to arrange it all. Now the question was how to proceed? To manage technical section of my thoughts was a huge task, as I have been working in the industry from a long time. So I opened that area and started looking at the thoughts to classify and manage. The first thought which I saw was related to COBOL. I have a special soft spot for COBOL, as this is the technology which I used in my very first job. So here it is :

Every COBOL program must have four divisions in the following order:

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.

Is this statement TRUE?

PS: I thought of putting my random technical thoughts here... keep looking for more...

Friday, December 18, 2015

Enhanced for loop and Immutability?

There I find a twist in the enhanced for loop and immutable nature. Where it does not change the data in an array of primitives, here I can see a clear variation.
Here is a Person class.
package test;
public class Person {
      private int pId;
      private String name;
      public Person(int pId, String name) {
          this.pId = pId;
          this.name = name;
     }
    public String toString(){
            return "Id : "+pId+" Name : "+name;
    }
    public int getpId() {
           return pId;
    }
    public void setpId(int pId) {
         this.pId = pId;
   }
   public String getName() {
         return name;
   }
   public void setName(String name) {
        this.name = name;
    }
}
And here is a tester class with enhanced for loop.
package test;
import static java.lang.Math.random;
import java.util.ArrayList;
import java.util.List;
public class PersonList {
      public static void main(String[] args) {
           List<Person> pList = new ArrayList<>();
           pList.add( new Person(101,"John"));
           pList.add( new Person(102,"James"));
           pList.add( new Person(103,"Dan"));
           pList.add( new Person(104,"Mathew"));
           pList.add( new Person(105,"Sam"));

          System.out.println("Added List elements : "+pList);
         /* To perfomr element wise operation */
          for(Person p : pList){
                System.out.println(p);
          }
         /* you try to modify the data in pList through enhanced for loop */
                System.out.println("List elements now : ");
                for(Person p : pList){
                     if(p.getName().equals("Sam"))
                          p.setName("Samuel");
                 }
/* OOPs... it has not changed...But let us conform it once again by using enhanced for loop */
          System.out.println("After Updation : view element in the list : ");
          for(Person p : pList){
                 System.out.println(p);
          }
  }
}
When I run this I see something different.
Added List elements : [Id : 101 Name : John, Id : 102 Name : James, Id : 103 Name : Dan, Id : 104 Name : Mathew, Id : 105 Name : Sam]
Id : 101 Name : John
Id : 102 Name : James
Id : 103 Name : Dan
Id : 104 Name : Mathew
Id : 105 Name : Sam
List elements now : 
After Updation : view element in the list : 
Id : 101 Name : John
Id : 102 Name : James
Id : 103 Name : Dan
Id : 104 Name : Mathew
Id : 105 Name : Samuel
Now the question is why the name gets changed from within the enhanced for loop. Now in previous post x reads data from ArrayList, and value change in x does not reflect in ArrayList. But in current example, p holds the reference to an object and using reference, it change the value. Rest is left to your imagination... :) !!!


Wednesday, December 16, 2015

Enhanced for Loop : Collections and Immutability

Enhanced for loop was added in Java SE 1.5. It can be used with Arrays and collections to iterate item-wise/element-wise. It works like read data of very ancient programming language called BASIC... atleast to me it looks like that... You can define read element of the collection element type and do operations on data. The amazing part is whatever you do to the read element, it does not disturb the original collection, that is data in for loop is immutable...
package test;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Math.random;
/**
*
* @author nkan
*/
public class EnhancedForLoop {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
for (int i=0;i<5;i++){
intList.add((int) (random() * 100));
}

System.out.println("Added List elements : "+intList);
/* To perfomr element wise operation */
for(int x : intList){
x++;
System.out.println("x = "+x);
}
/* you have incremented x, may be you are thinking that ArrayList : intList has be modified */
System.out.println("List elements now : "+intList);
/* OOPs... it has not changed...But let us conform it once again by using enhanced for loop */
System.out.println("After Updation : incremented element list is : ");
for(int x : intList){
System.out.println("x = "+x);
}
}
}
When I executed this program :
Added List elements : [98, 14, 30, 34, 43]
x = 99
x = 15
x = 31
x = 35
x = 44
List elements now : [98, 14, 30, 34, 43]
After Updation : incremented element list is : 
x = 98
x = 14
x = 30
x = 34
x = 43

Created and posted by Nancy @ 12/17/2015 2:25 am

Friday, October 16, 2015

Understanding mysterious static - 2

Oh! that was interesting, if you tried executing the program in the previous post. For a second may be it gave a food for thought to your mind. Why main is not executing first? Why am i getting output as:

In static block...InterestingStatic1
In main...InterestingStatic1

May be something is happening before main is executing. I must know it. Everything is mystery until understood. So let me take you to another interesting and mysterious scenario :

Here is what you do. Nothing much. Add InterestingStatic1Test class to the project you created yesterday. Let the class InterestingStatic1 be there in the project without any changes.

package teststatic;
public class InterestingStatic1Test {
    static{
        System.out.println("In static block...InterestingStatic1Test");
    }
    public void printSomething(){
        System.out.println("Just printing ... Welcome to Mysteries of Java");
    }
    public static void main(String[] args) {
        System.out.println("In main...InterestingStatic1Test");
        InterestingStatic1Test justObject = new InterestingStatic1Test();
        justObject.printSomething();
        InterestingStatic1 newObject = new InterestingStatic1();
    }
}

Yes, you are right. I am going to ask you to guess the output first and then run the program. Does it match your guess? Why the output is what it is.  wait... there is more to come ... See you...

©K.A.N. Nancy

Thursday, October 15, 2015

Understanding mysterious static - 1

package teststatic;
public class InterestingStatic1 {
    static{
        System.out.println("In static block...InterestingStatic1");
    }
    public static void main(String[] args) {
        System.out.println("In main...InterestingStatic1");
    }
}
What do you think should be the output. Normal understanding is main is executed first by JVM. Run this code. Do you find something unexpected?
More interesting code … tomorrow… let the mystery of static be resolved slowly, so that you enjoy…

©K.A.N. Nancy