9/21/2018

How to Achieve Abstraction in Java Programming

By following the below method you can easily achieve Abstraction in Java. In Java programming, a class defined with the abstract keyword is said to be an abstract class. It can use both abstract and non-abstract methods within the body. If you have just marked your presence in the developing zone, you can join the Java Certification Course which will help you to understand the core basics of Java programming with real-world application development. Before jumping directly to the abstract class of Java, let’s explore the abstraction process first. Let discuss how we to achieve Abstraction in Java language.

abstraction in oop java

What is the Abstraction exactly? 


In abstraction, we only show functionalities to a user by hiding the implementation and other details. Or, you can say displaying only useful information and hiding internal details. Let’s understand this with the help of an example, On your mobile phone, you set alarms without even knowing its internal functionalities like how it works.

Abstraction allows you to concentrate on objects work rather than explaining how it does.

Ways to Achieve to Abstraction in Java:


In Java, Abstraction can be achieved in two ways:

  1. By using abstract class
  2. By using interface

The interface helps you to achieve 100 percent abstraction.

Abstract Class: 

An abstract class is declared with the abstract keyword. It can leverage both abstract and non-abstract methods in it. An abstract class is extended and all its methods are implemented. Always remember a few rules before defining an abstract class:

  1. It must be declared by abstract keyword. 
  2. It comprised of both abstract and non-abstract methods. 
  3. It can include constructors as well as static methods. 
  4. It includes final methods which will not allow subclasses to alter the body methods. 

Declaration: 

abstract class X { }

Abstract Methods: 

The methods which are defined with the abstract keyword without any implementation are called as the abstract method. E.g.,

abstract void sayName(); //without method body

Real-world Example of Abstract Class:


Let’s understand the concept of an Abstract class and methods with the help of an example. Here I’m defining an abstract class named office with abstract method salary. The implementation of office class is provided by the emp and othworkers classes.

For end users- these implementation classes will be hidden. The factory mehtod (it returns the class instance) will provide the implementation class object. In this program, on creating the othworkers class instance, salary() method will be invoked.

abstract class office
{
abstract void salary();
}
class emp extends office{
void salary() {
System.out.println(“salaried employees”); }
class othworkers extends office{
void salary() {
System.out.println(“salaried workers”);
}
class AbstractionCheck
{
public static void main(String args[]){
office w=new otheworkers(); //object provided through method
w.salary();
}
}

In the above example the implementation will be unknown for end users and the methods will be called by user. The output of the program will be- salaried workers.

Another Example of Abstraction Achieved by Interface: 


Implementation of the interface can also be achieved by the abstract class. All the methods are overridden in such scenarios.

interface X{
void p();
void q();
void r();
}

abstract class Y implements X{

public void r(){
System.out.println(“Hello Its r”);
}

class S extends Y{
public void p(){
System.out.println(“Hi its p”);
}
public void q(){
System.out.println(“Hi its q”);
}
public void r(){
System.out.println(“Hi its r”);
}

class Check{
public static void main(String args[]){
X obj=new S();
obj.p();
obj.q();
obj.r();
}
}

Output#

Hi its p
Hi its q
Hi its r

Thus, we can see that abstraction can be achieved with the help of abstract classes as well as the interface. With abstraction class, you can create transparency of 0 to 100 percent while interface allows you to achieve 100% abstraction. 
Read More »

9/08/2018

Data Types in JavaScript with Examples

Primitive Data Types in JavaScript with Examples and full code in JS. When we talk about any language like C, C++, and Java, the most basic and most important characteristics of that is the set of data types of that language. Javascript support maximum 6 types of primitive data types which are Boolean, Null, Undefined, Number, String, and Symbol (new in ECMAScript 6), But we are focusing only 3 types of mainly using data types in javascript. JavaScript mainly focus on three primitive data types are–

  1. Numbers
  2. Strings
  3. Boolean

So What we are going to learn in this article, Here is a list of a content that we are going to learn in this Javascript datatypes article.

Index of Content


  • Picture of an Article
  • The concept of Data Types in JS
    1. Dynamic
    2. when number values added to a string
  • Data types in Javascript
    1. Numbers
    2. Strings
    3. Boolean
    4. Null
    5. Undefined
  • Examples of Data Types in Javascript
  • Javascript Array
  • Javascript objects
  • The typeof Operator
  • About Author

Javascript Data types with list of datatypes used in JS with Logo

Here, JavaScript also supports composite data type, is an object. An object is a collection of values like numbers, string, objects. JavaScript support dual nature of objects:

