Tuesday, 16 April 2019


PRACTICAL List:
1)Program to demonstrate Constant Variable.
2)Program to demonstrate scope of Variable
3)Program to demonstrate branching statement
4)Program to demonstrate Looping statement
5)Program to demonstrate simple class
6)Program to demonstrate method parameter
7)Program to demonstrate method overloading
8)Program to demonstrate constructor
9)Program to demonstrate static member
10)Program to demonstrate Method overriding
11)Program to demonstrate Final variable, Method and Final Class.
12)Program to demonstrate Finilize method()
13)Program to demonstrate Array and It’s types.
14)Program to demonstrate String class and it’s method.
15)Program to demonstrate String Buffer and it’s method.
16)Program to demonstrate inheritance and its Types
17)Program to demonstrate Abstract method and Abstract Class.
18)Program to demonstrate Multiple catch statement
19)Program to demonstrate finally clause
20)Program to demonstrate package
21)Program to demonstrate interface
22)Program to demonstrate Byte Stream Class
23)Program to demonstrate Chracter stream class
24)Program to demonstrate Gradatabase connectivity.






//program to demonstrate the constant variable
// final datatype  variable_name= constant value;
import java.io.*;
import java.util.Scanner;
class ConstantDemo
{
public static void main(String args[])
{
final float PI=3.14f;
float radius,area;
Scanner s=new Scanner(System.in);
System.out.println("Enter value for radius");
radius=s.nextFloat();
area=PI*radius*radius;
System.out.println("Area="+area);
}
}

Program to demonstrate scope of Variable
// instance variable -      Variable declared in class definition
//class /static variable - declared with  static keyword
//local variable  -declared and used only in block  {   }
//program for scope of variable
class employee
{
int empno;
public String name;
public employee()
{
empno=101;
name="smita";
}
public void show()
{
System.out.println("empno="+ empno);
System.out.println("Name="+ name);
}
}
class ScopeDemo
{
public static void main(String args[])
{
employee e1=new employee();
e1.show();
e1.name="suman";
e1.show();
}
}
3] Program to demonstrate branching statement
/*if
simple if
if -else
if else ladder
nested if
switch*/
//progam for branching statement
import java.util.Scanner;
import java.io.*;
class OddEven
{
public static void  main(String args[])
{
Scanner s=new Scanner(System.in);
int num;
System.out.println("Enter number");
num=s.nextInt();
if(num%2==0)
{
System.out.println("num is even=   "+num);
}
else
{
System.out.println("num is odd=    "+num);
}

}
}

// branching statment

//program for else if ladder

import java.io.*;
import java.util.Scanner;
class  ElseIfDemo
{
public static void main(String args[])
{
float avg;
Scanner s=new Scanner(System.in);
System.out.println("enter average");
avg=s.nextFloat();
if(avg>=75)
System.out.println("Distinction");
else if(avg>=65)
System.out.println("First class");
else
if(avg>=55)
System.out.println("second class");
else if(avg>=35)
System.out.println("Third class");
else
System.out.println("Fail");

}
}
//program for nested if
import java.io.*;
import java.util.Scanner;
class nestedif
{
public static void main(String args[])
{
int a,b,c,d;
Scanner s=new Scanner(System.in);
System.out.println("enter three nos");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
if(a>b)
{
if(a>c)
{   
if(a>d)
System.out.println("a greater"+a);
else
System.out.println("d greater"+d);
}
else
{
if(c>d)
System.out.println(" c greater"+c);
else
System.out.println("d greater"+d);
}
}
else
{if(b>c)
{
if(b>d)
System.out.println("b is greater"+b);
else
System.out.println("d is greater"+d);
}
else
{if(c>d)
System.out.println("c is greater"+c);
else
System.out.println("d is greater"+d);
}}

}
}
4) Program to demonstrate Looping statement
import java.io.*;
class loop
{
public static void main(String args[])
{
int i=100,sum=0;
do
{
sum=sum+i;
i++;
System.out.println("sum"+sum+"\ni="+i);

}while(i<=10);
System.out.println("sum="+sum);
}
}


