Top Banner
Name:- Swapnil Kulkarni Class:- MCA-SY--53 /*1. Write A java Program To Print Following Pattern * @ @ * * * @ @ @ @ */ class Program_1 { public static void main(String args[]) { int line; int row; char ch; for(line=1;line<=4;line++) { for(row=1;row<=line;row++) { if(line%2==0) ch='@'; else ch='*'; System.out.print(ch+" "); } System.out.println(); } } } /*Output- * @ @ * * * @ @ @ @ */
45

Java Programs

Oct 22, 2014

Download

Documents

mandy1890
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*1. Write A java Program To Print Following Pattern*@ @* * *@ @ @ @*/class Program_1{ public static void main(String args[]) { int line; int row; char ch;

for(line=1;line<=4;line++) { for(row=1;row<=line;row++) { if(line%2==0) ch='@'; else ch='*';

System.out.print(ch+" "); } System.out.println(); } }}

/*Output-*@ @* * *@ @ @ @*/

Page 2: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*2. Write A java Program To Print Following Pattern12 34 5 67 8 9 10*/

class Program_2{ public static void main(String args[]) { int line; int row; int cnt;

cnt=1;

for(line=1;line<=4;line++) { for(row=1;row<=line;row++,cnt++) { System.out.print(cnt+" "); } System.out.println(); } }}

/*Output:-12 34 5 67 8 9 10*/

Page 3: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*3. WAP to choose the maximum of an array of "n" numbers. The numbers "n" & the array of n number are from keyboard input.*/

import java.util.*;class Program_3{ public static void main(String args[]) { Scanner in=new Scanner(System.in);

int array[]; int lim; int max; int cnt; int temp;

System.out.println("Enter Limit For Array Elemnts= "); lim=in.nextInt();

array=new int[lim];

for(cnt=0;cnt<lim;cnt++) { System.out.println("Enter Element No "+cnt+" ="); temp=in.nextInt();

array[cnt]=temp; }

max=array[0];

for(cnt=1;cnt<lim;cnt++) { if(max<array[cnt]) max=array[cnt]; }

System.out.println("\n\nMaximum Element From Array Is = "+max); }}

Page 4: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*Output:-Enter Limit For Array Elemnts=5Enter Element No 0 =1Enter Element No 1 =2Enter Element No 2 =3Enter Element No 3 =4Enter Element No 4 =5

Maximum Element From Array Is = 5*/

Page 5: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*4. WAP to check whether given number is perfect no or not.*/

import java.util.*;

class Program_4{ public static void main(String args[]) { Scanner in=new Scanner(System.in);

int num; int temp; int cnt; int total=0;

System.out.println("Enter A No To Check Whether It Is Perfect Or Not= "); num=in.nextInt();

temp=num;

for(cnt=1;cnt<num;cnt++) {

if(num%cnt==0) total=total+cnt;

}

if(total==temp) System.out.println("\nEntered No Is A Perfect No!"); else System.out.println("\nEntered No Is Not A Perfect No!"); }}/*Output:-Enter A No To Check Whether It Is Perfect Or Not=6

Entered No Is A Perfect No!

Enter A No To Check Whether It Is Perfect Or Not=5

Entered No Is Not A Perfect No!*/

Page 6: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*5. WAP to check whether given number is prime no or not.*/

import java.util.*;

class Program_5{ public static void main(String args[]) { Scanner in=new Scanner(System.in);

int num; int flag; int cnt;

System.out.println("Enter A No To Check Whether It Is Prime Or Not= "); num=in.nextInt();

flag=0;

for(cnt=2;cnt<num;cnt++) {

if(num%cnt==0) { flag=1; break;

} }

if(flag==0) System.out.println("\nEntered No Is A Prime No!"); else System.out.println("\nEntered No Is Not A Prime No!"); }}/*Output:-Enter A No To Check Whether It Is Prime Or Not=5

Entered No Is A Prime No!

Enter A No To Check Whether It Is Prime Or Not=6

Entered No Is Not A Prime No!*/

Page 7: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*6. Create an abstract class Person. Derive two classes Employee & Worker From it. Use proper method to accept & display the details for the same. The fields of Employee are emp_no,emp_name,address. Similar fields Worker are name & worker_hours.*/import java.util.*;

abstract class Person{ abstract public void getData(); abstract public void putData();}

