Madurai Kamaraj University: JAVA Programming
Madurai Kamaraj University: JAVA Programming
Submitted by
: : :
BONAFIDE CERTIFICATE
ENROLMENT NUMBER
Certified that this is the bonafide record of work done by _____________________________ of the BCA (Batch No.____) -_______Lab for the year 2008-2009 at the Madurai Kamaraj University (Distance Education) Admission centre, No.11/6, Bakthavataslam Street, P.M.Apartment, West Tambram, Chennai-45.
Staff-in-Charge
Co-ordinator
_______.2009 at the
Madurai Kamaraj University (Distance Education) Admission Centre, No.11/6, Bakthavatsalam Street, P.M.Apartment, West Tambaram, Chennai -45.
Ex.No 1. 2. 3.
Date
Particulars Writing a String to a file To print the content of a file Count the No of words in given files Occurrence of a word Multithread Applet Program reads the given strings Inheritance in Java Create and read student record Client and server Chatting
Page no
Signature
4. 5. 6. 7. 8. 9. 10.
Ex No: 1
Date:
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to writing a string to a file. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
public class demostring { public static void main(String a[]) { try { String s="Welcome"; byte b[]=s.getBytes(); ByteArrayOutputStream b1=new ByteArrayOutputStream(); b1.write(b); FileOutputStream f=new FileOutputStream("TestBCA.doc"); b1.writeTo(f); byte b2[]=b1.toByteArray(); } catch(Exception e) { System.out.println(e); } } }
Output:
C:\BCA>javac demostring.java
Ex No: 2 Date :
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to print the content of a file. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 2:
import java.lang.Exception; public class print { public static void main(String s[]) { try { int i,t,c; String ch=""; FileOutputStream f1 = new FileOutputStream("printtest.txt"); i=0;t=s.length-1; while(i<=t) { FileInputStream f = new FileInputStream(s[i]); while((c=f.read())!=-1) ch=ch+(char)c; byte b[]=ch.getBytes(); f1.write(b); f.close(); i++; ch=""; } f1.close(); } catch(Exception e) { System.out.println(e); }
} } Output:
Aim: -
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to count the no of words in given files. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 3:
public static void main(String args[])throws IOException { int word1=1,c,flag=0,i=0,j=0, m=0; String ch="",word="",arr1=""; char ch1; String arr[]=new String[100]; try { FileInputStream f = new FileInputStream(args[0]);
do { c = f.read(); ch+=(char)c; while(i<ch.length()) { ch1=ch.charAt(i); if(ch1==' ') { arr[j]=arr1; flag=0; j++; arr1=""; }
else i++; } {
arr1=arr1+ch1;
for(i=m+1;i<j;i++) if(arr[m].equals(arr[i])) { word1++; arr[i]=""; } if(arr[m]!="") System.out.println("The number of times of occurence of the "+arr[m]+"is:"+word1); word1=1; } }while(i != -1); f.close(); } catch(FileNotFoundException fne) { System.out.println(fne.getMessage()); } catch(ArrayIndexOutOfBoundsException aie) {
System.out.println(aie.getMessage()); } } } Output:
C:\BCA>javac Count.java C:\BCA>java Count Test.txt The number of times of occurence of the nullis:1 The number of times of occurence of the nullis:1 The number of times of occurence of the nullis:1
Occurrence of a word
Ex No: 4 Date :
Aim: -
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to occurrence of a word. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 4:
public class occurance { public static void main(String s[])throws IOException { try { FileInputStream f=new FileInputStream(s[0]); int word=0,flag=0,c; String ch="",string w=s[1]; while((c=f.read())!=-1) Ch+=(char)c; while(1<ch.length()) { if ((char)c!="")ch=ch+(char)c; else flag=1; if(flag==1) { if(ch.compareTo(w)==0 word++; flag=0; ch=""; } } system.out.println("The number of times of occurrence of the word is:"+word); f.close(); } Output:
C:\Program Files\Java\jdk1.6.0_13\bin>javac Count.java C:\Program Files\Java\jdk1.6.0_13\bin>java Count Thilip.txt hai hfklsdhfklsdhklfhsdkl:1 C:\Program Files\Java\jdk1.6.0_13\bin>
Fibonic Series
Ex No: 5 Date :
Aim: -
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program Fibonic Series. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 5:
import java.io.*; import java.lang.System; import java.io.IOException; import java.lang.String; public class fibonic { public static void main(String a[])throws IOException {
String str; int i,j; int f1,f2,f3; InputStreamReader n=new InputStreamReader(System.in); BufferedReader m=new BufferedReader(n); System.out.println("Enter the N value:"); str=m.readLine(); i=Integer.parseInt(str); f1=0; f2=1; System.out.println(f1+f2); for(j=1;j<=i;j++) { f3=f1+f2; System.out.println(f3); f1=f2; f2=f3; } } }
Output:
10 1 1 2 3 5 8 13 21 34 55 89
Factorial
Ex No: 6 Date :
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to Factorial. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 6:
import java.io.*; public class factorial { public static int fact(int i) { int j,s=1;
for(j=1;j<=i;j++) s *= j; System.out.println(s); return(s); } public static void main(String a[])throws IOException { String str; int n,sum=0,i; BufferedReader m = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value for finding factorial: "); str = m.readLine(); n=Integer.parseInt(str); for(i=1;i<=n;i++) { sum = fact(i); } System.out.println("The sum of Factorial is :"+sum); } }
Output:
C:\BCA>javac factorial.java C:\BCA>java factorial Enter the value for finding factorial: 5 The sum of Factorial is : 120 C:\BCA>
Ex No: 7 Date :
Aim: -
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to program reads the given strings. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 7:
import java.io.FileOutputStream; import java.lang.Exception; import java.lang.String; public class readstring { public static void main(String a[]) { try
{ String s = "Thank you very much"; byte b[]=s.getBytes(); FileOutputStream f = new FileOutputStream("uexcept.txt"); f.write(b); f.close(); } catch(Exception e) { System.out.println(e); } } }
Output:
Inheritance in Java
Ex No: 8 Date :
Algorithm: Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to Inheritance in Java. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 8:
import java.io.*; import java.lang.System; import java.io.IOException; import java.lang.String; class Sclass11 {
protected String name; protected float price; public Sclass11(String n,float p) { name=n; price=p; } public void show_fn() { System.out.print("Name" +name+ "Price"+price); } } class Sclass21 extends Sclass11 { public Sclass21(String s,float p) { super(s,p); } void display() { System.out.println("Rs "+price+" per "+" one "+name); } } public class inheritanca { public static void main(String a[]) { Sclass11 sup = new Sclass11("cycle",2000); Sclass21 sub = new Sclass21("motor",4000); System.out.println(" "); sup.show_fn(); System.out.println(" "); sub.display(); System.out.println(" "); sub.show_fn(); } } Output:
Ex No: 9 Date :
Algorithm: -
Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to create and read student record. Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 9:
import java.io.BufferedReader; import java.io.InputStream; import java.io.PrintStream; import java.lang.Exception; import java.io.IOException; import java.io.FileInputStream; import java.io.*;
public class Record { public static void main(String s[])throws IOException { try { FileOutputStream f=new FileOutputStream(s[0]); PrintStream p=new PrintStream(f); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int op=1; String name,rno; int tmark; while(op!=0) { System.out.print("Enter the Name: "); name=br.readLine(); System.out.print("Enter the Rollno: "); rno=br.readLine(); System.out.print("Enter the Total mark: "); tmark=Integer.parseInt(br.readLine()); System.out.print("Do you want to continue(O/I): "); op=Integer.parseInt(br.readLine()); p.println(rno+":"+name+":"+tmark); }
} catch(Exception e) { System.out.println(e.getMessage()); } } }
Output:
C:\Program Files\Java\jdk1.6.0_13\bin>javac Record.java C:\Program Files\Java\jdk1.6.0_13\bin>java Record br.txt Enter the Name: Thilipnath Enter the Rollno: A8L423 Enter the Total mark: 1200 Do you want to continue(O/I): 1 Enter the Name: Narayanan Enter the Rollno: A8L424
C:\Program Files\Java\jdk1.6.0_13\bin>
Ex No: 10 Date :
Algorithm: -
Step 1 To start the program. Step 2 Initialize the Program. Step 3 Declare the main program. Step 4 Using Program to Client & Server Chating Program Step 5 To save and compile the program. Step 6 Run the program. Step 7 To stop the program.
Program 10:
SERVER import java.net.*; class Server { public static DatagramSocket ds; public static byte b[] = new byte[1024]; public static void main(String arg[]) throws Exception { InetAddress ia = InetAddress.getLocalHost();
//
InetAddress ia = InetAddress.getByName(arg[0]); ds = new DatagramSocket(); int p=0; while(true) { int c = System.in.read(); switch(c) { case '*' : System.out.println("Server Quit"); return; case '\n' : ds.send(new DatagramPacket(b,p,ia,1999)); p=0; break; default : b[p++]=(byte)c; } } }
Client Server import java.net.*; class Client { public static DatagramSocket ds; public static byte b[] = new byte[1024]; public static void main(String arg[]) throws Exception { ds = new DatagramSocket(1999); while(true) { DatagramPacket p = new DatagramPacket(b,b.length);
Output:
Server
Client