  1. JavaScript Object can represent an unordered collection of named values.
  2. And ordered a collection of numbered values. Like values are indexed with numeric values.

JavaScript Concept of Data Types:


Before working with data types one should know the behavior of Data Types. We assume that you already know the How statement Execute in JS.

i) Data types in JavaScript are Dynamic:

Data Types in JavaScript are dynamic means we define a variable once and that can hold different data type values. Like if we have a variable x then it can contain a numeric value and also a string value.

Example:

<!DOCTYPE html>
<html>
<body>
<p>Dynamic data types</p>
<p id="DynamicData"></p>


<script>
var x;             
x = 5;             
x = "Programming With Basics"; 
document.getElementById("DynamicData").innerHTML = x;
</script>
</body>
</html>

Here we define x and provide an integer value and again the same variable is provided string value. Now the value of x is string value then the result is “Programming With Basics”.

ii) when number values added to a string:

When a numeric value added to the string. JavaScript has a facility to handle this situation. Automatically it assumes int value is a string and append it with a string value.

Example:

<!DOCTYPE html>
<html>
<body>
<p>adding number and string</p>
<p id="DataTypeDemo"></p>
<script>
var AddNumAndString = 26 + "Programming with basics";
document.getElementById("DataTypeDemo").innerHTML = AddNumAndString;
</script>

</body>
</html>Now we see it didn’t gives error. Result will be 26Programming with basics.

Let’s define Data Types in Brief:

1. Strings Datatypes in JS:


The string is a series or collection of Characters.

Ex: “Programming with basics”

Also, the string can be defined in single quotes. We can use a string with conditions statements for example If, If-Else, and Switch statements.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Strings in JavaScript</h2>
<p id="StringsInJS"></p>
<script>
var OurSite = "Programming With Basics";
var OurTutorial = 'DataTypes';
document.getElementById("StringsInJS").innerHTML =OurSite + ":" + OurTutorial;
</script>
</body>
</html>

2. Numbers in Javascript:


The numeric data is all digit values including decimal values, like 29, 105.98 etc. In JavaScript there are no integer or floating point values, JavaScript treats all numbers as floating point values.

Example:

<!DOCTYPE html>
<html>
<body>
<h>Numbers</h>
<p id="Numbers"></p>
<script>
var a = 10;
var b = 11;
var c = 12;
document.getElementById("Numbers").innerHTML =a + "<br>" + b + "<br>" + c;
</script>
</body>
</html>

3. JavaScript Booleans:


Boolean data types have two values only, either true or false.

Ex:  var x=true;
        var x=false;

Example:

<!DOCTYPE html>
<html>
<body>
<h>Boolean Demo</h>
<p id="BooleanDemo"></p>
<script>
var a = 10;
var b = 10;
var c = 15;
var x;
if(a==b||b==c)
{
x=true;
}
else
{
x=false;
}
document.getElementById("BooleanDemo").innerHTML =x;
</script>
</body>
</html

JavaScript Arrays:

JavaScript arrays are defined within square brackets, and the items within brackets are separated by a comma. As we defined earlier array based on numeric values, its index starts from 0.

Ex: We defined an array of all we have in our blog:
Var OurSite = [“C”, ”C++”, ”Java”, ”JS”, ”Hackerrank”];
Here C is at 0 indexes, C++ at index 1 and so on.

Example:

<!DOCTYPE html>
<html>
<body>
<h>Arrays in JavaScript</h>
<p id="Array"></p>
<script>
var oursite = ["C", "C++", "JavaScript", "Java"];
var text = "";
var i;
for (i = 0; i < oursite.length; i++) {
    text += oursite[i] + "<br>";
}
document.getElementById("Array").innerHTML = text;
</script>
</body>
</html>

JavaScript Objects:

Objects are key: value pairs, separated by a comma, are written inside curly brackets.

Ex:  var book = {Name:“JavaScript”, Pages:500, Price=400 }

Example:

<!DOCTYPE html>
<html>
<body>
<h>JavaScript Objects</h>
<p id="demo"></p>
<script>
var Book = {
    Name : "John",   
    Price  : 500,
    Pages : 400 
};
document.getElementById("demo").innerHTML =
Book.Name + " has " + Book.Price + " with no of pages" + Book.Pages;
</script>
</body>
</html>

4. Undefined in Javascript:


A variable without a value, has value undefined.
<!DOCTYPE html>
<html>
<body>
<h>Undefined Data Types</h>
<p id="undefineddemo"></p>
<script>
var x;
document.getElementById("undefineddemo").innerHTML =x;
</script>
</body>
</html>

5. Null in Javascript:


Null means no value. If we want to assign a null value to a variable.
Var a = null;

Know More: List of All Events in JS

The typeof Operator

typeof operator finds the data type of a variable.

Example:

<!DOCTYPE html>
<html>
<body>
<h> typeof operator <h>

<p id="demo"></p>

<script>
var x= 5;
var y ="PWB"
var z=null;
var k=undefined ;
document.getElementById("demo").innerHTML =
typeof x + "<br>" +
typeof y + "<br>" +
typeof z + "<br>" +
typeof k;
</script>
</body>
</html>

Result :

number
string
object
undefined

Here we observe that typeof returns number for a numeric variable, string for a string variable. Undefined for undefined value variable. But returned Object for null type, this will be a bug of JavaScript. The only difference between null and undefined is their data types, either their working behavior is the same.

Read More: For Loop in Javascript.

About Author


Data types in Javascript, this in-depth article is written by Suman Yadav.  She already wrote an article on Functions, What are a Functions in JS. You can also write to us, Submit your article and be a member of our A Programming Solutions Blog. Join us by sending a well-written article for more information visit Write for Us.
Read More »

8/31/2018

What Are Different Types of Sorting Used in C Programming?

There is a list of 5 types of sorting techniques used in C programming Language. In programming, sorting of elements is highly effective to come up with arranged data. Different types of sorting used in c programming. It makes searching a data fast and easy along with saving time for a programmer. The two important types of sorting include internal sorting and external sorting. Main memory is used to store the data associated with these sortings. Here, a proper selection of an algorithm is effective to minimize the CPU cycle and save the time and effort in writing a C program. Don’t just look for bubble sort in C with an example, to embrace a simple solution. There are other forms of sorting algorithms helpful in programming. Here we are going to a list of different type of sorting in C language.

Index of Content



