Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

A simple Phone Number detector Code

Program Code:


package stringrecognizing;

/**
 *
 * @author Shawon
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String input = "01614728542,01914728542,01552354992,01863767578,01711081503";
        String[] str = input.split(",");
        for(int i=0; i<str.length; i++){
            if(str[i].startsWith("017")){
                System.out.println(str[i]+" -- This is a GP number");
            }else if(str[i].startsWith("018")){
                System.out.println(str[i]+" -- This is a Robi number");
            }else if(str[i].startsWith("019")){
                System.out.println(str[i]+" -- This is a Bangla Link number");
            }else if(str[i].startsWith("016")){
                System.out.println(str[i]+" -- This is a Airtel number");
            }else if(str[i].startsWith("015")){
                System.out.println(str[i]+" -- This is a Teletalk number");
            }else{
                System.out.println(str[i]+" -- Is not a valid Bangladeshi number");
            }
        }
    }

}

List of Some important problem for java ( Basic Practice in Java )


List of Some important problem for java


1. Write a program in Java to check if a number is even or odd in Java? (input 2 output true, input 3 : output false)


A number is called even if it is completely divisible by two and odd if it’s not completely divisible by two. For example number 4 is even number because when you do 4/2 , remainder is 0 which means 4 is completely divisible by 2. On the other hand 5 is odd number because 5/2 will result in remainder as 1.

2. Write a program in Java to find out if a number is prime in Java? (input 7: output true , input 9 : output false)
A number is called prime if it is divisible by either itself or 1. There are many algorithm to find prime numbers e.g. instead of dividing till number, division upto square root of number may be enough. Start from simplest one and than try to solve this problem with couple of different ways.

3. Write Java program to check if a number is palindrome in Java? ( 121 is palindrome, 321 is not)
A number is called a palindrome if number is equal to reverse of number e.g. 121 is palindrome because reverse of 121 is 121 itself. On the other hand 321 is not palindrome because reverse of 321 is 123 which is not equal to 321. See here for solution of  checking if a number is palindrome or not in Java.

4. How to find if a number is power of 2 in Java? (1,2, 4 power of 2, 3 is not)
This is another interesting Java programming exercise. This program can be solved using different ways e.g. using arithmetic operator or by using bit shift operator.

5. Write program to sort an integer array without using API methods?
Sorting questions are one of the integral part of programming questions. There are many sorting algorithm out there to sort any array in Java e.g. Bubble sort, Insertion sort, Selection sort or quick sort. Implementing sorting algorithm itself a good programming exercise in Java.

6. Write Java program to check if a number is Armstrong number or not? (input 153 output true,  123 output false)
An Armstrong number of 3 digit is a number for which sum of cube of its digits are equal to number e.g. 371 is an Armstrong number because 3*3*3 + 7*7*7 + 1*1*1 = 371).

7. Write a program in Java to reverse any String without using StringBuffer?
This is another classical Java programming question. You can reverse String in various way in Java but two programming technique is used to do e.g. Iteration and Recursion. Try solving this problem using Iteration by using Java’s arithmetic operator.

8. Write a program in Java to print Fibonacci series up to given number? Write  iterative version.
Fibonacci series a popular number series and very popular programming question in Java, in which number is equal to sum of previous two numbers, starting from third. Fibonacci series is also a good recursion exercise and often asked in Interviews as well. Try doing this exercise by using both Iteration e.g. loops .

9. Write a Java program to calculate factorial of an integer number ? Write iterative solution.
Calculating Factorial is also a classic recursion exercise in programming. Since factorial is a recursive function, recursion becomes natural choice to solve this problem. You just need to remember formula for calculating factorial which is for n! its n*(n-1)*…1.


10. Print following structure in Java?
This program is a good exercise for mastering loops e.g. for loop and while loop in Java. By the way you can print any character and use System.out.print() and System.out.println()) in below example input is 5,
    *
   ***
  *****
 *******
*********

Note: Solution will be updated one by one. Please stay with this site.............

Database connection code for Java Dekstop Application using SQLite manager

Database connection code for Java Desktop Application using SQLite manager


import java.sql.*;
import javax.swing.*;
public class JavaConnect {
    
    Connection conn=null;
    
    public static Connection connectorDB(){
       
        try{
            Class.forName("org.sqlite.JDBC");
            
                    Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\RunTime Error\\Desktop\\Contact\\contact_bd.sqlite");
     //              JOptionPane.showMessageDialog(null, "Connection Established");
                    
                    return conn;
                    
            } catch(Exception e){
                JOptionPane.showMessageDialog(null, e);
                  return null;
            }
      
        
    }
    
    
}

How to convert String to Number in java program

How to convert String to Number in java program 


Just use A simple Snip code:


String st=”123”;

Int i=Integer.ParseInt(st);

System.out.println(i);

A java simple source code consider all the exception

Write a Calculator class which prosses some methods named, add, multi, sub, div and reminder. Write another class named Tester in which you will prompt two inputs from user and print the results of those methods. Consider all the exception cases.

Ans:


//Write the main class name Tester and copy the code and run.
//Thanks to all




package calculator1;

import java.util.Scanner;

/**
 *
 * @author RunTime Error
 */

