Posts

Showing posts from 2012

Basic concepts in Oops - 2

Hi everyone, this is the continuation of the previous post. Here i am discussing some more important concepts in oops. 6) Destructor Destructors are invoked automatically when an object goes out of scope or when delete operation is used to destroy a dynamically created object. The method "destructor" is used in C++ to clean up an object's state. Garbage collector does the job of destroying unused objects in Java. 7) Inheritance Inheritance is the property by which objects of one class can access the properties of other class. There are two types of inheritance - single inheritance and multiple inheritance. In single inheritance one or more class inherit from a single base class. In multiple inheritance one or more class inherits from more than one base class. Following shows an example of inheritance. class Vehicle { int vehicleId; int milage; Vehicle(int a, int b) { vehicleId=a; milage=b; } } class Car extends Vehicle { int yearOfManufacture; Car(int a, int

Basic Concepts in Oops - 1

This blog post will teach about the basic concepts in Object oriented programming. I have written this post in the simplest possible way. 1) Object Objects are primary run-time entities in an object - oriented programming. Every objects has its own features and properties. Objects occupy space in memory. An object refers to an instance in class. 2) Class Class is a group of objects that have identical properties. Objects are variables of class type. A class is actually a model and a true specimen of object. In other words, class is a blueprint from which objects are created. Every object is defined using a class. public class Vehicle { int vehicleId; int mileage; int cost; } In the above example, Vehicle is a class and vehicleId, mileage and cost are its attributes. Vehicle vehicle = new Vehicle(); Here, vehicle is an object of the Class. 3) Instantiation The process of creating an object from the class is called instantiation. An object is the instance of t

Program to check whether a system is big-endian or little-endian

Hi everyone, this post will teach you how to check whether a system is big-endian  or little-endian. I have written the program in one of most simplest way. The program is very easy, provided you know what is meant by Endianness. Endianness defines the order in which system stores the bytes of a multibyte value. Endianness includes big-endian and little-endian systems. Big-endian system stores the Most significant byte (MSB) first and little-endian systems store the Least significant byte (LSB) first. Most of the personal laptop and systems which we use now are little-endian types, while most of the Power PC's use big-endian type of storing data. The term "big-endian" came from the famous novel "Gullivers Travels". How to check the endianness of a system Since you understood the concept, implementation won't be that difficult. It is as follows:- Assign num=1 Create a char pointer which point to "num". Check the de-refernced pointer. I

How to find whether 2 strings are anagram or not using java

2 strings are anagrams, if one string is a jumbled form of other string. In technical terms, we can say like, every characters in the first string should appear int the second string and the count of each character should be the same. Eg:- "ajva" is an anagram of "java". But "avaja" is not an anagram of "java" because the latter contains more number of "a". There are many methods to find out whether 2 strings are anagrams or not. One of the method is to sort both the strings and check whether they are equal. Minimum time complexity taken in this method is   O(n log n). The other method is to store the count of each of the characters of the strings in 2 separate maps. And then check the count of each characters. This method will use some extra storage spaces, but the time taken is in O(n). The program is as follows: import java.util.HashMap; import java.util.Map; public class Anagram { public static void

Sets in C++ made easy

This tutorial will helps you in learning about Sets in C++. I have written in the simplest possible way. Like Maps, sets are also a type of associate containers which stores unique elements. The element value in the set is the key itself. Set Sets are associative containers that stores unique elements. All elements are ordered in set in C++. Use Sets are mainly used whenever we want to store unique items of a group or activities relating to unique elements. Eg - Program to print unique words from a file. Defining a Set Sets in java are defined as follows: - set<string> set1; Instead of string, we can use int, float and other data types. Adding an entry to a Set Entry/ Values can be added to set in the following ways: - set1. insert("Sujith"); Checking whether a value is present in the Set Entry/ Values can be checked in the Set for their presence. It is similar to those in Map. We use count for checking whether a value is there or not. The entr

Map in C++ made easy

Hi everyone, lets move on to C++ for a while. This tutorial will help you in learning Map in C++. I have written in the simplest possible way. Before coming to C++, please refer my previous post about Map and Set in Java made easy. We won't be covering any implementation of Map in this post. MAP Similar to the maps in Java, map in C++ is an associative container that stores key and a mapped value. An associative containers refer to the group of class templates in the standard library of C++ programming language that implement ordered arrays. In map in C++, all the elements are ordered. The key values will be unique. Use Maps are mainly used, whenever we want to store particular occurrence of a variable. For e.g.: - Program to count the occurrence of all words in a file. Defining a Map Map in C++ is defined as follows: - map<string, int > map1; Instead of string and int, we can use other datatype as well.We should include the header file - #include<map>

Program to generate Excel Sheet Column Sequence

Excel Sheet Column sequence means a sequence like A,B,C..Z,AA,AB..ZZ,AAA...ZZZ..... . The program is very simple to code once we understood the logic. For converting a number to the corresponding string in this sequence, we need to know two things : - How many characters are there in the string. What is the corresponding value of each character. To find how many characters are there in the string, we should keep on diving the number/26 until it is greater than 0. To get the corresponding value of each character from last, we need to get number mod 26. One tricky thing about this algorithm is that, when we do the modulus of number corresponding to 'Z', we will get 26 mod 26 =0. We can add 26 to the modulus value if we get 0. Also in case of 'Z' , we need to subtract 1 from the result of number/26 so that our loop won't go for an extra iteration. For generating the Excel Sheet Column sequence to a given number, convert all the numbers from 1 to given numbe

Program to find whether a number is power of 2

This post will teach you how to find whether a number is power of 2. I am using 2 types of methods to solve this problem. Method 1 - checkPower If the number mod 2 not equal to 0, return false. Till number greater than 2, number = number/2 and check whether number mod 2 equal to 0 or not. If not equal to 0, return false. Return true at the end of loop. Time complexity of the program is O(log n). For better results, a new method is used. It is as follows. Method 2 - checkPower1 Find log(number)/log(2). If (2^ log(number)/log(2)) equals the given number, it is power of 2. Else not. Time complexity of the program is O(1). The program is as follows: - public class powerOf2 { public static void main(String args[]) { int num = 3; if (checkPower(num)) { System. out .println( " The number is a power of two" ); } else { System. out .println( " The number is not a power of two" ); }

Program to find the longest sequence in an array

This post will teach you to find the longest sequence of elements in an array. We need two sets in this program. One for adding the current elements and one which contains the longest sequence of elements.The logic is as follows:- Initialize current set and set containing longest sequence of values. For each current element, we will check the next elements. Clear the values in current set. Add the current element to a set. Assign previous element to current element If the next element is 1 greater than the previous element, update previous element as the next element and next element as next element of array. Else check whether the size of current set is greater than the set which contain longest sequence of elements. If it is greater, then remove all the elements form longest sequence set and add all elements of current sequence set to that. Since we need to loop all the elements twice, the time complexity of the program is O(n^2). The program is as follows: import