Saturday, 28 October 2017

Binary Search 


Let's understand without going into the details.
Let's say we have a sorted array, say in ascending order like:
1, 4, 10, 13, 40, 65, 100.

Now let's say I searched for 10. The program will execute and it will look at the middle term and will compare it with our number. If the match is found then it will show the position of array. And if it is smaller than the middle term then it will look for the numbers before the middle term, and this will go on until the number is found. If not found, then we will display: "Not found".

Program:


void main()
{
clrscr();
int n, i, arr[20], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
getch();
}

Monday, 11 September 2017

Linear Search. 



One of the easiest method for searching the element in an array.

Structure:

int Array[10], x; //x is variable for search 
for (int i =0 ; i<10; i++) //Initalize, condition, increment 
{
cin>>Array[i];

cout<<"Enter the element to Search: ";
cin>>x;
for (int i =0 ; i<10; i++) //Initalize, condition, increment 
{
if(Array[i] == x){ cout<<"Number found at position" <<i+1;  break;}
}

Tuesday, 25 July 2017

Handling the Patterns in C++


___________________________________________________________
These are often asked and the funny thing is we still don't pay much attention to this.
___________________________________________________________
Consider the Example:
*
**
***
****

Now , for a beginner it's a little bit challenging.

There are two ways to do this:
1. Just print everything with "COUT"
2. Using Loops(Your Logic)
________________________________________________________

The first can be done by anybody so lets start with second One.

for(int i=  0; i<=5 ;i++)
{
for(int j=0; j<i; j++)
{
cout<<"*";
}
cout<<"\n";
}
________________________________________________________

Explanation: 

This works from outer to inner. When i was 0, condition was true, loop goes inside, now j=0 and second condition is false so the loop comes outside and prints "\n"(A new Line).
Now the value of i=1, condition is true, loop goes inside, j=0, and the condition is true, so inner loop runs and prints "*".
Now the inner loop runs one more time and the j becomes 1 and now the condition is false, so inner loop terminates. back to the outer loop.
Like this the loop will go on until the i becomes greater than 5 and you will obtain an output. 

Tuesday, 20 June 2017

Easiest way to use onClick method in Android. 

It can be done by using the XML. Just define a Button with 'id' and and 'onClick'.

Like: 
<Button 
android: height = "wrap_content"
android: height = "wrap_content"
android: text = "Click it"
android: id = "@id/click_button_1"
android: onClick = "Do_the_Click"
/>

So when you will define the onClick, there will be a suggestion on the left side of the statement. Click on that, it would suggest to make a method in java file with name "Do_the_Click", Click on it and it will create it.

And then you can write your code in that method. 

Sunday, 11 June 2017

How to import or use any defined thing in the MainActivity.java File?

The simple Answer is from Resources.
Now, What are resources and how to use Resources?

The Most simple and Standard way is to create a Variable and use that to get the other resources from other Files like Strings.xml.
_______________________________________________________________________________
Consider that had defined a String-Array in Strings.xml file and want to import that array into you ListView.

Step 1. Create a Listview in MainActivty.xml(Your main Layout file) and define ID.

Step 2. Define you string -array in Strings.xml file(It's located in Values directory) and give a name say "Languages".

Step 3. Go to MainActivity.java file and type following code

ListView list = (ListView) findViewById(R.id.list_items);

Resources resources = getResources();
String[] languages = resources.getStringArray(R.array.Languages);

ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.Simple_list_1, languages); //I don't exactly know the ListView drop-down options
listview.setAdapter(adapter);

Step 4.  Run the Emulator.

Saturday, 27 May 2017

BOXING and UNBOXING (Java)

---------------------------------------------------------------------------------------------------------------------
Well it's not what you see on the YouTube rather than its a Topic when you read about the Wrapper Classes.

Now What are Wrapper Classes?
So the Answer is simple, you have heard about the Primitive data types. So during development it was difficult to use the Primitive data types because if they had used Primitive data types then there was a possibility that their files would have altered the data. This created a need for Objects to use instead of the directly using them.
Now, the Objects accept the properties of the primitive data types. this is why we need Wrapper classes. 
Wrapper classes are the Subclass of the Number class. 
Diagram of Number Class and its subclasses
_______________________________________________________________________________

And now comes the Boxing and Unboxing.

So, Converting primitive data types into object is called boxing and the vice versa is called Unboxing. They are done by Compiler so the User doesn't have to do much.

Tuesday, 16 May 2017

What will be the next Update in Android after Nougat?


Nougat was released on 9 March 2016(The alpha version) and then officially on August 22, 2016. We know some of the important features that we got from this Update.
 Whether, it's Split Screen or Notifications redesigning(You can also set the priorities of the Notifications).

So the Next 'Android O' will be coming soon. And there are many guessing about the name going on with "Oreo, Orange, etc." It's pretty much possible that the name will be Android Oreo.

But the name doesn't matter as the user will judge it on the basis of its performance and appearance. We already know some of the features like the picture-in-picture multitasking and some new emoji s introduced.

Updates available for the Device:

  • Nexus 5P, Nexus 6P, Nexus Player
  • Pixel C, Pixel, Pixel XL

It's Beta version is out there and you can download it from Google but it's going through a developing stage so it will contain Bugs for sure. So download at your own risk. It will be officially released between August to September 2017.


Thursday, 11 May 2017

New Feature of Java

Multi- resolution Image API

How to include: import java.awt.image;

Use: Basically image package was used for creating and modifying images. (Some classes: BufferdImage, ColorModel, etc..)

MultiResolutionImage encapsulates a set of images of different resolutions(many images of different height and width in pixels) and then allows us to modify them according to our requirements.


Monday, 24 April 2017

New Update of JAVA 

Java 9 will be coming this Year and we are much excited(Talking about the Programmers here). So one of the best feature that I found is :
Jshell

So what is Jshell?
Let's say that you have to run a few lines of Code, like adding two numbers. So, for that you have to write all that code for that small work. But the jshell is an extra feature will be added in the new Update so you don't have to install it separately. This will really save Time.

What's your favorite Feature?Comment

Friday, 21 April 2017

Why is Java more safe than C++?

It's one of the questions we think everytime, whenever we are asked about it. So, the simple answer that is due to pointers.
Now how is this this the shortest Answer? Think about it that what pointers do? They create loop holes in a language.
But How?
A  pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address.(This definition is from Wikipedia).

Now My own definition: A pointer is an object which points to the value and address of the other object in the Program. We can change the value of that variable through pointer.(Can even find out the address).

So by having the ability to point to the other variable it decreases the security level in Programming Languages.  So Java doesn't have Pointers. That's how it becomes More safe because everything is running inside the JVM. 

Friday, 7 April 2017

Does Java can have more than on main method?

Well, depending on the type of the question I can say that yes we can define more than one main method in a Java Program.
But the difference will come when you will give the name to your Program, then the class having that name should have at-least one main method.

Example:

class Test1{

public static void main(String[] args)
{
System.out.println("This is Test 1");
}
}

class Test2{

public static void main(String[] args)
{
System.out.println("This is Test 2");
}
}

Explanation: 
Now at the end of the Program, you have to just save it with the class name(Let's say Test1 in above code), which you want to be executed first.
Like: Test1.java

Tuesday, 4 April 2017

Is there "goto" in Java?

Yes and No, I think both will be correct But the absolute answer is "Yes, there is goto in Java".(Not for usage but it is present and reserved). Now you might be wondering, How?

Let's understand this with an Example:

import java.io.*;
import java.util.*;
import java.lang.*;

class Test6{

public static void main(String[] args) throws IOException {

starting_point:

for (int i=1;i<10 ;i++ ) {

System.out.println("Now the Value of Number "+i);

if (i=9) {
goto starting_point;

}

}

}


Explanation: 

Now on Compiling this errors occurs,

and that is Because the goto is reserved keyword. True the goto is there  but it is not available to be used in the Program. The reason is that if in case it were to be added to a later version of Java and if the Programmer had used that word  in his Program, then the Program would break. So the Program will not even execute.

There is another option instead of using goto, you can use labels(with break).

Thursday, 30 March 2017

Out of While and do-while , which is more efficient?


We often think that all the loops are same because we can use them almost everywhere and wherever we like in the Program but to become an efficient programmer we need to think where to use which loop so that our Programming can become efficient(More useful).

Explanation: 

We know the while loop first test the condition then executes the Statements, and do-while first executes the statement then checks the test condition. So any Programmer would like to have control on it's program from the very start, so the while loop is more efficient.

Another way of thinking this would be using the do-while loop because it will run at-least on time to show the first statement(Even when the condition is wrong), So user will get atleast some Output. While on the other hand the while loop will not show anything.

Wednesday, 29 March 2017

If Strings are immutable then what the other way to work on "strings"?


So we have studied that Strings are immutable so we have got StringBuffer class which represents strings in such a way that we can manipulate data in it(I am talking about Object).

Creating a StringBuffer reference object is same like creating any new Object of any class.

Example:
StringBuffer stringBuf = new StringBuffer("abc");

or 

StringBuffer stringBuf = new StringBuffer();
stringBuf.append("abc");

Explanation:
This will create an object "abc" in String Constant Pool,  and the reference variable will be stringBuf.

Monday, 27 March 2017

Creating a Simple Server- Client Connection in JAVA


The Client can connect to the Server with the same Socket by which the Server is Connected. Now the Server and Client can Communicate by reading and writing from the Socket. Communication is done by TCP(Transmission Control Protocol).

Example:

impot java.io.*;
//First the Server side

class ServerSide{
public static void main (String args[])
    {
       ServerSocket serverSocket = new ServerSocket(Port_number);
       System.out.println("SERVER STARTED");
       Socket s = ss.accept();
       System.out.println("CLIENT CONNECTED");
    }
}

//Now the Client Side

import java.net.*;

class Client {

public static void main(Sring args[])
    {
      Socket s= new Socket("localhost", Port_number);
      //Port no.should be same as the Server Port Number.
    }
 
}

Explanation:
So, the Client will connect to the Server Only when the Client class will execute. And One more thing make different java files for both. One for Client and one for Server.

Wednesday, 22 March 2017


How Strings are Immutable?

Let's Understand this with an Example.

Example:

String s1="abc";
String s2="abc";

s1="bcd";

System.out.print(s1);
System.out.print(s2);

/*Both Strings were referring to same object so if the reference variable
is changed then it should also change the String "s2". So let's take a look at the Output. */

Output:
bcd
abc


Explanation
So from the Above Example can we say that String are not Immutable? Well on the Outside it looks like the String is actually changed. But on the Inside, A Object is being Created(for "bcd") and s1 is assigned to it. Now both s1 and s2 are referring to different objects.

Wednesday, 15 March 2017

Now Time to Answer my First  Challenge Question. So I asked how many objects were created? 

Code: 
//Consider that a class is already created. 

String s = new String("abc");
String s1="abc";
String s2= new String("abc");

Now the Answer:
Total Objects will be 3.

Now the reason, How?
A. So First line of code has "new String". This will create an Object in the Heap Memory. But then the JVM will realise that it is a String, so it will create another Object in String Constant Pool and then the Garbage Collector will remove the Object that was created in Heap and "s" will be assigned to String Constant Pool Object. 

Now the second Statement.
It will refer to the same Object like "s" string. 

Now the third Statement.
It will again create a new Object of "s2" in Heap. But then it will create another object in String Constant Pool(But the Object "abc" is already created so it will also refer to it and no new Object will be Created in String Constant Pool). and then Garbage Collector will remove the Object present in Heap. 

Now You Know the Solution. Q& A's are All Welcomed.

Tuesday, 14 March 2017

Solve(JAVA CODE)

Code:
//Consider that a class is already created.
String name = null;
String s="";

How Many Objects are Created in the Above Code, and if object is created then what will be the Length of the Object Created?

Let's see who can answer this One.(JAVA CODE).


Code: 
//Consider that a class is already created. 

String s = new String("abc");
String s1="abc";
String s2= new String("abc");

How Many Objects are created in the above program(Total number of Objects)? Comment your Answer Below.

Saturday, 11 February 2017

INHERITANCE  


Inheritance is basically nothing but property of One Class to inherit properties from another Class.
So, the question arises that how we can do that?
The answer is simple that when you define a Class(i.e. when you make a Class) and then you create another class which can also can access some parts of the Base class then it is considered as Inheritance.

Syntax: 
class base class-name{
};

class derived class-name : visibility mode base class-name{


}
Visibility mode can be private, protected or even public. it means which part the derived class can access from the Base Class(i.e. either private or protected or public).

Let's take an Example to understand it better.
Example: 

class Base{

//Let's assume that all functions are defined
} ;

class Subclass : public Base
{
};

Now the Subclass will be able to access all the Public members of the Class Base(Either Variables or Functions). If it was protected then the Sub class would be able to access the protected members of Class base.

I will post the next topic soon.If you have any questions you can ask in the Comments.
Thanks.