  • Picture of sorting in C Language
  • Selection Sort
  • Program for Selection Sort
  • Bubble Sort
  • Bubble sort GIF
  • Program for Bubble Sort
  • Insertion Sort
  • Program for Insertion Sort
  • Merge Sort
  • Program for Merge Sort
  • Quick Sort
  • Program for Quick Sort


List of Different Types of Sorting Used in C Language

C programming includes following common sorting algorithms for effective data management:

Selection Sort in C


It starts with finding the minimum or maximum data in a list. It is compared with each element in the list and swapped with the one where the selected minimum has to be placed. Every time after the right placement, the algorithm again finds a new minimum out of the remaining data and repeats the process to come up with a sorted list in the end.

Example: Selection Sort Program in C Language with an explanation

Bubble Sort in C


Two adjacent elements in a list are compared and then arranged in a sorted way with the minimum data above. Then, in the next pass, the process is performed on the next pair apart from the sorted data. All out-of-order elements are sorted using.

How Bubble Sort works GIF image of bubble sort in C Language


Example: Bubble Sort Program in C Language with an explanation

Insertion Sort in C


In this algorithm, you start with the first element and then traverse through every element in an array. In the process, if an element is found out-of-order, it is inserted in the right position.

Example: Insertion Sort Program in C Language with an explanation

Merge Sort in C


One of the popular algorithms for sorting, Merge sort follows a recursive pattern to come with a sorted array. The given list is split into two halves and repeatedly invokes merge sort on elements. After all sorted elements, it is merged to form a final array.

Example: Merge Sort Program in C Language with an explanation

Quick Sort in C


Here you need to select an element (pivot) from a list, for example, the last element. Compare and place smaller elements before and higher elements after that pivot. Now repeat the process with the sections containing the smaller and higher elements to sort them out one by one. In the end, you will get a sorted group of data.

Example: Quick Sort  Program in C Language with an explanation

Most of the sorting algorithms compare and place data at its right place. Above mentioned sorting algorithms help C programmers to come up with a sorted list of data. A sorted data offers simplified functions and operations of data in a C program.
Read More »

8/01/2018

Javascript Events Tutorial with Complete List of Events

Events in JavaScript, When User or browser tries to manipulate pages then events occurred to handle the JavaScript’s interaction with HTML. As we know, JavaScript works with HTML, So, Everything that happens with pages loading, clicking a button, minimizing the window, click to mouse etc. all are the events. Like if we display any message to the user on a button click, this happens through the events. there are many things you need to study before reading javascript Events. You must know about Functions in Javascript.

All HTML elements like button, textbox, images can contain events that can be triggered using JavaScript code. And all these events are the part of DOM(Document Object Model). Here we will discuss some most frequent Events used in JavaScript:

Table of Content


