Thursday, 13 June 2013

getChars:  
getChars is used to select the more than one character using locations

// Program to use of getchars

class getChars
{
public static void main(String arr[])
{
String a="There is Shaminder singh";
int start=6;
int end=14;
char buf[]= new char[end-start];
a.getChars(start,end,buf,0);
System.out.println(buf);
}
}




Use of getChars

getChars:  
getChars is used to select the more than one character using locations

// Program to use of getchars

class getChars
{
public static void main(String arr[])
{
String a="There is Shaminder singh";
int start=6;
int end=14;
char buf[]= new char[end-start];
a.getChars(start,end,buf,0);
System.out.println(buf);
}
}




LENGTH():   length() is used to find the length of a string


// To print the string and its length.

class Length
{
public static void main(String arr[])
{
String s="getcoderesults.com";                   //For declare String literals
System.out.println(s);
System.out.println(s.length());

}}



Output:



Length Function

LENGTH():   length() is used to find the length of a string


// To print the string and its length.

class Length
{
public static void main(String arr[])
{
String s="getcoderesults.com";                   //For declare String literals
System.out.println(s);
System.out.println(s.length());

}}



Output:



This entry was posted in :
// To find the character on a given location

class ExactLoc
{
public static void main(String arr[])
{
String a="shaminder";
System.out.println(a.charAt(4));
}}






Use of charAt

// To find the character on a given location

class ExactLoc
{
public static void main(String arr[])
{
String a="shaminder";
System.out.println(a.charAt(4));
}}






This entry was posted in :
// A simple program to print the length of character.

class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}



Output:

Use of Lenth Function

// A simple program to print the length of character.

class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}



Output:
This entry was posted in :

Tuesday, 11 June 2013

String Handling provides a lot of concepts that can be performed on a string such as concatinating string, comparing string, substring etc. 

// Simple program to  print the length of character in a array

  class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}


Output:





























String Handling provides a lot of concepts that can be performed on a string such as concatinating string, comparing string, substring etc. 

// Simple program to  print the length of character in a array

  class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}


Output:





























This entry was posted in :

Sunday, 9 June 2013

Using the keyword interface, you can fully abstract a class interface from its implementations. Interfaces are similar to classes, but they lack instance variables, and their methods are declared without any body. Once, it is defined any number of classes can implements an interface. Also, one class can implement any number of interfaces.
      An interface is defined much like a class. This is the general form of an interface:
      
           access interface name
          {
           return-type method-name1(parameter-list);

            return-type method-name2(parameter-list);


             type final-varname1=value;
             
              type final-varname1=value; 
               
              }

//      A simple program to describe interface

interface Drawobject
{
 float P=3.14f;
public void Area(int x);

}
class Mcolor
{
void Setcolor(String col)
{
System.out.println("Color selected by you is  "+   col);
}
}

class circle extends Mcolor implements Drawobject
{
public void Area(int r)
{
float res=P*r*r;
System.out.println("Area of circle with r  "+   res);
}
}

class Testinterface
{
public static void main(String arg[])
{
circle obj=new circle();
obj.Setcolor("red");
obj.Area(5);
}
}


Output:




Interfaces

Using the keyword interface, you can fully abstract a class interface from its implementations. Interfaces are similar to classes, but they lack instance variables, and their methods are declared without any body. Once, it is defined any number of classes can implements an interface. Also, one class can implement any number of interfaces.
      An interface is defined much like a class. This is the general form of an interface:
      
           access interface name
          {
           return-type method-name1(parameter-list);

            return-type method-name2(parameter-list);


             type final-varname1=value;
             
              type final-varname1=value; 
               
              }

//      A simple program to describe interface

interface Drawobject
{
 float P=3.14f;
public void Area(int x);

}
class Mcolor
{
void Setcolor(String col)
{
System.out.println("Color selected by you is  "+   col);
}
}

class circle extends Mcolor implements Drawobject
{
public void Area(int r)
{
float res=P*r*r;
System.out.println("Area of circle with r  "+   res);
}
}