class Calculator{

static int add(int x, int y)     // adds x and y, then returns
{
int res;
res = x+y;
return res;

}
static int sub(int x, int y) // subtract  y from x, then returns
{
int res;
res = x-y;
return res;

}
static int multi(int x, int y) // multiplies x and y, then returns
{
int res;
res = x*y;
return res;
}
static String div(int x, int y) // divide x by y, then returns the quotient
{
float res=0;
                String st=null;
try{
                   res = x/y; 
                   st=res+"";
                } catch(Exception e){
                    System.out.println("Input Error: "+e);
                }
                
return st;
}
static String remainder(int x, int y) // divide x by y, then returns the remainder
{
           float res=0;
                String st=null;
try{
                   res = x%y; 
                   st=res+"";
                } catch(Exception e){
                    System.out.println("Input Error: "+e);
                }
                
return st;
}


}
public class Tester extends  Calculator{

    /**
     * @param args the command line arguments
     */
    
public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    System.out.println("Enter 1st Number:");
int a=input.nextInt(); 
 System.out.println("Enter 2nd Number:");
int b=input.nextInt();

//result = add(a,b);
System.out.println("The result of the Addition is : "+ add(a,b));
//result = sub(a,b);
System.out.println("The result of the Subtraction is : "+ sub(a,b));
//result = multi(a,b);
System.out.println("The result of the Multiplication is : "+ multi(a,b));
//result = div(a,b);
                String div=div(a,b);
                if(div!=null)
System.out.println("The result of the Division is : "+ div);
//result = mod(a,b);
                 String rem=remainder(a,b);
                if(rem!=null)
System.out.println("The result of the remainder is : "+ div);
                

}

}

Pattern and Matcher easy source code in java

Write a class named “find”, use Pattern, Matcher class to find “love” in the following String: “I love java”.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester
{
public static void main (String[] args)
{
String t ="I love Java";
Pattern pattern = Pattern. compile("love");
Matcher matcher = pattern. matcher(t);
if (matcher . find())
{
System. out.println("Found match!");
}
}
}

if , ifelse and elseif Operator In C++

If Operator


"If", which is used as a conditional operator. "If" keyword execute the statement only if the condition will be true.

Syntax: 
if (condition) 
statement:
Source code of "If" operator:


If-else


When if condition will be false then the statement of else condition will execute.

Syntax: 
if (condition) 
statement1
 else statement2.

Source code of "Ifelse" operator:



In above code “a=10”,”b=9” there are not equal , so “if(a==b)” condition is not true .
the output will be….

If-else-if


When  if condition will be false then it execute else. If we want to add more if condition with else,Then we have to use elseif such as:

Syntax:
if (condition)
 statement 1 
else if 
(condition) statement 2.

Source code of "If-else-if" operator:





In above code “a=10”,”b=9” there are not equal , so “if(a>b)” condition is not true .So
the output will be….


Thats everything for if, ifelse and elseif condition.


How to create android first project on eclipse???

How to create android first project on eclipse???



Step 01: Click New  in the toolbar.
                                  



Step 02: Open the Android folder, select Android Application Project, and click Next.




Step 03: Fill in the form that appears:
ü  Application Name.
ü  Project Name.
ü  Package Name.
Step 04: Minimum Required SDK is the lowest version of Android that your app supports, indicated using the API level.
Step 05: Target SDK indicates the highest version of Android (also using the API level) with which you have run with your application.
Step 06: Click Next.


Step 07: Again click Next.



Step 08: The next screen can help you create a launcher icon for your app.



Step 09: Click Next.
Step 10: For initial project, select BlankActivity and click Next.



Step 11: Then  click Finish.



Finally your android first project is opened....WoW!!! You have done it!!

Prepare Your System for Java using easy way

Prepare Your System for Java

1. Download JDK. latest version from here-->

2. Install the JDK in your PC.