//Program for looping statement
//entry controlled -while for
//exit controlled   do while

//program for factorial
4!=4*3*2*
import java .io.*;
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int num,fact=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter number");
num=s.nextInt();
while(num>=1)
{
fact=fact*num;
num--;
}
Sysytem.out.println("factorial="+fact);
}
}

 
 //program for nested for to print numbers in trianle shape


/*1
12
123
1234*/
import java.io.*;
class triangle
{
public static void main(String args[])
{

for(int r=1;r<=5;r++)
{
for(int  c=1;c<=r;c++)
{
System.out.print(" *");
}
System.out.print("\n");
}
System.out.println("out of nested for");
}
}

7) Program to demonstrate method overloading
class  Shapes
{
void area(int len,int width)
{
System.out.println("Area of rectangle="+(len*width));
}
float area(float base,int hight)
{
return(0.5f*base*hight);
}
float area(int radius)
{
return(3.14f*radius*radius);
}
}
class MethodOverloading
{
public static void main(String args[])
{
Shapes s=new Shapes();
s.area(5,6);
float result=s.area(5.5f,6);
System.out.println("Area of Triangle="+result);
result=s.area(8);
System.out.println("Area of Circle="+result);
}
}
5) Program to demonstrate simple class
 import java.util.*;
import java.io.*;
class Student
{
int rno;
String name;
int total;
Scanner s=new  Scanner(System.in);

void getdata()
{
System.out.println("Enter roll number");
rno=s.nextInt();
System.out.println("Enter name of the student");
name=s.next();
System.out.println("Enter total marks of student");
total=s.nextInt();
}
void display()
{
System.out.println("Rollno="+rno);
System.out.println("Name="+name);
System.out.println("Total="+total);
}

}

class StudentRecord
{
public static void main(String args[])
{    
Student s1;
s1=new Student();
for(int i=1;i<=5;i++)
{
s1.getdata();
s1.display();
}

}
}

6) Program to demonstrate method parameter
class Rectangle
{
int len,width;
public void getdata(int l,int w)
{
len=l;
width=w;
}
void area()
{
System.out.println(" Area of rectangle="+(len*width));
}
}
class AreaRect
{
public static void main(String args[])
{
Rectangle r=new Rectangle();
r.getdata(10,20);
r.area();
int x=33, y=11;
r.getdata(x,y);
r.area();
r.len=22;
r.width=33;
r.area();
}
}

8) Program to demonstrate constructor
class student
{
int rno;
String name;
int total;
student() //default constructor
{
rno=1;
name="seeta";
total=400;
}
student(int r,String n,int t) //pararmeterized
{
rno=r;
name=n;
total=t;
}
void display()
{
System.out.println("Roll no="+rno);
System.out.println("Name of student="+name);
System.out.println("Total="+total);
}
}
class DemoConstruct
{
public static void main(String args[])
{
student s=new student();
s.display();
student s1=new student(2,"suman",450);
s1.display();

}
}
9) Program to demonstrate static member
//static variable is shared among the objects of class
// store a single copy of static variable
//it is memory efficient
//can be accessed with class name
class college
{
static int count ;
college()
{
count++;
}
static void display()// static  method
{
System.out.println("Count="+count);
}
}
class StaticDemo
{
public static void main(String args[])
{ college bca=new college();
college.display();
college  bsc=new college();
college.display();
college  msc=new college();
college.display();
college.count=10;
System.out.println("count="+college.count);
}
}


10) Program to demonstrate Method overriding
class Base
{ final void display()
{
System.out.println("method of base class");
}
}
class Derieved extends Base
{
void display()
{
System.out.println("Derieved class method");
}
}

class MethodOverRidding
{
public static void main(String args[])
{
Derieved d=new Derieved();
d.display();// call method of derieved
Base b=new Base();
b.display(); //call base class method
}
}