class Testinterface
{
public static void main(String arg[])
{
circle obj=new circle();
obj.Setcolor("red");
obj.Area(5);
}
}


Output:




Thursday, 6 June 2013

public class Zs
{
     public static void main(String args[])
     {
          short s1 = 5;
         byte b1 = (byte) s1;
          System.out.println(b1);    
}
}

To convert short into byte using casting

public class Zs
{
     public static void main(String args[])
     {
          short s1 = 5;
         byte b1 = (byte) s1;
          System.out.println(b1);    
}
}
This entry was posted in :
class Box
{
double width;
double height;
double depth;
}
class BoxDemo
{
public static void main(String arg[])
{
Box mybox=new Box();
double vol;
mybox.width=5;
mybox.height=10;
mybox.depth=15;
vol=mybox.width*mybox.height*mybox.depth;
System.out.println("Volume"+vol);
}
}

To find volume using Class

class Box
{
double width;
double height;
double depth;
}
class BoxDemo
{
public static void main(String arg[])
{
Box mybox=new Box();
double vol;
mybox.width=5;
mybox.height=10;
mybox.depth=15;
vol=mybox.width*mybox.height*mybox.depth;
System.out.println("Volume"+vol);
}
}
class Pro4
{
public static void main(String arg[])
{
int a,b,c,max;
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[1]);
c=Integer.parseInt(arg[2]);
if((a>b)&&(a>c))
{
System.out.println("a is greater");

}
else if((b>a)&&(b>c))
{
System.out.println("b is greater");
}
else
{
System.out.println("c is greater");
}
}
}

To find the Max no. among three numbers

class Pro4
{
public static void main(String arg[])
{
int a,b,c,max;
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[1]);
c=Integer.parseInt(arg[2]);
if((a>b)&&(a>c))
{
System.out.println("a is greater");

}
else if((b>a)&&(b>c))
{
System.out.println("b is greater");
}
else
{
System.out.println("c is greater");
}
}
}
class Aa
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}}

To print the * in a sequence form

class Aa
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}}

Tuesday, 4 June 2013

class Pro21
{
public static void main(String arg[])
{
int a,b,c,i,n;
a=0;
b=1;
c=0;
n=Integer.parseInt(arg[0]);
System.out.println(a);
System.out.println(b);

for(i=1;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
System.out.println(c);
}

}
}

To print the fibnocci series

class Pro21
{
public static void main(String arg[])
{
int a,b,c,i,n;
a=0;
b=1;
c=0;
n=Integer.parseInt(arg[0]);
System.out.println(a);
System.out.println(b);

for(i=1;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
System.out.println(c);
}

}
}
class Pro20
{
public static void main(String arg[])
{
int a,i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=10;i++)
{
a=n*i;                 
System.out.println(a);
}
}
}

To print the table of given no.

class Pro20
{
public static void main(String arg[])
{
int a,i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=10;i++)
{
a=n*i;                 
System.out.println(a);
}
}
}


class Pro22
{
public static void main(String arg[])
{
int fact=1,i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}

To find the factorial of given no.



class Pro22
{
public static void main(String arg[])
{
int fact=1,i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}
class Pro16
{
public static void main(String arg[])
{
int i,a=0,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a=a+i;
}
}
System.out.print("sum is "+a);
}
}

To print the sum of odd no.

class Pro16
{
public static void main(String arg[])
{
int i,a=0,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a=a+i;
}
}
System.out.print("sum is "+a);
}
}
class Pro15
{
public static void main(String arg[])
{
int i,a=0,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
a=a+i;
}
System.out.print("sum is "+a);
}
}

To print the sum of n given numbers

class Pro15
{
public static void main(String arg[])
{
int i,a=0,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
a=a+i;
}
System.out.print("sum is "+a);
}
}
class Pro14
{
public static void main(String arg[])
{
int i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
System.out.println(i);
}
}
}
}

