follow

Saturday, 27 July 2013

String builder class : JAVA Programming by "Gopal Krishna"

public class bld
{
public static void main(String arg[])
{
 StringBuilder sb=new StringBuilder("hello");
 System.out.println(sb.append("hi"));
 System.out.println(sb.insert(2,"ram"));
 System.out.println(sb.replace(2,7,"ram"));
 System.out.println(sb.reverse());
 System.out.println(sb.delete(2,4));
}
}

Method with varargs : JAVA Programming by "Gopal Krishna"

public class mwv
{
        public static void main(String args[])
    {
        test("India", "Delhi");
        test("United States", "New York", "California");
    }

    public static void test(String... args)
    {
            for(String arg: args)
            {
                System.out.print("name of Country Is: " +arg+"\n");
            }
    }
}

String class : JAVA Programming by "Gopal Krishna"




public class astr

{
public static void main(String arg[])
{
String s1="sachin";
String s2="SACHIN";
String s3="mohan";
String s4=new String("sachin");
String s5=new String("mohan");
String s6=new String("sachin");
String s7="sachin";
String s18=" sachin ";

if(s1.equals(s7))
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

if(s1.equals(s2))
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

if(s1.equalsIgnoreCase(s2))
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

int a=s1.compareTo(s2);
{
System.out.println("this is="+a);
}

int b=s1.compareToIgnoreCase(s2);
{
System.out.println("this is="+b);
}

int c=s1.compareTo(s3);
{
System.out.println("this is="+c);
}

int d=s3.compareTo(s1);
{
System.out.println("this is="+d);
}


String s8=s1.concat(s2);
{
System.out.println(s8);
}


String s9=s1.concat("  hello");
{
System.out.println(s9);
}

String s10=s1.substring(2);
{
System.out.println(s10);
}

String s11=s3.substring(3);
{
System.out.println(s11);
}

String s12=s1.substring(0,3);
{
System.out.println(s12);
}

String s13=s2.substring(3,5);
{
System.out.println(s13);
}


if(s1.startsWith("sa"))
{
System.out.println("yes");
}
else
{
System.out.println("no");
}

if(s1.endsWith("ni"))
{
System.out.println("yes");
}
else
{
System.out.println("no");
}


int e=s1.length();
{
System.out.println(e);
}

String s15=s3.toUpperCase();
{
System.out.println(s15);
}

String s16=s15.toLowerCase();
{
System.out.println(s16);
}

String s19=s18.trim();
{
System.out.println(s19);
}

int p=s1.indexOf("h");
{
System.out.println(p);
}

char q=s1.charAt(3);
{
System.out.println(q);
}

if(s1==s2)
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

if(s1==s6)
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

if(s1==s5)
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

if(s1==s3)
{
System.out.println("same");
}
else
{
System.out.println("not same");
}

if(s1==s7)
{
System.out.println("same");
}
else
{
System.out.println("not same");
}
}
}

Java.lang pre-defined package : JAVA Programming by "Gopal Krishna"

comming sonnnnnnnnn friendzzzz

Friday, 26 July 2013

Package : JAVA Programming by "Gopal Krishna"

{

  1.  एक फोल्डर बनाना है जिसका नाम वही रखना है जो पैकेज का नाम है |
  2. उस फोल्डर में आपको एक प्रोग्राम लिखना है जिसमे आप कोई से भी फंक्शन हो सकते हैं लेकिन में मेन मेथड नहीं होगा |
  3. मैंने जो प्रोग्राम बनाया है उसमे मैंने डीयर नाम का एक पैकेज बनाया है उसके अन्दर दो जावा फ़ाइल लिखी है  तथा कम्पाइल की हैं | कम्पाइल करने पर उसमे क्लास फ़ाइल आ जायेगी | जिसका प्रयोग हम किसी और प्रोग्राम में करेंगे | ध्यान रहे कि यदि आपके पैकेज का नाम dear है तो आपको पैकेज ( फोल्डर )के अन्दर रखी जावा फाइलों की पहली लाइन package dear; होना चाहिए |
  4. फिर हमने पैकेज ( जो कि एक प्रोग्राम का फोल्डर है ) के साथ ही एक और फ़ाइल रखी जिसमे हम इन क्लास फाइल्स का प्रयोग करेंगे | 
  5.  import dear.*;  ये हम अपने उस प्रोग्राम के ऊपर पहली लाइन में लिखेंगे जिसमें हम पैकेज की क्लास फ़ाइल को  यूज करना चाहते हैं |

}

make a folder and rename it as dear 

now save one file as xyz.java :