11) Program to demonstrate Final variable, Method and Final Class.
final class  Test
{
final int a=10;// constant variable
final void show()
{a=20;
System.out.println("Value of a="+a);
}
}
class Test2 extends Test
{void show()
{
System.out.println("Method of class test2");
}
}
class FinalTest
{
public static void main(String args[])
{Test2 t1=new Test2();
t1.show();
}
}



12) Program to demonstrate Finilize method()
class  Test
{
int a;
Test()
{
a=10;
}
void show()
{
System.out.println("A="+a);
}
protected void finalize() throws Throwable
{
System.out.println("Garbage collected");
}
}
class FinalizeDemo
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1=t2;  //t2 in unreferenced object
t1.show();
t2.show();
Runtime.getRuntime().runFinalization();//request to garbage collector
 System.gc();
System.out.println("Executed main method");
}
}


13) Program to demonstrate Array and It’s types.
import java.util.Scanner;
class Search
{
public static void main(String args[])
{
int no[]=new int[10];
int item,loc=0,count=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter 10 number");
for(int i=0;i<10;i++)
{
no[i]=s.nextInt();
}
System.out.println("Enter numbr to search");
item=s.nextInt();
for(int i=0;i<10;i++)
{
if(no[i]==item)
{loc=i;
count++;
}
}
if(count>0)
System.out.println("number found at  "+loc +"   location");
else
System.out.println("number not found ");

}
}

//Program for two dimensional array

/**int a[][]=new int[2][3];
a[0][0]=10
a[0][1]=20;
int b[][]={ { 10,20,30};
                   {30,40,50}
                 }
Scanner  s=new Scanner(System.in);
int c[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=s.nextInt()
}
}
*/

import java.util.Scanner;
class Tdimension
{
public static void main(String args[])
{
int a[][]=new int[2][3];
Scanner s=new Scanner(System.in);
System.out.println("Enter 6 numbers");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=s.nextInt();
}
}

System.out.println("Array of 2X3");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.print("\n");
}

}
}

14) Program to demonstrate String class and it’s method.
import java.lang.*;
class StringTest
{
public static void main(String args[])
{
String name1=new String("  cocsit  ");
String name2=new String("College");

System.out.println("Length of Name1="+name1.length());
System.out.println("Upper case="+name1.toUpperCase());
System.out.println("Upper case="+name1.toLowerCase());
System.out.println("Name="+name1.concat(name2));
System.out.println("Replace  i with I="+name1.replace('i','I'));
System.out.println("Remove space="+name1.trim());
}
}

15) Program to demonstrate String Buffer and it’s method.
class StringBufferTest
{
public static void main(String args[])
{
System.out.println("String buffer class");
int n=0;
StringBuffer obj1=new StringBuffer("Java Language");
StringBuffer obj2=new StringBuffer("Object Oriented");
obj2.setCharAt(6,'-');
System.out.println(obj1.append(obj2));
System.out.println(obj1.insert(5,obj2));
System.out.println("Length="+obj1.length());
}}

16) Program to demonstrate inheritance and its Types
class Employee
{
int id;
String name;
Employee(int id,String name)
{
this.id=id;
this.name=name;
}
void display()
{
System.out.println("Id="+id);
System.out.println("Name="+name);
}
}
class Record extends Employee
{
int bs,hra,da,gs;
Record(int bs)
{super(100,"Abc");
this.bs=bs;
}
void show()
{
display();
System.out.println("Basic salary"+bs);
if(bs>50000)
{hra=bs*20/100;
da=bs*10/100;
}
else
{
hra=0;
da=0;
}
gs=bs+hra+da;
System.out.println("HRa="+hra);
System.out.println("Da="+da);
System.out.println("Gross salary ="+gs);
}
}
class SingleInheritance
{
public static void main(String args[])
{
Record r=new Record(55000);
r.show();
Record r1=new Record(40000);
r1.show();
}
}


17) Program to demonstrate Abstract method and Abstract Class.
abstract class Bank
{
abstract int getInterestRate();
}
class SBI extends Bank
{
int getInterestRate()
{
return(7);
}
}
class HB extends Bank
{
int getInterestRate()
{
return(8);
}}
class AbstractClass1
{
public static void main(String args[])
{
SBI s=new SBI();
System.out.println("SBI Interest rate="+s.getInterestRate());

HB h=new HB();
System.out.println("HB Interest rate="+h.getInterestRate());
}
}