class Employee extends Person{ String emp_name; int emp_no; String address;

public void getData() { Scanner in=new Scanner(System.in);

System.out.println("\n\nEnter Emploee No= "); emp_no=in.nextInt();

System.out.println("Enter Employee Name= "); emp_name=in.next();

System.out.println("Enter Employee Address= "); address=in.next(); }

public void putData() { System.out.println("\n\nEmployee No= "+emp_no); System.out.println("Employee Name= "+emp_name); System.out.println("Employee Address= "+address); }}

class Worker extends Person{ String name; int hours;

public void getData() { Scanner in=new Scanner(System.in);

Page 8: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

System.out.println("\n\nEnter Worker Name= "); name=in.next();

System.out.println("Enter Worker hours= "); hours=in.nextInt(); }

public void putData() { System.out.println("\n\nWorker Name= "+name); System.out.println("Worker Working Hours= "+hours); }}

class Program_6{ public static void main(String args[]) { Employee obj_Employee=new Employee();

Worker obj_Worker=new Worker();

obj_Employee.getData();

obj_Worker.getData();

obj_Employee.putData();

obj_Worker.putData(); }}/*Output:-

Enter Emploee No=1Enter Employee Name=SwapnilEnter Employee Address=Pune

Enter Worker Name=AbhiEnter Worker hours=8

Page 9: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

Employee No= 1Employee Name= SwapnilEmployee Address= Pune

Worker Name= AbhiWorker Working Hours= 8*/

Page 10: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*7. WAP A class ExceptionDemo throws the following exception depending upon the following condition Take any integer from keyboard. i] If the integer is between 0 to 5, the exception of type Small Number is thrown. ii] If the number is between 5 to 10 Proper Number is thrown.iii] If number is greater than 10 Greaaater Number is thrown.Also find Factorial of that number.*/

import java.util.*;

