Program to calculate simple interest (SI) for any
amount computed at the rate of 7% for 5 years.
class SimpleInterest
{
public static void main(String s[])
{
int p = [Link](s[0]);
int si=(p*7*5)/100;
[Link]("SimpleInterest for 5years at rate of 7% is=
"+si);
}
}
Output:
Program to count a number of even numbers and
odd numbers from a given list of [Link]
even and odd list separately
class EvenOdd
{
public static void main(String s[])
{
int a=[Link](s[0]);
int n=[Link](s[1]);
[Link]("Even list between ="+a+"
&"+n);
for(int i=a;i<=n;i++)
{
if(i%2==0)
{
[Link](i);
}
}
[Link]("Odd list between= "+a+" &
"+n);
for(int i=a;i<=n;i++)
{
if(i%2!=0)
{
[Link](i);
}
}
}
}
Output:
Program to find greatest of 3 numbers using
command line arguments.
class Greatest
{
public static void main(String s[])
{
int a=[Link](s[0]);
int b=[Link](s[1]);
int c=[Link](s[2]);
int big=(a>b)?a:b;
if(big>c)
{
[Link]("Greatest is "+big);
}
else
{
[Link]("Greatest is "+c);
}
}
}
Output:
Program to code the Students grade based on
the result [Link] must be declared
failed if the grade if F.
class Grade
{
public static void main(String s[])
{
int marks = [Link](s[0]);
if(marks>=80)
{ [Link]("Grade A");
}
else if(marks>=60)
{
[Link]("Grade B");
}
else if(marks>=40)
{
[Link]("Grade C");
}
else if(marks>=32)
{
[Link]("Grade D");
}
else
{
[Link]("Fail");
} } }
Output:
Program to overload a constructor of
rectangle class having length and breadth as
variables and int area() as method.
class Rectangle
{
int l,w;
Rectangle(int x,int y)
{
l=x;
w=y;
int area= l*w;
[Link]("Area of Rectangle is "+area);
}
Rectangle(int a,int b,int c)
{
int vol = a *b*c;
[Link]("Volume of Rectangle is "+vol);
}
public static void main(String s[])
{
Rectangle o = new Rectangle(4,3);
Rectangle ob = new Rectangle(4,3,4);
}
}
Output:
Program to implement Multilevel
Inheritance.
class Room
{
int length,breadth,area;
public void Area(int x,int y)
{
length=x;
breadth=y;
area = length * breadth;
[Link]("Area of Room is "+area+" sq. ft.");
}
}
class BedRoom extends Room
{
int height,volume;
public void Volume(int z)
{
height=z;
int vol=length*breadth*height;
[Link]("Volume of room is "+vol+" cu. ft.");
}
}
class Paint extends BedRoom
{
public void Cost(int a)
{
int csa=2*(area+(height*length));
int farea=csa+area;
int cost=a*farea;
[Link]("Cost of Painting is Rs."+cost);
}
}
class Multilvl
{
public static void main(String ar[])
{
Paint ob = new Paint();
[Link](2,3);
[Link](2);
[Link](50);
}
}
Output:
Program to implement Abstract class
abstract class Shape
{
abstract void Area(int x,int y);
abstract void Show();
}
class Rectangle extends Shape
{
int l,b,area;
public void Area(int x,int y)
{
l=x;
b=y;
area=l*b;
}
public void Show()
{
[Link]("This is the Rectangle class :");
[Link]("Area of Rectangle is "+area+" sq. m");
}
}
class Circle extends Shape
{
double r;
double area;
public void Area(int a,int b)
{
r=(double)a;
area=3.14*r*r;
}
public void Show()
{
[Link]("This is the Circle class :");
[Link]("Area of Circle is: "+area+" sq. m");
}
}
class Abstrac
{
public static void main(String ar[])
{
Rectangle r = new Rectangle();
[Link](4,3);
[Link]();
Circle c = new Circle();
[Link](4,0);
[Link]();
}
}
Output:
Program to implement Interfaces.
interface Shape
{
void Area(int x,int y);
void Show();
}
class Rectangle implements Shape
{
int l,b,area;
public void Area(int x,int y)
{
l=x;
b=y;
area=l*b;
}
public void Show()
{
[Link]("This is the Rectangle class :");
[Link]("Area of Rectangle is "+area+" sq.
m");
}
}
class Circle implements Shape
{
double r;
double area;
public void Area(int a,int b)
{
r=(double)a;
area=3.14*r*r;
}
public void Show()
{
[Link]("This is the Circle class :");
[Link]("Area of Circle is "+area+" sq.
m");
}
}
class Interfac
{
public static void main(String ar[])
{
Rectangle r = new Rectangle();
[Link](2,3);
[Link]();
Circle c = new Circle();
[Link](2,0);
[Link]();
}
}
Output:
Program to read 2 strings using StringBuffer
and perform the following operations:
a) Append ("Java") to string 1
b) Trim() string2
c) Compare() both strings
d) Convert string1 in Upper case
class StringB
{
public static void main(String s[])
{
String a = s[0];
String b = s[1];
StringBuffer sb = new StringBuffer(a);
[Link]("String1 is appended with
Java : "+[Link]("Java"));
StringBuffer st = new StringBuffer(b);
[Link]("String 2 is trimmed from
Index 3 to Index 5 : "+[Link](3,5));
[Link]("String1 is compared with
String2 : "+[Link](b));
[Link]("String1 is coverted to upper
case : "+[Link]());
}
}
Output:
Program to implement use of scanner class
import [Link].*;
class Scan
{
public static void main(String s[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Name: " + name);
[Link]("Enter your age: ");
int i = [Link]();
[Link]("Age: " + i);
[Link]("Enter your salary: ");
double d = [Link]();
[Link]("Salary: " + d);
[Link]("Enter your Contact: ");
int j = [Link]();
[Link]("Contact: " + j);
[Link]();
}
}
Output:
Program to create a package and import it
into another java file
package pack;
public class A
{
public void msg(){[Link]("Hello");}
}
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
[Link]();
}
}
Output:
Program to implement exceptional handling
using multiple catch statements
class ExceptionTest
{
public static void main(String s[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
[Link]("handle ArithmeticException ");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("handleArrayIndexOutOfBoundsException
");
}
catch(NumberFormatException e)
{
[Link]("handle number format
exception ");
}
catch(Exception e)
{
[Link]("All exception are handle ");
}
[Link]("end of code");
} }
Output:
Program to create your own exception
class OwnException
{
void m()
{
int data=50/0;
}
void n()
{
m();
}
void p()
{
try
{
n();
}
catch(Exception e)
{
[Link]();
}
}
public static void main(String s[])
{
OwnException ob=new OwnException();
ob.p();
[Link]("end of main>>>");
}
}
Output:
Program to create thread by implementing
runnable interface
import [Link];
class Multi3 implements Runnable
{
public void run()
{
[Link]("thread is running...");
}
public static void main(String args[])
{
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
[Link]();
}
}
Output:
Program to create thread by extending
thread class
import [Link];
class Multi extends Thread
{
public void run()
{
[Link]("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi();
[Link]();
}
}
Output: