You are on page 1of 32

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 = Integer.parseInt(s[0]);
int si=(p*7*5)/100;
System.out.println("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 numbers.display
even and odd list separately

class EvenOdd
{
public static void main(String s[])
{
int a=Integer.parseInt(s[0]);
int n=Integer.parseInt(s[1]);
System.out.println("Even list between ="+a+"
&"+n);
for(int i=a;i<=n;i++)
{
if(i%2==0)
{
System.out.println(i);
}
}
System.out.println("Odd list between= "+a+" &
"+n);
for(int i=a;i<=n;i++)
{
if(i%2!=0)
{
System.out.println(i);
}
}
}
}

Output:
Program to find greatest of 3 numbers using
command line arguments.
class Greatest
{
public static void main(String s[])
{
int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
int c=Integer.parseInt(s[2]);
int big=(a>b)?a:b;
if(big>c)
{
System.out.println("Greatest is "+big);
}
else
{
System.out.println("Greatest is "+c);
}
}
}

Output:
Program to code the Students grade based on
the result achieve.Students must be declared
failed if the grade if F.
class Grade
{
public static void main(String s[])
{
int marks = Integer.parseInt(s[0]);
if(marks>=80)
{ System.out.println("Grade A");
}
else if(marks>=60)
{
System.out.println("Grade B");
}
else if(marks>=40)
{
System.out.println("Grade C");
}
else if(marks>=32)
{
System.out.println("Grade D");
}
else
{
System.out.println("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;
System.out.println("Area of Rectangle is "+area);
}
Rectangle(int a,int b,int c)
{
int vol = a *b*c;
System.out.println("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;
System.out.println("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;
System.out.println("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;
System.out.println("Cost of Painting is Rs."+cost);
}
}
class Multilvl
{
public static void main(String ar[])
{
Paint ob = new Paint();
ob.Area(2,3);
ob.Volume(2);
ob.Cost(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()
{
System.out.println("This is the Rectangle class :");
System.out.println("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()
{
System.out.println("This is the Circle class :");
System.out.println("Area of Circle is: "+area+" sq. m");
}
}
class Abstrac
{
public static void main(String ar[])
{
Rectangle r = new Rectangle();
r.Area(4,3);
r.Show();
Circle c = new Circle();
c.Area(4,0);
c.Show();
}
}

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()
{
System.out.println("This is the Rectangle class :");
System.out.println("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()
{
System.out.println("This is the Circle class :");
System.out.println("Area of Circle is "+area+" sq.
m");
}
}
class Interfac
{
public static void main(String ar[])
{
Rectangle r = new Rectangle();
r.Area(2,3);
r.Show();
Circle c = new Circle();
c.Area(2,0);
c.Show();
}
}

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);
System.out.println("String1 is appended with
Java : "+sb.append("Java"));
StringBuffer st = new StringBuffer(b);
System.out.println("String 2 is trimmed from
Index 3 to Index 5 : "+st.substring(3,5));
System.out.println("String1 is compared with
String2 : "+a.compareTo(b));
System.out.println("String1 is coverted to upper
case : "+a.toUpperCase());
}
}
Output:
Program to implement use of scanner class

import java.util.*;
class Scan
{
public static void main(String s[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.next();
System.out.println("Name: " + name);
System.out.print("Enter your age: ");
int i = in.nextInt();
System.out.println("Age: " + i);
System.out.print("Enter your salary: ");
double d = in.nextDouble();
System.out.println("Salary: " + d);
System.out.print("Enter your Contact: ");
int j = in.nextInt();
System.out.println("Contact: " + j);
in.close();
}
}

Output:
Program to create a package and import it
into another java file

package pack;
public class A
{
public void msg(){System.out.println("Hello");}
}
import pack.*;

class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
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)
{
System.out.println("handle ArithmeticException ");
}
catch(ArrayIndexOutOfBoundsException e)
{

System.out.println("handleArrayIndexOutOfBoundsException
");
}
catch(NumberFormatException e)
{
System.out.println("handle number format
exception ");
}
catch(Exception e)
{
System.out.println("All exception are handle ");
}
System.out.println("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)
{
e.printStackTrace();
}
}
public static void main(String s[])
{
OwnException ob=new OwnException();
ob.p();
System.out.println("end of main>>>");
}
}

Output:
Program to create thread by implementing
runnable interface

import java.lang.Thread;

class Multi3 implements Runnable


{
public void run()
{
System.out.println("thread is running...");
}

public static void main(String args[])


{
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Output:
Program to create thread by extending
thread class

import java.lang.Thread;
class Multi extends Thread
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi();
t1.start();
}
}
Output:

You might also like