Category Archives: Language

Loops in python

Loops in python:

Output:

FOR Loop with index

Output:

 

Data Mining:Intros and Weka With Java

https://www.ibm.com/developerworks/library/os-weka1/

http://www.programcreek.com/2013/01/a-simple-machine-learning-example-in-java/

http://www.cs.umb.edu/~ding/history/480_697_spring_2013/homework/WekaJavaAPITutorial.pdf

Advanced string manipulation

Actually we use this method to split string from a big sentence using regex.

String Manipulation

Java OOP Examples

https://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html

Triangular Number Java Code True or False

Here yo can see what is triangular number:

https://en.wikipedia.org/wiki/Triangular_number

 

Java Tut – Packages

Packages are used in Java in order to prevent naming conflicts,to control access, to make seraching/locating and usageof classes,interfaces,enumberations,annotations easier etc.

some of the existing packages in Java are:Java.lang – bundles the fundamental classes
java.io – classes of imput, output functions are bundled in this package
example:
http://www.tutorialspoint.com/java/java_packages.htm

Java Tut – Interface

Interface is a collection of abstract methods.A class implements an interface, thereby inheriting the abstract method of the interface.
Declaring Interafces:

interface keyword is used to declare an interface.simple example:

Implementing/Declaring Interfaces:
– An interface is implicitly abstract.You do not need to use the abstract keyword while declaring an interface.
– Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
– Methods in an interface are implicitly public.

example:
Animal.java

Implementing Interfaces:
When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific of the behaviors of the iterafce, the class must declare itself as abstract.

A class uses the implements keyword to implement an interface.Thje implements keyword appears in the class declaration following the extends portion of the declaration.
example:
MammalInt.java

Output:

When overriding methods defined in interfaces there are several rules to be followed:
– Checked exceptions should not be declared on implementation methdos other than the ones declared by the interface method or subclasses of those declared by the interface methods.

– The signature of the interface method and the same retrun type or subtype should be maintained when overriding teh methods.

– An implementation class itself can be abstract  and if so interface methods need not to implemented.

When implementation interfaces there are several rules:
-A class can implement ore than one interface at a time.
– A class can extend only one class,but implement many interfaces.
-An interface can extend another interface,similarly to the way that a class can extend another class.

Extending Interfaces:

An interface can extend another interface, similarly to the way that a class can extend another class.The extends keyword is used to extend an intrerface, and the child interface inherits the methods of the the parent interface.

The following sports interface is extended by Hockey and Footbal interfaces.
Filename: Sports.java

The hockey interface has four methods, but it inherits two from Spors;
thus a class that implements Hockey need to implement all six methods.Similarly, a class that implements Football need to define the three methods from Football and the two methods from Sports.

Extending Multiple Interfaces:
An interface is not class so Interface can extend more than one parent interface.
example:

Tagging Interfaces:
the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is:

An interface with no methods in it is referred to as a tagging interface.There are two basic design purposes of tagging interfaces
Creates a common parent:

Adds a data type to a class:

Reference:
http://www.tutorialspoint.com/java/java_interfaces.htm

 

Java Tut – Aggregation/HAS-A relationship

code:

reference:
http://www.javatpoint.com/aggregation-in-java

Java Tut – Encapsulation

Encapsulation is one of the four fundamental OOP concepts.The other three are inheritance,polymorphism and abstraction.

it is a mechanism of wrapping the data variables and code acting on the data together as single unit. In encapsulation teh variables of a class will be hidden from other classes and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
To achieve-

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to modify and view the variables values.

code:
EncapTest.java

the public setXXX() and getXXX() ethods are the access points of the instance variables of the EncapTest class.Normally these methods are referred as getters and setetrs.Therefore any class that wants to access teh variables should access them through these getters and setters.

RunEncap.java

output:

 

 

 

Java Tut – Abstraction

In OOP abstraction is a process of hiding the implementation details from the use, only the functionality will be provided to the user.In other words user will have the information on what the object does instead of how it does it.

In java abstraction is achieved using abstract classes, and interfaces.
Abstract Class:
A class which contains the abstract keyword in its declaration is known as abstract class.

– Abstract classes may or may not contain abstract methods ie. methods with out body( public void get(); )

– But if a class have at least one abstract method,then the class must be declared abstract

– If a class is declared abstract it cannot be instantiated
– To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
– If you inherit an abstract class you have to provide implementations o all abstract methods in it

Example:

and from another class

It will show error because of abstract class:

Abstract Method:

Declaring a method as abstract has two consequences:

  • The class containing it must be declared as abstract.
  • Any class inheriting the current class must either override the abstract method or declare itself as abstract.

Eventually a descendant class has to implement the abstract method; otherwise you would have a hierarchy of abstract classes that cannot be instatiated.

Suppose salary class is inherits the Employee class, then it should implement the computePay() method as shwon below:
//will add the code later

references:
http://www.tutorialspoint.com/java/java_abstraction.htm

Java Tut – Polymorphism

Polymorphism the most common use of OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic.In java all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class object.
It is important to know that only possible way to access an object is through a reference variable.A reference varibale can only be one type.Once declared, the type of a reference variable cannot be changed.

A reference variable can be declared as a class or interface type

Let us look an example

Now the Deer class is considered to be polymorphic since this has multiple inheritance.Following are true for the above example:

Virtual Methods:
In this section,I will show you how the behavious of overriden methods in java allows you to take advantage of poly

Employees.java

Salary.java

