All Java Practicals-Balaji

  • View
    216

  • Download
    1

Embed Size (px)

DESCRIPTION

For only B.Sc. IT Students..

Text of All Java Practicals-Balaji

Class:- SYIT Subject:- JDS

Class:- SYIT Subject:- JDS

PRACTICAL:- 1AIM:-Design a calculator class in Java and implement all the methods required by calculator operations.

PROGRAM CODE:-

import java.io.*;class calculator

{

int a,b;

int ans;

public void setdata(int x,int y)

{

a=x;

b=y;

}

public void add()

{

ans=a+b;

System.out.println("ADDITION="+ans);

}

public void sub()

{

ans=a-b;

System.out.println("SUBTRACTION="+ans);

}public void mul()

{

ans=a*b;

System.out.println("MULTIPLICATION="+ans);

}

public void div()

{

ans=a/b;

System.out.println("DIVISSION="+ans);

}

}

class calcDemo

{

public static void main(String a[])

{

int x,y;

x=23;y=56;

calculator c1=new calculator();

c1.setdata(x,y);

c1.add();

c1.sub();

c1.mul();

c1.div();

}

}

OUTPUT:-

PRACTICAL:- 1AIM:- Design a Java class for method overloading.PROGRAM CODE:-import java.io.*;class over{void area(int a,int b){System.out.println("Area of Rectangle="+(a*b));}void area(int a){System.out.println("Area of Square="+(a*a));}double area(double a){double d;d=3.14*a*a;return d;}}class overDemo{public static void main(String args[]){over o=new over();o.area(10,20);o.area(10);double b=o.area(3.4);System.out.println("Area of Circle="+b);}}OUTPUT:-PRACTICAL:- 2AIM:- Design a Java program for hybrid Inheritance and Method Overriding.PROGRAM CODE:-class student{int roll_number;void get_number(int a){roll_number=a;}void put_number(){System.out.println("Roll No. :"+roll_number);}}interface test{final int nos=2;void get_marks(double s1,double s2);}class Result extends student implements test{double sem1,sem2;public void get_marks(double s1,double s2){sem1=s1;sem2=s2;System.out.println("Marks Obtained :");System.out.println("sem1 :"+sem1);System.out.println("sem2 :"+sem2);}double total,avg;void display(){total=sem1+sem2;avg=total/nos;put_number();System.out.println("Total Marks :"+total);System.out.println("Average :"+avg);}}class multint{public static void main(String args[]){Result R1=new Result();R1.get_number(123);R1.get_marks(460.5,240.5);R1.display();}}OUTPUT:-PRACTICAL:- 2AIM:- Design a Java class fot the use of interface.PROGRAM CODE:-class student{int roll_number;void get_number(int a){roll_number=a;}void put_number(){System.out.println("Roll No. :"+roll_number);}}interface test{final int nos=2;void get_marks(double s1,double s2);}class Result extends student implements test{double sem1,sem2;public void get_marks(double s1,double s2){sem1=s1;sem2=s2;System.out.println("Marks Obtained :");System.out.println("sem1 :"+sem1);System.out.println("sem2 :"+sem2);}double total,avg;void display(){total=sem1+sem2;avg=total/nos;put_number();System.out.println("Total Marks :"+total);System.out.println("Average :"+avg);}}class multint{public static void main(String args[]){Result R1=new Result();R1.get_number(123);R1.get_marks(460.5,240.5);R1.display();}}OUTPUT:-PRACTICAL:- 2AIM:-Design a Java class performing string operation.PROGRAM CODE:-import java.lang.*;class CaseDemo{public static void main(String args[]){String s1="MAHATMA";String s2="gandhi";String temp;temp=s1.toLowerCase();System.out.println("Original String1 is :"+s1+"\nAfter calling toLowerCase(),String1 is: "+temp);temp=s2.toUpperCase();System.out.println("Original String2 is :"+s2+"\nAfter calling toUpperCase(),String2 is: "+temp);System.out.println();System.out.println("charAt");String s="INDIA";char ch;ch=s.charAt(2);System.out.println("String is :"+"\n Character at Index 2 is :"+ch);System.out.println();System.out.println("compareTo and compareToIgnoreCase");s1="computer";s2="computer";if(s1.compareTo(s2)==0)System.out.println("Strings are equals");elseSystem.out.println("Strings are not equal");if(s1.compareToIgnoreCase(s2)==0)System.out.println("Stringd are equal");elseSystem.out.println("Strings are not equal");System.out.println();System.out.println("\nConcatenated String after calling concat() :"+s1.concat(s2));temp=s1+s1;System.out.println("\nConcatenated String after applying'+'operator :"+temp);System.out.println();System.out.println("equals");s1="Java";s2=new String(s1);System.out.println(s1+"equals"+s2+"a"+s1.equals(s2));System.out.println(s1+"=="+s2+"a"+(s1==s2));System.out.println();System.out.println("getChars(start,end,buffer,0)");s="Java is Internet Language";int start=8;int end=16;char buffer[]=new char[end-start];System.out.println("String is :"+s);s.getChars(start,end,buffer,0);System.out.println("Extraxted characters from String :");System.out.println(buffer);System.out.println();System.out.println("indexOf");System.out.println("\nindexOf(t)="+s.indexOf('t'));System.out.println();System.out.println("length");System.out.println("Length of String is :"+s.length());System.out.println();System.out.println("replace");s1="Bye Bye !!";s2=s1.replace('y','e');System.out.println("Original String is :"+s1);System.out.println("String after calling replace() :"+s2);System.out.println();System.out.println("substring");System.out.println("Original String is :"+s);temp=s.substring(1,2);System.out.println("Substring from index 12 to end of String is :"+temp);temp=s.substring(0,5);System.out.println("Substring from index 0 to 4 is :"+temp);System.out.println();System.out.println("trim");s1="Mahatma Gandhi";temp=s1.trim();System.out.println("Original String is :"+s1);System.out.println("String after calling trim() :"+temp);}}OUTPUT:-PRACTICAL:- 3AIM:-Design a Java class for the use of super.PROGRAM CODE:-class CD{String title;//name of the itemint length;//number of minutesboolean avail;//is the type in the storeCD(CD vt){title=vt.title;length=vt.length;avail=vt.avail;}CD(String t,int l,boolean a){title=t;length=l;avail=a;}CD(){title=null;length=0;avail=false;}}class Movie extends CD{String director;//name of the directorString rating;//G,PG,R or XMovie(Movie m){super(m);director=m.director;rating=m.rating;}Movie(String t,int l,boolean a,String d,String r){super(t,l,a);director=d;rating=r;}Movie(){super();director=null;rating=null;}public void show(){System.out.println("Title :"+title+"\nlength :"+length+"\nAvail :"+avail);System.out.println("Director :"+director+"\nRating :"+rating);}}class SuperObjRef{public static void main(String args[]){Movie m=new Movie("Jaws",120,true,"Special","PG");CD cd1=new CD();m.show();cd1=m;//assign movie rference to CD reference.cd1.title="Titanic";cd1.length=120;cd1.avail=true; // cd1.director="spielberg";//illegal // cd1.rating="PG";//illegal // cd1.show();//illegal}}OUTPUT:-PRACTICAL:- 3AIM:-Design a Java class for implementing the Package.PROGRAM CODE:-1) L.java File:-package p1;public class L{int a,b;public L(){a=100;b=20;}public void add(){System.out.println("ADD="+(a+b));}public void sub(){System.out.println("SUB="+(a-b));}public void mul(){System.out.println("MUL="+(a*b));}public void div(){System.out.println("DIV="+(a/b));}}2) K.java File:-import p1.*;class K{public static void main(String args[]){L p=new L();System.out.println("Extending packages.........");p.add();p.sub();p.mul();p.div();}}OUTPUT:-PRACTICAL:- 4AIM:-Design a Java class for implementing the concept of multithreading.PROGRAM CODE:-class newthread extends Thread{newthread(){super("demo thread");System.out.println("child thread :"+this);start();}public void run(){try{for(int i=5;i>0;i--){System.out.println("child thread :"+i);Thread.sleep(500);}}catch(InterruptedException e){System.out.println("child thread Interrupt");}System.out.println("Exiting child thread");}}class extendthread{public static void main(String args[]){new newthread();try{for(int i=5;i>0;i--){System.out.println("main thread :"+i);Thread.sleep(1000);}}catch(InterruptedException e){System.out.println("main thread interrupt");}System.out.println("main thread exiting");}}OUTPUT:-PRACTICAL:- 4AIM:-Design a Java class for performing all the file input and output.PROGRAM CODE:-1)FileInputStream :-import java.io.*;class FIS{public static void main(String args[]) throws Exception{int size;InputStream f=new FileInputStream("aaa.txt");System.out.println("Total Bytes : "+(size=f.available()));System.out.println("Reading first 3 bytes");for(int i=0; i