18) Program to demonstrate Multiple catch statement
//multiple catch statement
import java.lang.*;
class  Division
{                                                
public static void main(String args[])
{
try
{

int n=10;
int d=n/0;
}
catch(ArithmeticException e)
{
System.out.println("Divisor can not be zero");
}
catch(ArrayIndexOutOfBoundsException obj)
{
System.out.println("ArrayIndexOutOfBoundException occurred");
}
catch(NullPointerException e)
{
System.out.println("Null pointer Exception occurred");
}
catch(Exception e)
{
System.out.println("Exception occurred");
}
finally
{
System.out.println("Finally block gets executed always");
}
}
}

20) Program to demonstrate package
package mypack;
public class ClassA
{
int a,b;
public ClassA(int a,int b)
{
this.a=a;
this.b=b;
}
public void add()
{
System.out.println("Addition="+(a+b));
}
}
package mypack;
public class ClassB
{
int a,b;
public ClassB(int a,int b)
{
this.a=a;
this.b=b;
}
public void Sub()
{
System.out.println("Subtraction="+(a-b));
}
}
package mypack;
public class ClassA
{
int a,b;
public ClassA(int a,int b)
{
this.a=a;
this.b=b;
}
public void add()
{
System.out.println("Addition="+(a+b));
}
}
20) Program to demonstrate package
interface interface1
{
final static int a=10;
void display();
}
class Myclass implements interface1
{
public void display()
{
System.out.println("A="+a);
}
}

class InterfaceTest
{
public static void main(String args[])
{Myclass m=new Myclass();
m.display();
interface1 obj;
obj=m;
obj.display();
}
}


21) //PROGRAM TO DEMONSTRATE THE CHARACTER BYTE STREAM CLASS
import java.io.*;
class  Input
{
public static void main(String args[])
{
try
{
FileOutputStream out=new FileOutputStream("d:\\Myfile.txt");
String s="This is the file created by byte stream class";

byte b[]=s.getBytes();

out.write(b);

out.close();
}
catch(Exception e)
{
System.out.println("Error");
}

}
}

import java.io.*;
class  Output
{
public static void main(String args[])
{
try
{
FileInputStream in=new FileInputStream("d:\\Myfile.txt");
int i;

while((i=in.read())!=-1)
{
Sytem.out.print((char)i);
}
in.close();
}
catch(Exception e)
{
System.out.println("Error");
}

}
}

22) //program to demonstrate the chracter  stream class

// reading and writing character data  from and to  file
//Write class
//Reader Class
import java.io.*;
class writer
{
public static void main(String args[])
{
try
{
FileWriter w=new FileWriter("Test.txt");
String  s ="This is writer class demo";
w.write(s);
w.close();
FileReader r=new FileReader("Read.java");
int i=0;
while((i=r.read())!=-1)
{
System.out.print((char)i);
}
r.close();
}
catch(Exception e)
{
System.out.println("Error");
}

}
}

//Program for character stream classes
import java.io.*;
class  Reader
{
public static void main(String args[])
{
try
{
FileReader r=new FileReader("test.txt");

int data=0;
while(data!=-1)
{
System.out.print((char)data);
data=r.read();
}
}
catch(Exception e)
{
System.out.println("Error");
}
}
}

//Program to demonstrate the database connectivity wit MySql
    import java.sql.*; 
    class MysqlCon{ 
    public static void main(String args[]){ 
    try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con=DriverManager.getConnection( 
    "jdbc:mysql://localhost:3306/cocsit","root",""); 
    //here sonoo is database name, root is username and password 
    Statement stmt=con.createStatement(); 
    ResultSet rs=stmt.executeQuery("select * from emp"); 
    while(rs.next()) 
    System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "); 
    con.close(); 
    }catch(Exception e){ System.out.println(e);} 
    } 
    }