VirtualDemo.java

 

Output:

References:
http://www.tutorialspoint.com/java/java_polymorphism.htm
http://www.javatpoint.com/runtime-polymorphism-in-java

Java Tut – Method Overriding

If subclass has the same method s declared in the parent class, it is known as method overriding in java.

– Method override is used for runtime polymorphism
– Method overriding is used to provide specific implementation of a method that is already provided by its super class.

Rules –
1.Method must have same name is in the parent class
2. method must have separator as in the parent class
3. Must be IS-A relationshp

understanding the problem without method overriding:

Output:

Problem is that I have to provide a specific implementation of run() method in subclass that is why we use method overriding.

Example of method overriding:

Output:
May be I did some wrong so output is not coming

Another example :
Java method overriding example of bank

Bank.java

Test2.java

Output:

Reference:
http://www.javatpoint.com/method-overriding-in-java

Java Tut – inheritance

The classs which inherits the properties of other is known as subclass(derived class,child class etc.) and the class whose properties are inherited is known as superclass(base class,parent class).

extends keyword:
extends is the keyword used to inherit the properties of a class, below given is the syntax of extends keyword:

example:
Calculation.java

My_Calculation.java

Output:

Super keyword:
The super keyword is similar to this keyword, following are the scenarios where the super keyword is used

– it is ised to differentiate the members of superclass from the members of subclass, if they have same names,
– It is used to invoke the superclass contructor from subclass
Differentiating the members:
supe
super.method();

Super_classs.java

Sub_class.java

Output:

Invoking Superclass constructor

If you want to call a parametrized constructor of the super class, you need to use the super keyword as shown below:
super(values);

Sample code:

SuperClass.java

SubClass.java

output:

IS-A Relationship:
IS-A is a new way of saying: This object is a type of that object.Let us see how the extends keyword is used to achieve inheritance.

In object oriented terms, the following are true-
Animal is the superclass of Mammal class .
Animal is the superclass of Reptile class
Mammal and Reptile are subclass of Animal class
Dog is the subclass of both Mammal and ANimal classes
Now  if we consider the IS-A relationship,,,we can say-
-Mammal IS-A Animal
-Reptile IS-A Animal
-Dog IS-A Mammal
Hence: Dog IS-A Animal as well

Using the extends keyword the subclasses will inherit all the properties of super class except the private properties.

Now, we can ensure that Mammal is actually an Animal with the use of the instance operator.
Example:
Animal.java

Dogg.java

Output:

Since we have a good understanding of the extends keyword let us look into how the implements keyword is used to get the IS-A relationship.

Genrally the implements keyword s used with classes to inherit the properties of an inheritance.Interfaces can never be extended by a class.
Example:

The instanceof keyword:

Let us use the instanceof operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal

Output:

Though there is some error in my above code.

HAS-A Relationship
These relationships are mainly based on the usage.This determines whether a certain class HAS-A certain thing.This relationship help to reduce duplication of codes as well as bugs.

let us look into an exampel:

 

More details in the link below.
Types of inheritance:
Java Tutorial

And one thing is Multiple Inheritance is not supported in java.It is illegal below:

 


Reference:
http://www.tutorialspoint.com/java/java_inheritance.htm

 

Difference Between Method Overloading and Method Overriding in Java

Method Overloading:
1) Performed within class
2) Parameters must be different
3)Is the example of compile ime polymorphism
4)Return type can be the same or different in method overloading.But you must have to change the parameter.
Method Overriding:
1)Method overriding occurs in two classes that have IS-A(inheritance) relationship
2)Parameters must be the same
3) Is the example of run time polymorphism.
4) Return type must be the same or covariant in method overriding

Overloading Example:

Overriding Example:

 

Java Tut – Exception Handling

The exeption handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained

In here we learn
What is exception: From dictionary: Exception is a n abnormal condition.
In java, exception is an event that disrupt he flow of the program.It is an object which is thrown at runtime.

What is exception handling. Exception handling is a mechanism to handle runtimer errors such as ClassNotFound,IO,SQL,Remote etc.

The core advantage of excepion handling is to maintain the normal flow of the application.Exception normally disruts the normal flow of the application that is wh we use exception handling:
Let take the scenario:
1) Checked Exception: e.g: IOException,SQLException
The classes that extend throwable class except RuntimeException and Error are known as checked exceptions.Checked exceptiions are checked at compile time.

2} Unchecked exceptions: The classes that extend RuntimeExceptiona re known as unchecked exceptions e.g: ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException etc. Uncheked exceptions are not checked at compile time rather checked at run time.

3)Error: Error is irrecoverable e.g:OutOfMemoryError, VirtualMachineError, VirtualMachineError, AssertionError etc.

Java exception handliung keywords:

1.Try
2. catch
3. finally
4. throw
5. throws

1) Scenario where ArithemeticException occurs::

2) Scenario where NullPointerException occurs
E.g:

3) Scenario where NumberFormatException occurs:
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException

4)Scenario where ArrayIndexOutOfBoundsException occurs

If you are inserting any value in the wrong index, It would result ArrayIndexOutOfBoundsException as shown below:

throw vs throws:
Throw: You cannot throw multiple exceptions.
Throws: You can declare multiple exceptions e.g: public void method() throws IOException,SQLException etc.

Java throw example:

Java throws example:

Java throw and throws example:

for further learning you can check out this link for reference: http://www.javatpoint.com/exception-handling-in-java