class SmallNumberException extends Exception{ String error_msg;

public SmallNumberException(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

class ProperNumberException extends Exception{ String error_msg;

public ProperNumberException(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

class GreaterNumberException extends Exception{ String error_msg;

public GreaterNumberException(String arg) {

Page 11: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

error_msg=arg; }

public String getMessage() { return error_msg; }}

public class ExceptionDemo{ public static void main(String args[]) { Scanner in=new Scanner(System.in);

int num=0; int fact; int cnt;

try {

System.out.println("Enter Number To Find It's Factorial= ");num=in.nextInt();

if(num>=0&&num<=5) throw new SmallNumberException("Entered Number Is Between 0 to 5");

if(num>5&&num<=10) throw new ProperNumberException("Entered Number Is Between 5 to 10");

if(num>10) throw new GreaterNumberException("Entered Number Is Greater Than 10"); } catch(SmallNumberException e) {

System.out.println(e+"\n"+e.getMessage()); } catch(ProperNumberException e) { System.out.println(e+"\n"+e.getMessage()); } catch(GreaterNumberException e) { System.out.println(e+"\n"+e.getMessage()); } finally {

fact=1;

for(cnt=1;cnt<=num;cnt++)fact=fact*cnt;

Page 12: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

System.out.println("\n\nFactorial Of "+num+" Is= "+fact); } }}

/*Output:-Enter Number To Find It's Factorial=5SmallNumberException: Entered Number Is Between 0 to 5Entered Number Is Between 0 to 5

Factorial Of 5 Is= 120*/

Page 13: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*8. Write a class student with attributes roll no, name, age & course.Initialize values through parameterized constructor. If age of student it not between 14 & 23then generate user defined exception "age Not Within The Range. If name contains number or special symbols raise exception "Name Not Valid*/import java.util.*;

class AgeRange extends Exception{ String error_msg;

public AgeRange(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

class InvalidName extends Exception{ String error_msg;

public InvalidName(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

public class Student{ public static void main(String args[]) { String name; int age; int roll_no; String course; int cnt;

Page 14: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

int flag=0;

Scanner in=new Scanner(System.in);

try { System.out.println("Enter Student Roll No= "); roll_no=in.nextInt();

System.out.println("Enter Student Name= "); name=in.next();

System.out.println("Enter Student Age= "); age=in.nextInt();

System.out.println("Enter Student Course= "); course=in.next();

if(age<14||age>23) throw new AgeRange("Age Is Not Within The Range");

for(cnt=0;cnt<=(name.length()-1);cnt++) {

if((name.charAt(cnt)<'A')||(name.charAt(cnt)>'Z'&&name.charAt(cnt)<'a')||(name.charAt(cnt)>'z'))

throw new InvalidName("Name Not Valid");

} } catch(AgeRange e) { System.out.println(e+"\n"+e.getMessage()); } catch(InvalidName e) { System.out.println(e+"\n"+e.getMessage()); } }}/*Output:-Enter Student Roll No=1Enter Student Name=sw@pnilEnter Student Age=15Enter Student Address=Mca

Page 15: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

InvalidName: Name Not ValidName Not Valid

Enter Student Roll No=2Enter Student Name=abhiEnter Student Age=12Enter Student Course=MCAAgeRange: Age Is Not Within The RangeAge Is Not Within The RangePress any key to continue . . .

*/

Page 16: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*9. Create a package MCA which will have 2 classes as Mathametics with a method to add two numbers, add three float& class Maximum with method to find maximum of three numbers*/

Mathematics.javapackage MCA;

public class Mathematics{ public void add(int arg1,int arg2) { System.out.println("Sum Of Two Integer Is "+(arg1+arg2)); }

public void add(double arg1,double arg2,double arg3) { System.out.println("Sum Of Two Integer Is "+(arg1+arg2+arg3)); }}

Maximum.javapackage MCA;

public class Maximum{ public void max(int arg1,int arg2,int arg3) { if(arg1==arg2&&arg2==arg3) System.out.println("All Numbers Are Equal"); else if(arg1>arg2&&arg1>arg3) System.out.println(arg1+" Numbers Is Greater Number"); else if(arg2>arg1&&arg2>arg3) System.out.println(arg2+" Numbers Is Greater Number"); else System.out.println(arg3+" Numbers Is Greater Number"); }}

Page 17: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

Program_9.javaimport MCA.*;

class Program_9{ public static void main(String args[]) { Mathematics obj_Mathematics=new Mathematics();

Maximum obj_Maximum=new Maximum();

obj_Mathematics.add(1,2);

obj_Mathematics.add(1.2,2.3,3.4);

obj_Maximum.max(1,2,3); }}

/*Output:-Sum Of Two Integer Is 3Sum Of Two Integer Is 6.93 Numbers Is Greater NumberPress any key to continue . . .*/

Page 18: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*10. Create an abstarct class Shape.Derive three classes Circle,Rectangle & triangle from it.Calculate Area & volume of all.*/

abstract class Shape{ abstract void area(); abstract void volume();}

class Circle extends Shape{ int radious;

public Circle(int arg) { radious=arg; }

public void area() { System.out.println("Area Of Circle= "+(radious*3.14)); }

public void volume() { System.out.println("Volume Of Circle= "+(radious*radious)); }}

class Rectangle extends Shape{ int length; int width;

public Rectangle(int arg1,int arg2) { length=arg1; width=arg2; }

public void area() { System.out.println("\nArea Of Rectangle= "+(length*width)); }

Page 19: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

public void volume() { System.out.println("Volume Of Rectangle= "+((length*width)*2)); }}

class Triangle extends Shape{ int base; int height;

public Triangle(int arg1,int arg2) { base=arg1; height=arg2; }

public void area() { System.out.println("\nArea Of Triangle= "+(0.5*base*height)); }

public void volume() { System.out.println("Volume Of Triangle= "+(base*height)); }}

public class Program_10{ public static void main(String args[]) { Circle obj_Circle=new Circle(5);

Rectangle obj_Rectangle=new Rectangle(5,6);

Triangle obj_Triangle=new Triangle(5,6);

obj_Circle.area(); obj_Circle.volume();

obj_Rectangle.area(); obj_Rectangle.volume();

obj_Triangle.area(); obj_Triangle.volume();

Page 20: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

}}

/*Output:-Area Of Circle= 15.700000000000001Volume Of Circle= 25

Area Of Rectangle= 30Volume Of Rectangle= 60

Area Of Triangle= 15.0Volume Of Triangle= 30*/

Page 21: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*11. WAP to find exception MarkOutOfBounds. Create class Student if the mark is greater than 100, it must generate the user defined exception called Mark Out Of Bound Exception & throw it.*/

import java.util.*;

class MarkOutOfBounds extends Exception{ String error_msg;

public MarkOutOfBounds(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

public class Student_11{ public static void main(String args[]) { String name; int marks; int roll_no;

Scanner in=new Scanner(System.in);

try { System.out.println("Enter Student Roll No= "); roll_no=in.nextInt();

System.out.println("Enter Student Name= "); name=in.next();

System.out.println("Enter Student Marks= "); marks=in.nextInt();

if(marks>100) throw new MarkOutOfBounds("Marks Are More Than 100!!!");

} catch(MarkOutOfBounds e) {

Page 22: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

System.out.println(e+"\n"+e.getMessage()); } }}

/*Output:-Enter Student Roll No=1Enter Student Name=AbhiEnter Student Marks=100

Enter Student Roll No=1Enter Student Name=VivianEnter Student Marks=105MarkOutOfBounds: Marks Are More Than 100!!!Marks Are More Than 100!!!*/

Page 23: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*12. WAP to accept file name from user as command line argument & display every alternative character of file. Generate exception for insufficient number of argument.*/

import java.util.*;import java.io.*;

class InsufficientArgument extends Exception{ String error_msg;

public InsufficientArgument(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

class FileNotFound extends Exception{ String error_msg;

public FileNotFound(String arg) { error_msg=arg; }

public String getMessage() { return error_msg; }}

class Program_12{ public static void main(String args[])throws Exception { int ch; int cnt=1;

try { if(args.length==1) {

Page 24: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

File f=new File(args[0]); FileReader fr=new FileReader(f);

if(f.exists()) { while((ch=fr.read())!=-1) { if(cnt%2==0) System.out.print((char)ch+" "); cnt++; } } else throw new FileNotFound("File Not Found!!!"); } else throw new InsufficientArgument("File Name Not Specified!!!"); } catch(InsufficientArgument e) {

System.out.println(e+"\n"+e.getMessage()); } catch(FileNotFound e) {

System.out.println(e+"\n"+e.getMessage()); } }}/*Output:-

C:\Java>java Program_12 Test.txth t n B a a s t e a t o f f u l c b s e o e s i e P i t S m o e ( 0 4, O e N g t @ t e C l e t r ( 0 5 h i t k s o i e ( 0 8 t t s (0 9 .

C:\Java>java Program_12InsufficientArgument: File Name Not Specified!!!File Name Not Specified!!!*/

Page 25: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*13. WAP to copy contents of file1.txt to file2.txt into file3.txt using command line argument.*/

import java.util.*;import java.io.*;

class Program_13{ public static void main(String args[])throws Exception { int ch; int cnt=1;

if(args.length==3) { File f1=new File(args[0]); File f2=new File(args[1]); File f3=new File(args[2]);

FileReader fr1=new FileReader(f1); FileReader fr2=new FileReader(f2);

RandomAccessFile ra3=new RandomAccessFile(f3,"rwd");

if(f1.exists()||f2.exists()||f3.exists()) { while((ch=fr1.read())!=-1) { ra3.writeChar(ch); }

ra3.seek(ra3.length());

while((ch=fr2.read())!=-1) { ra3.writeChar(ch);

}

} else System.out.println("File Not Found!!!"); } else System.out.println("File Name Not Specified!!!");

}}

Page 26: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*Output:-file1.txtC h e t a n B h a g a t i s t h e a u t h o r o f f o u r b l o c k b u s t e r n o v e l s ,

file2.txtF i v e P o i n t S o m e o n e ( 2 0 0 4 ) , O n e N i g h t @ t h e C a l l C e n t e r ( 2 0 0 5 ) T h e 3 M i s t a k e s o f l i f e ( 2 0 0 8 ) & 2 S t a t e s ( 2 0 0 9 ) .

file3.txt C h e t a n B h a g a t i s t h e a u t h o r o f f o u r b l o c k b u s t e r n o v e l s , F i v e P o i n t S o m e o n e ( 2 0 0 4 ) , O n e N i g h t @ t h e C a l l C e n t e r ( 2 0 0 5 ) T h e 3 M i s t a k e s o f l i f e ( 2 0 0 8 ) & 2 S t a t e s ( 2 0 0 9 ) .*/

Page 27: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/14. WAP to display content of the file in reverse order.*/

import java.util.*;import java.io.*;

class Program_14{ public static void main(String args[])throws Exception { int ch; long cnt;

if(args.length==1) { File f=new File(args[0]);

if(f.exists()) {

RandomAccessFile ra=new RandomAccessFile(f,"r");

cnt=(ra.length());

ra.seek(cnt);

while(cnt!=-1) {

ra.seek(cnt); System.out.print((char)ra.read()); cnt--;

} } else System.out.println("File Not Found!!!"); } else System.out.println("File Name Not Specified!!!");

}}/*Output:-C:\Java>java Program_14 Test.txt?.)9002( setatS 2 & )8002( efil fo sekatsiM 3 ehT )5002( retneC llaC eht @ thgiN enO ,)4002( enoemoS tnioP eviF ,slevon retsubkcolb ruof fo rohtua eht si tagahB natehC*/

Page 28: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*15. WAP to conver all lowercase character to uppercase & copy content of given file name the source file name & destination file name hasto be accepted from command line argument.*/

import java.util.*;import java.io.*;class Program_15{ public static void main(String args[])throws Exception { int ch; int cnt=1; int diff='A'-'a';

if(args.length==2) { File f1=new File(args[0]); File f2=new File(args[1]);

FileReader fr1=new FileReader(f1);

RandomAccessFile ra3=new RandomAccessFile(f2,"rw");

if(f1.exists()||f2.exists()) { while((ch=fr1.read())!=-1) {

if(ch>='a'&& ch<='z') ra3.writeChar(ch-32); else ra3.writeChar(ch); } } else System.out.println("File Not Found!!!"); } else System.out.println("File Name Not Specified!!!"); }}

/*Output:-C:\Java>java Program_15 file1.txt file3.txtfile1.txtChetan Bhagat is the author of four blockbuster novels,file3.txt C H E T A N B H A G A T I S T H E A U T H O R O F F O U R B L O C K B U S T E R N O V E L S ,

Page 29: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*16. WAP to count uppercase,, lowercase, special character in file*/

import java.util.*;import java.io.*;

class Program_16{ public static void main(String args[])throws Exception { int uchar,lchar,ch,schar;

lchar=uchar=schar=0;

if(args.length==1) { File f=new File(args[0]);

if(f.exists()) {

FileReader ra=new FileReader (f);

while((ch=ra.read())!=-1) {

if(ch>='A'&& ch<='Z') uchar++; else if (ch>='a'&&ch<='z') lchar++; else schar++;

}

System.out.println("Uppercase Characters Are = "+uchar);

System.out.println("Lowercase Characters Are = "+lchar);

System.out.println("Special Characters Are = "+uchar); } else System.out.println("File Not Found!!!"); } else System.out.println("File Name Not Specified!!!");

}}

Page 30: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*Output:-

C:\Java>java Program_16 Test.txtUppercase Characters Are = 12Lowercase Characters Are = 94Special Characters Are = 12

*/

Page 31: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*17. WAP to accept number from textbox & check whether accepted number from client is prime or not display msg on client frame.*/

Server_Program_17.javaimport java.io.*;import java.net.*;

class Server_Program_17{

public static void main(String args[])throws Exception { String msg; int num; int flag; int cnt;

try { Socket sck;

ServerSocket ssck=new ServerSocket(8000);

sck=ssck.accept();

System.out.println("Server Is Running...");

ObjectOutputStream oos=new ObjectOutputStream(sck.getOutputStream());

ObjectInputStream ois=new ObjectInputStream(sck.getInputStream());

String arg=(String)ois.readObject();

num=Integer.parseInt(arg);

flag=0;

for(cnt=2;cnt<num;cnt++) { if(num%cnt==0) { flag=1; break; } }

if(flag==1) msg=num+" No Is Not Prime No!!!!";

Page 32: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

else msg=num+" No Is Prime No!!!!";

oos.writeObject(msg);

ois.close();

oos.close();

sck.close(); } catch(Exception e) {

}

}}

Client_Program_17.javaimport java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.swing.*;

class Client_Program_17 extends JFrame implements ActionListener{ JLabel lbl_1,lbl_2,lbl_3; JTextField txtInput; JButton btnCheck,btnExit;

public Client_Program_17(String str) { super(str);

setLayout(new FlowLayout(FlowLayout.CENTER,4,2));

lbl_1=new JLabel("Enter Number"); lbl_2=new JLabel("Message"); lbl_3=new JLabel();

txtInput=new JTextField("",20);

btnCheck=new JButton("Check");

btnExit=new JButton("Exit");

Page 33: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

add(lbl_1);

add(txtInput); add(btnCheck); add(btnExit); add(lbl_2); add(lbl_3);

btnCheck.addActionListener(this); btnExit.addActionListener(this);

setVisible(true); setSize(500,500); }

public void actionPerformed(ActionEvent ae) { if(ae.getSource()==btnCheck) { try { Socket sck=new Socket("maddy-85c64ded7.",8000);

ObjectOutputStream oos=new ObjectOutputStream(sck.getOutputStream());

ObjectInputStream ois=new ObjectInputStream(sck.getInputStream());

String arg=txtInput.getText();

oos.writeObject(arg);

arg=(String)ois.readObject();

lbl_3.setText(arg);

ois.close();

oos.close();

sck.close();}catch(Exception e){

} } else if(ae.getSource()==btnExit) System.exit(0);

Page 34: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

}

public static void main(String args[]) {

new Client_Program_17("Client Side"); }}

/*Output:-

Output:-Server Is Running...*/

Page 35: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*18. WAP to accept number from client & calculate the sum of Digit & display output on server*/

Server_Program_18.javaimport java.io.*;import java.net.*;

class Server_Program_18{

public static void main(String args[])throws Exception{

Socket sck;

ServerSocket ssck=new ServerSocket(8000);

System.out.println("Server Is Waiting...");

sck=ssck.accept();

System.out.println("Client Request Accepted...");

ObjectOutputStream oos=new ObjectOutputStream(sck.getOutputStream());

ObjectInputStream ois=new ObjectInputStream(sck.getInputStream());

String arg=(String)ois.readObject();

int num=Integer.parseInt(arg);

int total=0;

int rem;

int temp=num;

while(num!=0) {

rem=num%10;

total=total+rem;

num=num/10;}

System.out.println("Sum Of Digits Of "+temp+" = "+total);

ois.close();

ssck.close();

Page 36: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

sck.close();}

}

Client_Program_18.java

import java.io.*;import java.net.*;import java.util.*;

class Client_Program_18{public static void main(String args[])throws Exception{ Socket sck=new Socket("maddy-85c64ded7.",8000);

ObjectOutputStream oos=new ObjectOutputStream(sck.getOutputStream());

Scanner in=new Scanner(System.in);

int num;

System.out.println("Client Side....");

System.out.println("Enter The Number To Find Out Sum Of It's Digits= "); num=in.nextInt();

String arg=String.valueOf(num);

oos.writeObject(arg);

oos.close();

sck.close(); }}/*Output:-Client Side....Enter The Number To Find Out Sum Of It's Digits=1234567890Press any key to continue . . .

Server Is Waiting...Client Request Accepted...Sum Of Digits Of 1234567890 = 45Press any key to continue . . .*/

Page 37: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

/*20. WAP to accept file name from client. Check whether if file exist or not on server. If it exist thendisplay the content of file on client side.*/

Server_Program_20.javaimport java.io.*;import java.net.*;

class Server_Program_20{

public static void main(String args[])throws Exception{

Socket sck;

ServerSocket ssck=new ServerSocket(8000);

System.out.println("Server Is Waiting...");

sck=ssck.accept();

System.out.println("Client Request Accepted...");

ObjectOutputStream oos=new ObjectOutputStream(sck.getOutputStream());

ObjectInputStream ois=new ObjectInputStream(sck.getInputStream());

String arg=(String)ois.readObject();

File f=new File(arg);

if(f.exists()) {

arg="Yes";

System.out.println("File Existed.....");

oos.writeObject(arg);

FileReader fr=new FileReader(f);

int ch;

arg="";

while((ch=fr.read())!=-1){ arg=arg+(char)ch;}

Page 38: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

oos.writeObject(arg);

}else{ arg="No";

System.out.println("File Does Not Exist on Server!!!!");

oos.writeObject(arg);}

ois.close();

ssck.close();

sck.close();}

}

Client_Program_20.java

import java.io.*;import java.net.*;import java.util.*;

class Client_Program_20{

public static void main(String args[])throws Exception{ Socket sck=new Socket("maddy-85c64ded7.",8000);

ObjectOutputStream oos=new ObjectOutputStream(sck.getOutputStream());

ObjectInputStream ois=new ObjectInputStream(sck.getInputStream());

Scanner in=new Scanner(System.in);

String fname;

System.out.println("Client Side....");

System.out.println("\nEnter The File Name= "); fname=in.next();

oos.writeObject(fname);

Page 39: Java Programs

Name:- Swapnil Kulkarni Class:- MCA-SY--53

String reply=(String)ois.readObject();

if(reply.equals("Yes")) {

System.out.println("\nFile Exist On Server Side!!!");

System.out.println("\nFile Content=\n");

String content=(String)ois.readObject();

System.out.println(content);

} else if(reply.equals("No")) {

System.out.println("\nFile Does Not Exist on Server!!!!"); }

oos.close();

sck.close();}

}

/*Output:-Server Is Waiting...Client Request Accepted...File Existed.....

Client Side....

Enter The File Name=Test.txt

File Exist On Server Side!!!

File Content=

Chetan Bhagat is the author of four blockbuster novels, Five Point Someone (2004), One Night @ the Call Center (2005) The 3 Mistakes of life (2008) & 2 States (2009).*/