package dear;

public class xyz

{
public int a(int i)
{
i=i+1;
System.out.println("The valueof i ->"+i);
return 0;
}
public void disp()
{
System.out.println("i am store in package dear");
}

public void d()
{
System.out.println("i am also store in package dear");
}
}


Save as gkg.java in folder dear :


package dear;

public class gkg
{

public int ak(int i)
{
i=i+2;
System.out.println("The valueof i ->"+i);
return 0;
}

public void dispk()
{
System.out.println("i am store in package dear");
}


public void dk()
{
System.out.println("i am also store in package dear");
}
}


Save as krishna.java parlel to folder dear

import dear.*;

class krishna
{
public static void main(String arg[])
{
xyz j=new xyz();
gkg k=new gkg();
j.d();
j.disp();
j.a(1);

k.dk();
k.dispk();
k.ak(1);


}
}




Code is ready compile and use package in class krishna .. :) 



Hide class in package: If we wants to hide any class then not use public in front of that .



NOTE : we call with the package name if two classes having same name but diffrent function.

abc.xyz.x=new xyz();

Thursday, 25 July 2013

Vector and Wrapper class : JAVA Programming by "Gopal Krishna"

coming sooon

SIMPLE PROGRAMS SET-2 : JAVA Programming by "Gopal Krishna"

1. Display the cost of 12 pencil cost of 5 pencil is 12 :

Code:

class gk
{
public static void main(String arg[])
{
float a=12.0f;
int f=5;
//cost of 5 pencil = 12
float x= (a/f); //icost of 1 pencil
float b=(x*a);//cost of 12 pencil

System.out.println("bil -> "+b);
}


2. Convert the temperature as input = F and output =C .

Code :

class ftoc
{
public static void main(String arg[])
{

System.out.println("ENTER THE TEMPRATURE->"+arg[0]);

float fahrenheit=Integer.parseInt(arg[0]);

float celsius = ((fahrenheit-32)*5/9);

System.out.println(" CONVERTED TEMPRATURE ->"+celsius);
}
}


3. Display cheap and expensive for given book cost . compair with 1000 .

Code:

class chex
{
public static void main(String arg[])
{
System.out.println("ENTER THE PROCE OF BOOK ->"+arg[0]);
int a=Integer.parseInt(arg[0]);
int b=1000;
if (a>b)
{
System.out.println("expensive");
}
else
{
System.out.println("cheep");
}
}
}

4. Display more or less or equal for entered number , no is more then 500 .

Code :

class cmpr
{
public static void main(String arg[])
{
System.out.println("ENTER THE NUMBER->"+arg[0]);
int a=Integer.parseInt(arg[0]);
int b=500;
if (a>b)
{
System.out.println("enterd no is greater then 500");
}
else if (a<b)
{
System.out.println("enterd no is less then 500");
}
else
{
System.out.println("enterd no is equal to 500");
}
}
}

5. Display got admission or not admission on the bases of entered % . (Got admission % is 70 ) .

Code:

class adm
{
public static void main(String arg[])
{
System.out.println("ENTER THE % of student->"+arg[0]);
int a=Integer.parseInt(arg[0]);
int b=70;
if (a>b)
{
System.out.println("get admition");
}
else
{
System.out.println("not admition");
}
}
}
  

6.  Display got admission or not admission on the bases of entered Hindi , English , Math , Computer marks .( Got admission % is more then 70 ) .

Code:

class madm
{
public static void main(String arg[])
{
System.out.println("ENTER THE MARKS of student IN HINDI->"+arg[0]);
System.out.println("ENTER THE MARKS of student IN ENGLISH ->"+arg[1]);
System.out.println("ENTER THE MARKS of student IN MATH ->"+arg[2]);
System.out.println("ENTER THE MARKS of student IN COMPUTER ->"+arg[3]);

int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);
int c=Integer.parseInt(arg[2]);
int d=Integer.parseInt(arg[3]);

int e=(a+b+c+d)/4;
int f=70;

if (e>f)
{
System.out.println("get admition");
}
else
{
System.out.println("not admition");
}
}
}

7. Display the smaller for two numbers .

Code:

class cmcm
{
public static void main(String arg[])
{
System.out.println("ENTER 1st no.>"+arg[0]);
System.out.println("ENTER 1st no.>"+arg[1]);

int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);

if (a>b)
{
System.out.println("in both greater is"+a);
}
else
{
System.out.println("in both greater is"+b);
}
}
}

8. Display illegible or  not illegible on the in bothof math and science marks . Marks should be 60 % .

Code :

