Core java practice Examples

Example 1  Print hello

class ex1
{
public static void main(String args[])
{
System.out.println(“hello world”);
}
}

 

Example 2  Calculte area of circle

class a
{
public static void main(String args[])
{
double pi,r,area;
pi= 3.14;
r= 10.20;
area = pi * r * r;
System.out.println(“the area of the circle is” + area);
}
}

 

Example 3 Display square root of  a float no and use of Math class.

class squareroot{
public static void main(String args[])
{
float a = 23.25f;
System.out.println(“sqare root of”+ Math.sqrt(a));
System.out.println(“maximumof two no (8,4) is=”+ Math.max(8,4))
}
}

Example 4 Use of two dimensional array

class twodiar
{
public static void main(String args[])
{
float myarr[][]={{-56.7f},{500.1f,70.70f},{100.9f,0.5f,20.20f}};
int i,j;
System.out.println(” the length of arry is =”+myarr.length);
for (i=0;i<myarr.length;i++)
{
for (j=0;j<=i;j++)
{
System.out.print(myarr[i][j]+” “);
}
System.out.println();
}
}
}

Example 5 Use of string

class usestring
{
public static void main(String args[])
{
String myname = “kruti “+ ” jani”;
System.out.println(myname);
}
}

Example 6 Use of substring

class firsttench
{
public static void main(String args[])
{
String s = “abcdef ghijk lmnop”;
System.out.println(s.substring(0,10));

}
}

 

Example 7 Display last no of string using length function

class lastno
{
public static void main(String args[])
{
String s = “1,2,3,4,5,6,7”;
System.out.println(s.substring(“,”,s.length()));
}
}

Example 8 Append at string buffer

class buffappen
{
public static void main (String args[])
{
StringBuffer s = new StringBuffer(“abcd”);
s.append(‘e’);
System.out.println(“s.length==”+s.length());
System.out.println(“s.reverse==”+s.reverse());
s.setCharAt(3,’k’);
System.out.println(“s.tostring==”+s.toString());
s.setLength(10);
System.out.println(“s.length==”+s.length());
}}

 

Example 9  Insert at string buffer

class buffinsrt
{
public static void main (String args[])
{
StringBuffer s = new StringBuffer(“efgh”);
int a=s.length()-s.length();
s.insert(a+1,”abcd”);
System.out.println(“s.buffer is ==”+s.toString());
}
}

 

Example 10 Use of Integer class

class stringtoint
{
public static void main(String args[])
{
String s = “125”;
Integer obj= new Integer(s);
obj=Integer.valueOf(s);
int i = obj.intValue();
// or int i = Integer.valueOf(s).intValue();
// int i = Integer.parseInt(s).
i+=10;
System.out.println(i);
}
}

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.