  • Events in Javascript
  • Event list in JS Graphical 
  • Onclick Event in Javascript
  • OnMouseOver in Javascript
  • Example of Events
  • List of Event in Javascript
  • Keyboard Event operation perform
  • Event Listeners Flow
  • Author

Mouse and Keyboard Javascript Event Listeners list

Onclick Event


OnClick Event occurs when a left mouse button is clicked, Like if you want to show any message or warning or data validation or something on button click you can do using Onclick event. As we display a message box in the example:

Example

<html>
   <head>   
      <script type="text/javascript">     
            function Welcome() {
               alert("Welcome to Programming With Basics")
            }
      </script>   
   </head>
   <body>
      <form>
        <input type="button" onclick="Welcome()" value="Welcome to PWB"/>
      </form>   
   </body>
</html>

OnMouseOver :


Onmouseover event triggers when the mouse comes over the element contains the mouseover event. For example, if you want to highlight or want to change the color of the text when the mouse comes over the element then this event is used.

Example:

<html>
   <head>
      <script type="text/javascript">     
            function over() {
            var x = document.getElementById("div1");
            x.style.color = 'red';
            }     
      </script>   
   </head>
   <body>
      <div id="div1" onmouseover="mouseover()">
         <h2> Mouse Over </h2>
      </div>     
   </body>
</html>

Read: How Controls Flow Works in Javascript.

Keyboard Event and Mouse Events: You can perform keyboard events test bt visiting a https://www.w3.org/2002/09/tests/keys-cancel2.html or you can also check how many keyboard or mouse events are active in a webpage for more information check below picture.

javascript Keyboard listener properties

Press Ctrl + Shift + I or right click of any webpage click to the event listener, you will see a list of event listeners available for the web page. See the event listener in hacker rank programming solutions for day 8.

hackerrank solution event listeners list on browser

Below is the list of above-defined events, there are many more events in JavaScript:



EventDescription
onclickTriggers on mouse click.
OfflineTriggers when the document goes offline
onblurTriggers when the window loses focus
oncanplayTriggers when media can start play, but might has to stop for buffering
OnabortTriggers on an abort event
onafterprintTriggers after the document is printed
onbeforeprintTriggers before the document is printed
oncanplayTriggers when media can start play, but might has to stop for buffering
onbeforeonloadTriggers before the document loads
oncanplaythroughTriggers when media can be played to the end, without stopping for buffering
oncontextmenuTriggers when a context menu is triggered
onclickTriggers on a mouse click
onchangeTriggers when an element changes
ondblclickTriggers on a mouse double-click
ondragoverTriggers at the start of a drag operation
ondragleaveTriggers when an element is being dragged over a valid drop target
ondragenterTriggers when an element has been dragged to a valid drop target
ondragendTriggers at the end of a drag operation
ondragTriggers when an element is dragged
onendedTriggers when media has reach the end
onemptiedTriggers when a media resource element suddenly becomes empty.
ondurationchangeTriggers when the length of the media is changed
ondragstartTriggers at the start of a drag operation
ondropTriggers when dragged element is being dropped
onerrorTriggers when an error occur
onformchangeTriggers when a form changes
onfocusTriggers when the window gets focus
onforminputTriggers when a form gets user input
oninvalidTriggers when an element is invalid
oninputTriggers when an element gets user input
onhaschangeTriggers when the document has change
onkeypressTriggers when a key is pressed and released
oninvalidTriggers when an element is invalid
onkeydownTriggers when a key is pressed
onloadeddataTriggers when media data is loaded
onkeydownTriggers when a key is pressed
onkeyupTriggers when a key is released
onloadeddataTriggers when media data is loaded
onmouseoutTriggers when the mouse pointer moves out of an element
onmousedownTriggers when a mouse button is pressed
onmousemoveTriggers when the mouse pointer moves
onmessageTriggers when the message is triggered
onloadstartTriggers when the browser starts to load the media data
onmouseupTriggers when a mouse button is released
onplayingTriggers when media data has start playing
onmousewheelTriggers when the mouse wheel is being rotated
onoineTriggers when the document comes online
onofflineTriggers when the document goes offline
onpagehideTriggers when the window is hidden
onpageshowTriggers when the window becomes visible
onpauseTriggers when media data is paused
onplayingTriggers when media data has start playing
onplayTriggers when media data is going to start playing
onpopstateTriggers when the window's history changes
onratechangeTriggers when the media data's playing rate has changed
onprogressTriggers when the browser is fetching the media data
onreadystatechangeTriggers when the ready-state changes
onstorageTriggers when a document loads
onstalledTriggers when there is an error in fetching media data
onseekingTriggers when a media element's seeking attribute is true, and the seeking has begun
onscrollTriggers when an element's scrollbar is being scrolled
onseeked
Triggers when a media element's seeking attribute is no longer true, and the seeking has ended
onresizeTriggers when the window is resized
onredoTriggers when the document performs a redo
onsuspend
Triggers when the browser has been fetching media data, but stopped before the entire media file was fetched
onundoTriggers when a document performs an undo
onwaitingTriggers when media has stopped playing, but is expected to resume
ontimeupdateTriggers when media changes its playing position
onvolumechangeTriggers when media changes the volume, also when the volume is set to mute

Event Listeners Flow:


Check the Event Listener Propagations, How Events work when opening a webpage when we click on a button, link etc.

Flow of Mouse Event Listener in Javascript

Author


This in-depth article of Event in JavaScript is written by Suman Yadav.  You can also write to us, Submit your article and be a member of our A Programming Solutions Blog. Join us by sending a well-written article for more information visit Write for Us.
Read More »