To Print the odd no.

class Pro14
{
public static void main(String arg[])
{
int i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
System.out.println(i);
}
}
}
}
class Pro13
{
public static void main(String arg[])
{
int i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{

System.out.println(i);
}
}
}

To print the number given by user from 1 to n

class Pro13
{
public static void main(String arg[])
{
int i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{

System.out.println(i);
}
}
}
class Pro12
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if(a>=80)
{
System.out.println(" Grade A");
}
else if(a>=70&&a<80)
{
System.out.println(" Grade b");
}
else if(a>=60&&a<70)
{
System.out.println(" Grade c");
}
else if(a>=40&&a<60)
{
System.out.println(" Grade D");
}
else if(a<40)
{
System.out.println(" Sorry! You have failed, Try again Next year!");
}
}
}

To Print the grade according to Number using if-else

class Pro12
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if(a>=80)
{
System.out.println(" Grade A");
}
else if(a>=70&&a<80)
{
System.out.println(" Grade b");
}
else if(a>=60&&a<70)
{
System.out.println(" Grade c");
}
else if(a>=40&&a<60)
{
System.out.println(" Grade D");
}
else if(a<40)
{
System.out.println(" Sorry! You have failed, Try again Next year!");
}
}
}
class Pro11
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
switch(a)
{
case 1:
{
System.out.print("sunday");
break;
}
case 2:
{
System.out.print(" Monday");
break;
}
case 3:
{
System.out.print(" Tuesday");
break;
}
case 4:
{
System.out.print(" Wednesday");
break;
}
case 5:
{
System.out.print(" Thrusday");
break;
}
case 6:
{
System.out.print(" Friday");
break;
}
case 7:
{
System.out.print(" Saturday");
break;
}
}
}
}

To print the day by using switch statement

class Pro11
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
switch(a)
{
case 1:
{
System.out.print("sunday");
break;
}
case 2:
{
System.out.print(" Monday");
break;
}
case 3:
{
System.out.print(" Tuesday");
break;
}
case 4:
{
System.out.print(" Wednesday");
break;
}
case 5:
{
System.out.print(" Thrusday");
break;
}
case 6:
{
System.out.print(" Friday");
break;
}
case 7:
{
System.out.print(" Saturday");
break;
}
}
}
}
class Pro10
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if((a%3==0)&&(a%5==0)&&(a%7==0))
{
System.out.print("Numberis divisible by 3 ,5,7");
}
else
System.out.print("Number is not divisible by all of these");
}
}

To find the given no. is divisble by 3,5,7 or not

class Pro10
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if((a%3==0)&&(a%5==0)&&(a%7==0))
{
System.out.print("Numberis divisible by 3 ,5,7");
}
else
System.out.print("Number is not divisible by all of these");
}
}
class Pro8
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if(a%4==0)
{
System.out.print("leap year");
}
else
{
System.out.print("not leap year");
}
}
}

To find given year is leap or not

class Pro8
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if(a%4==0)
{
System.out.print("leap year");
}
else
{
System.out.print("not leap year");
}
}
}
class Pro7
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if(a%2==0)
{
System.out.println("No is even");
}
else
{
System.out.println("no is odd");
}
}
}

To find the no. is Even or Odd

class Pro7
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);
if(a%2==0)
{
System.out.println("No is even");
}
else
{
System.out.println("no is odd");
}
}
}
class Pro6
{
public static void main(String arg[])
{
int a,b;
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[0]);
if(a>b)
{
System.out.print("a is greater than b");
}
else
{
System.out.print("b is greater than a");
}
}
}

To find the greater no. between two numbers

class Pro6
{
public static void main(String arg[])
{
int a,b;
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[0]);
if(a>b)
{
System.out.print("a is greater than b");
}
else
{
System.out.print("b is greater than a");
}
}
}
class Pro5
{
public static void main(String arg[])
{
double p,r,area;
p=3.14;
r=Integer.parseInt(arg[0]);
area=p*r*r;
System.out.print("Area of circle is"+area);
}
}