class ms
{
public static void main(String arg[])
{

System.out.println("ENTER marks of math ->"+arg[0]);
System.out.println("ENTER marks of science ->"+arg[1]);

int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);

if (a>=60 && b>=60)
{
System.out.println("elegible");
}
else
{
System.out.println("not elegible");
}
}
}

9. Display illegible or  not illegible on the in bothof math and science marks . Marks should be 60 % both .

Code :

class mss
{
public static void main(String arg[])
{

System.out.println("ENTER marks of math ->"+arg[0]);
System.out.println("ENTER marks of science ->"+arg[1]);

int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);

if (a>=60 || b>=60)
{
System.out.println("elegible");
}
else
{
System.out.println("not elegible");
}
}
}

20.  Display the smaller for three numbers .

Code :

class cmcm
{
public static void main(String arg[])
{
System.out.println("ENTER 1st no.>"+arg[0]);
System.out.println("ENTER 1st no.>"+arg[1]);
System.out.println("ENTER 1st no.>"+arg[2]);

int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);
int c=Integer.parseInt(arg[2]);


if ((a>b) && (a>c))
{
System.out.println("in these greater is"+a);
}
else if ((a<b) && (b>c))
{
System.out.println("in bothese greater is"+b);
}
else
{
System.out.println("in these greater is"+c);
}
}
}

Thursday, 18 July 2013

SIMPLE PROGRAMS SET-1 : JAVA Programming by "Gopal Krishna"


1.  Enter your name and display it.
Coading :

class xyz
{
public static void main(String arg[])
{
System.out.println("GOPAL KRISHNA CHAUHAN");
}
}

2.  Enter a number and display it.
Coading :

class xyz
{
public static void main(String arg[])
{
System.out.println("1001910009");
}
}

3.  Display the sum of any two numbers.
Coading :

class xyz
{
public int add(int a,int b)
{
return (a+b);
}
}
class pgm3
{
public static void main(String arg[])
{
xyz x=new xyz();
System.out.println("sum="+x.add(2,5));
}
}

4.  Display the avg. of given number.
Coading :

class xyz
{
public static void main(String arg[])
{
int a=5;
int b=15;
int d=a+b;
int c=d/2;
System.out.println("avg of two numbers="+c);
}
}


5.  Display the bill to pay Raman bought 10 pencil of worth  25/- of and color pens worth 24/- .
Coading :

class xyz
{
public static void main(String arg[])
{
int e=25;
int f=24;
int g=e+f;
 System.out.println("Bill of raman ="+g);
}
}

6.  Display the bill to pay Raman bought 10 pencils for rs. 2.50 And 12 color pen for 5 each.
Coading :

class xyz
{
public static void main(String arg[])
{
int a=10;
double b=2.50;
int c=12;
int d=5;
int e=c*d;
double f=a*b;
double g=e+f;
System.out.println("Bill of raman ="+g);
}
}

7.  Display the entered hour minutes and second into total seconds.
Coading :

class xyz
{
public static void main(String arg[])
{
int a=3;//hour
int b=10;//minut
int c=55;//sec
int d=a*60*60;
int e=b*60;
int f=e+d+c;
System.out.println("total seconds="+f);
}
}


8.  Display the electricity bill on the bases of current and previous reading charges reading and charges 1 current unit=3.
Coading :

class xyz
{
public static void main(String arg[])
{
double a=43533.5;
double b=48424.9;
double c=b-a;
int z=3;
double f=c*z;
System.out.println("electricity Bill of the month ="+f);
}
}

9.  Display the hotal bil on the bases of invited guest .all other will have sup dinar desert . 8% sails tex will be impose on it .
Coading :

class xyz
{
public static void main(String arg[])
{
int a=14;//guest
int b=35;//soop
int c=125;//dinar
int d=35;//desrt
int e=d+c+b;
int f=a*e;
int g=100*f;
int h=g/8;
System.out.println("bill without tax="+f);
System.out.println("bill with tax="+h);
}
}
  


INTERFACE : JAVA Programming by "Gopal Krishna"

Program of Interface :


interface a
{
public void sum();
}

interface b
{
public void sub();
}

class xy implements a,b
{
public void sum()
{
System.out.println("i am sum");
}
public void sub()
{
System.out.println("i am sub");
}
}

abstract class xyz1 extends xy
{
abstract public void mul();
}


class xyz extends xyz1
{
public void mul()
{
System.out.println("i am mal");
}
}

class test
{
public static void main (String args[])
{
xyz x=new xyz();
x.sum();
x.sub();
x.mul();

}

}

Saturday, 6 July 2013

