Posts

Showing posts from October, 2015

Overriding equal method

In java when we use equals method to compare two objects it returns true only if these referring to same memory location. Code public class OverrideEqual{ public static void main(String[] args){ Person p1= new Person("John",17); Person p2= new Person("John",17); if(p1.equals(p2)){ System.out.println("p1 equals to p2"); }else{ System.out.println("p1 is not equals to p2"); } } } class Person{ String name; int age;         Person(String name,int age){ this.name=name; this.age=age; } } Output:  p1 is not equals to p2 So if we want to compare two objects according to the states or values then we have to override the equals method. Code: public class OverrideEqual{ public static void main(String[] args){ Person p1= new Person("John",17); Person p2= new Person("John",17); if(p1.equals(p2)){ System.out.println("p1 equals to p2"); }else{ System.out.println("p1 is not

Simple Sorting and Searching Algorithms

Bubble Sort   In bubble sort we compare a adjacent pair of items and swap if they are in incorrect order until all the items are sorted out. Code: public class BubbleSort{ public static void main(String args[]){ int []arr={99,34,23,54,32,10}; bubbleSort(arr); System.out.println("\nSorted Array:"); printArray(arr); } static void bubbleSort(int []arr){ int temp; for(int k=arr.length;k>1;k--){ for(int i=0;i<arr.length;i++){ if(arr[i]>arr[i+1]){ temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } printArray(arr); } } static void printArray(int []arr){ for(int i:arr){ System.out.print(i+", "); } System.out.println(); } } Output: 34, 23, 54, 32, 10, 99, 23, 34, 32, 10, 54, 99, 23, 32, 10, 34, 54, 99, 23, 10, 32, 34, 54, 99, 10, 23, 32, 34, 54, 99, Sorted Array: 10, 23, 32, 34, 54, 99, Linear Search   In linear search we gonna take value by value sequentially until we find the item or until all the items a

JAVA

Image
Both the program and related documents on that program together is considered as a software. There is three basic stages when we are developing a program in any kind of language. Design Develop Test To develop computer programs we should select a programming language for that. There are many kind of languages that has been evaluated until now. If we categorize these languages as, machine oriented (Low level Languages) problem oriented (High level languages) In first generations of programming languages they used binary values to program software. So the programmer should have very good knowledge about hardware implementation of the system and how does circuits operates. In second generation languages we used assembly language which used mnemonics (Simple English words) to represent simple instructions such as add, store instead on binary instructions which used in first generation. But still the programmer needed better understanding on machines. Third generation languages

Polymorphism

The ability of an object to behave differently in different forms is called as polymorphism. So the methods have multiple implementations and the compiler or in runtime java will decide which method to execute. If the compiler can decide it, we called it static polymorphism (example: method overloading) while if it is determined in runtime we called it dynamic polymorphism (example: method overriding). Below code is an example for method overriding. Code: public class testPolymorphism{ public static void main(String []args){ Person p1; p1= new Person("John",20); p1.eat(); p1.sleep(); p1= new Student("Merry",16); p1.eat(); p1.sleep(); } } class Person{ String name; int age; Person(String name,int age){ this.name=name; this.age=age; System.out.println(this.name+":"+"I'm a Person"); } void eat(){ System.out.println(this.name+":"+"Eating..."); } void sleep(){ System.out.println(this.nam

Inheritance

Inheritance is a concept which allows to create an " is-a relationship " in between two classes. It's similar to partent-child relationship. As example think of a student. Every Student is a Person . So Student inherits every behaviour and attribute which a person have. (this depends on access specifier. As example if the access specifier is private then those fields or methods cannot be inherited by its sub classes.) Code: public class TestInheritance{ public static void main(String []args){ Person p1 = new Person("John",20); p1.eat(); p1.sleep(); Student s1= new Student("Merry",16); s1.eat(); s1.sleep(); s1.study(); } } class Person{ String name; int age; Person(String name,int age){ this.name=name; this.age=age; System.out.println(this.name+":"+"I'm a Person"); } void eat(){ System.out.println(this.name+":"+"Eating..."); } void sleep(){ System.out.println(this.name+

Encapsulation

Encapsulation is the process of wrapping or binding all the functions and data together into a single unit. So we can hide the implementation of the code from the user by using access specifiers. We can make private member variables and have public getters to read data from the variable and setters to write data to the variable, so likewise we can control the access to those variables. Code: public class TestEncapsulation{ public static void main(String args[]){ Circle c1=new Circle(7.0); System.out.println("Radius of the circle is:"+c1.getRadius()); System.out.println("Perimeter of the circle is:"+c1.getPerimeter()); c1.setRadius(14.0); System.out.println("Radius of the circle is:"+c1.getRadius()); System.out.println("Perimeter of the circle is:"+c1.getPerimeter()); } } class Circle{ private double radius; private double perimeter; Circle(double radius){ this.radius=radius; setPerimeter(); } public void setRadiu

Abstraction

Showing only the functionalities in more generalized manner is called as abstraction. In java we can achieved this either by implementing an Interface or an abstract class. Interface provides 100% abstraction while Abstract class can provide a partial abstraction in between 0% to 100%. Abstract Class If a class is declared using the key word "abstract" then we called it as an abstract class. We can't instantiated an abstract class. (Can't make objects) but it can have sub classes. Code: -------------------------------------------------------------------------------------------------------------------------------- public class TestAbstractClass{ public static void main(String args[]){ Student s1= new Student("John", 18); s1.eat(); s1.sleep(); } } -------------------------------------------------------------------------------------------------------------------------------- abstract class Person{ String name; int age; Person(Strin