3. Now check the Java Installation is correct or not:
to do this, go to command prompt by start button-->type "cmd" in the search box-->Click Enter.
then type "java" ( Ignore the "" double qoutation)
If a list is shown, the java installation is successfull.

4. Now check whether the Java Library is properly installed
    or not.
to do this, type "javac" ( Ignore the "" double qoutation)
Generally, it shows a message "javac is not recognized as an....."
This means that, Java library is not connected to OS.

 5. If you have installed JDK in default directory, it is installed in
     "C:\Program Files\Java\" directory. There should be two directories
     named "jdk" and "jre". Here, JDK is the java library to compile your program
     and JRE is the java runtime environment to deploy your program.

 6. Now go to the "jdk" folder, then "bin" folder. Copy the address from the address
     bar. The address may look alike "C:\Program Files\Java\jdk1.6.0\bin".

 7. Now, go to desktop and right click on the "MyComputer" icon.
     Select "Properties". A "System Properties" dialog box will be opened.
     Select "Advanced" tab. Then click on the "Environment Variable" button.

 8. A "Environment variable" dialog box will be opened. Under this dialog box
     There is a list of System variables, where you should check is there any
     variable named "Path" is available or not. If not,Click the "new" button.
     "New System Variable" window will be opened prompting two information:

Are you new in programming???????

Are you new in programming???????

If you are new, better will be if you start programming in C.



Who are new in programming for them its a little try to help them..

C programming tutorials collection..
        
       
            Please Click Here To Download

Android Development Environment Setup Using easy way (Step by Step)

Android Development Environment Setup Using easy way:

 Android Mobile Application Development using the ADT Eclipse Bundle is very much easy:

Step 01: Download and Install JRE and JDK

Step 02: Download and Extract Android ADT Bundle



Step 03: Opening the ADT Bundle

Step 04: Go \adt-bundle-windows-x86-20130522\eclipse and open eclipse




Step 05: Set Workspace Location -> Ok




Step 06: Write SDK path
-----WindowsàPreferenceàAndroidàSDK location

Object Oriented Programming Concept

Object Oriented Programming Concept:

Object-Oriented Programming is a methodology to design a program using.............

1.Object
2.Class
3.Inheritance
4.Polymorphism
5.Abstraction
6.Encapsulation

Object:
In real world there is everything which we see has its state and behavior is called object. Such as book, room, table etc.

Class:
Class is collection of objects.

Inheritance:
It is just like parents and child.
In real word, we know that child can access all the properties from parents.
But parents can’t access child’s properties.
This concept is basically called Inheritance. It provides code reusability.

Polymorphism:
Poly means many and morphos means forms.
Polymorphism means many forms or ways.
Java use method overloading and method overriding to achieve polymorphism.

Abstraction:
Abstraction means hiding internal activity and showing functionality.
For example: File save, we just click save but we don't know the internal processing.
Java use abstract class and interface to achieve abstraction.

Encapsulation:
Encapsulation means code and data together into a single unit.
For example: capsule, it is bonded with different medicines.
Class is the example of encapsulation.

We all know that in a class all the resource or code stay together.

Method overriding in java


Method overriding:

Method overriding means having the same method in parent class/super class and child class/sub class.
In a word, same method but different implementation.

Example:

Super class:

class Circle
{
protected double radius;
public Circle(double radius)
{
      this.radius = radius;
}
// other method definitions here
public double getArea()
{
      return Math.PI*radius*radius;
}
}// end of class circle

Sub class:

class Cylinder extends Circle
{
protected double length;
public Cylinder(double radius, double length)
{
     super(radius);
     this.length = length;
}


public double getArea()
{
     // method overriden here
     return 2*super.getArea()+2*Math.PI*radius*length;
}

}// end of class Cylinder

Method overloading in java

Method overloading:

Two or more methods with the same name but different signatures in the same class.

Example:

class EmployeeList
{
private String firstName;
private String lastName;

Employee ()
{
this.firstName = "";
this.lastName = "";
}

Employee (String FirstName)
{
this.firstName = FirstName;
this.lastName = "";
}

Employee (String FirstName, String LastName)
{
this.firstName = FirstName;
this.lastName = LastName;
}
}


Introduction To C Programming Language

In the mid 1970s at Bell Labs C, is the high-level programming language developed by Dennis Ritchie. C that kind of language which is basically used for UNIX programs. But there is a speech that “C is the mother language or heart of programming”. It is a high level programming language but it is so much closer to assembly language.
If you are first at programming, first of all you have to set up the environment to compile and run source code.

I personally prefer to use CodeBlocks for C programming language.

CodeBlocks Download From Here à