INSTANCE OF KEYWORD : JAVA Programming by "Gopal Krishna"

COMING SOON .....

THIS KEYWORD : JAVA Programming by "Gopal Krishna"


1) The this keyword can be used to refer current class instance variable.

If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.

Understanding the problem without this keyword

Let's understand the problem if we don't use this keyword by the example given below:               
class Student10{  
    int id;  
    String name;  
      
    student(int id,String name){  
    id = id;  
    name = name;  
    }  
    void display(){System.out.println(id+" "+name);}  
  
    public static void main(String args[]){  
    Student10 s1 = new Student10(111,"Karan");  
    Student10 s2 = new Student10(321,"Aryan");  
    s1.display();  
    s2.display();  
    }  
Output:0 null
       0 null
In the above example, parameter (formal arguments) and instance variables are same that is why we are using this keyword to distinguish between local variable and instance variable.

Solution of the above problem by this keyword

//example of this keyword  
class Student11{  
    int id;  
    String name;  
      
    Student11(int id,String name){  
    this.id = id;  
    this.name = name;  
    }  
    void display(){System.out.println(id+" "+name);}  
    public static void main(String args[]){  
    Student11 s1 = new Student11(111,"Karan");  
    Student11 s2 = new Student11(222,"Aryan");  
    s1.display();  
    s2.display();  
}  
}  

Output111 Karan
       222 Aryan

class base
{
base()
{
System.out.println("i am base class");
}
base(int i)
{
System.out.println("i am perametrised");
}
}

class derived extends base

String s;
int a;
public static void main(String args[])
{

derived x=new derived("hello",4);
}

derived (int i)
{
super(i);
System.out.println("the value of i ->"+i);
}
derived (String s,int a)
{
//this(i);
System.out.println("the value of s="+s);
System.out.println("the value of a="+a);

}
}

super keyword in java : JAVA Programming by "Gopal Krishna"

The super keyword in java is a reference variable that is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Usage of java super Keyword

  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.

1) super is used to refer immediate parent class instance variable.

Problem without super keyword
 class Vehicle
    { 
      int speed=50; 
    } 
   
    class Bike3 extends Vehicle
    { 
      int speed=100; 
      void display()
        { 
           System.out.println(speed);//will print speed of Bike  
        } 
         
    }     
    class stest     
    {
      public static void main(String args[])
    { 
       Bike3 b=new Bike3(); 
       b.display(); 
    } 
    }

Output:100

In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance bydefault, but I have to refer parent class instance variable that is why we use super keyword to distinguish between parent class instance variable and current class instance variable.
Solution by super keyword
  1. //example of super keyword  
  2.   
  3. class Vehicle{  
  4.   int speed=50;  
  5. }  
  6.   
  7. class Bike4 extends Vehicle{  
  8.   int speed=100;  
  9.       
  10.   void display(){  
  11.    System.out.println(super.speed);//will print speed of Vehicle now  
  12.   }  
  13.   public static void main(String args[]){  
  14.    Bike4 b=new Bike4();  
  15.    b.display();  
  16.      
  17. }  
  18. }  
Output:50

2) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor as given below:
  1. class Vehicle{  
  2.   Vehicle(){System.out.println("Vehicle is created");}  
  3. }  
  4.   
  5. class Bike5 extends Vehicle{  
  6.   Bike5(){  
  7.    super();//will invoke parent class constructor  
  8.    System.out.println("Bike is created");  
  9.   }  
  10.   public static void main(String args[]){  
  11.    Bike5 b=new Bike5();  
  12.         
  13. }  
  14. }  
Output:Vehicle is created
       Bike is created



Note: super() is added in each class constructor automatically by compiler.





3) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used in case subclass contains the same method as parent class as in the example given below:
  1. class Person{  
  2. void message(){System.out.println("welcome");}  
  3. }  
  4.   
  5. class Student16 extends Person{  
  6. void message(){System.out.println("welcome to java");}  
  7.   
  8. void display(){  
  9. message();//will invoke current class message() method  
  10. super.message();//will invoke parent class message() method  
  11. }  
  12.   
  13. public static void main(String args[]){  
  14. Student16 s=new Student16();  
  15. s.display();  
  16. }  
  17. }  



class xyz
{
xyz()
{
System.out.println("I am default");
}
xyz(int i)
{
System.out.println("The value ->"+i);
}
}

class xyz1 extends xyz
{
xyz1(int i)
{
super(i);
System.out.println("The value of i ->"+i);
}
}

class test
{
public static void main(String arg[])
{
xyz1 x1=new xyz1(10);
}
}