To find the area of circle by give the value by user

class Pro5
{
public static void main(String arg[])
{
double p,r,area;
p=3.14;
r=Integer.parseInt(arg[0]);
area=p*r*r;
System.out.print("Area of circle is"+area);
}
}
class Pro4
{
public static void main(String arg[])
{
double p,r,area;
r=1;
p=3.14;
area=p*r*r;
System.out.println("Area of circle is"+ area);
}
}

To find the area of circle statically

class Pro4
{
public static void main(String arg[])
{
double p,r,area;
r=1;
p=3.14;
area=p*r*r;
System.out.println("Area of circle is"+ area);
}
}
class Pro3
{
public static void main(String arg[])
{
int a,sq;
a=10;
sq=a*a;
System.out.println("Square of two number is"+sq);
}
}

To find the square of No.

class Pro3
{
public static void main(String arg[])
{
int a,sq;
a=10;
sq=a*a;
System.out.println("Square of two number is"+sq);
}
}
class Pro2
{
public static void main(String arg[])
{
int a,b,sum;
a=10;
b=4;
sum=a+b;
System.out.println("sum of two number is "+sum);
}
}

To find the sum of two Numbers.

class Pro2
{
public static void main(String arg[])
{
int a,b,sum;
a=10;
b=4;
sum=a+b;
System.out.println("sum of two number is "+sum);
}
}
class Pro22
{
public static void main(String arg[])
{
int fact=1,i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}

To find the factorial of a given number

class Pro22
{
public static void main(String arg[])
{
int fact=1,i,n;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}

Saturday, 1 June 2013

Things you need to know about Java


1. Object-Oriented : The first thing that you need to know about Java is that it is an object-oriented programming language. This means that Java allows you to create objects and to work with them. Some of the advantages of object-oriented programming languages are inheritance and encapsulation. Don't worry if you don't understand these terms. You will learn more about them later.

2. Platform-indepedent : Java is a platform-independent programming language unlike C and C++. This means a Java program written in your system that runs on Windows 7 can be executed in another platform (say Mac). This is why people offer refer Java as "Write once, Run Anywhere".

3. Java is secure : Unlike C, Java does not use pointers. It means, Java does not usually deal with memory storage and this is why Java is said to be more secure. Data from one object cannot be accessed by another object without your permission.

4. Java is both compiled and interpreted : Java is first compiled and the source code is converted into Byte-code which is a language that can be understood by a Java Virtual Machine (JVM). Then, JVM interprets the byte-code into the machine language of the computer in which the JVM runs. This is why, Java is referred to be a programming language that is both compiled and interpreted. However, you can compile the byte-code into the machine language using the Just-in-Time compiler, though, it is not the case most of the time.


Java Introduction

Things you need to know about Java


1. Object-Oriented : The first thing that you need to know about Java is that it is an object-oriented programming language. This means that Java allows you to create objects and to work with them. Some of the advantages of object-oriented programming languages are inheritance and encapsulation. Don't worry if you don't understand these terms. You will learn more about them later.

2. Platform-indepedent : Java is a platform-independent programming language unlike C and C++. This means a Java program written in your system that runs on Windows 7 can be executed in another platform (say Mac). This is why people offer refer Java as "Write once, Run Anywhere".

3. Java is secure : Unlike C, Java does not use pointers. It means, Java does not usually deal with memory storage and this is why Java is said to be more secure. Data from one object cannot be accessed by another object without your permission.

4. Java is both compiled and interpreted : Java is first compiled and the source code is converted into Byte-code which is a language that can be understood by a Java Virtual Machine (JVM). Then, JVM interprets the byte-code into the machine language of the computer in which the JVM runs. This is why, Java is referred to be a programming language that is both compiled and interpreted. However, you can compile the byte-code into the machine language using the Just-in-Time compiler, though, it is not the case most of the time.


This entry was posted in :