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
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;
System.out.println("\n\nMaximum Element From Array Is = "+max); }}
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*/
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!*/
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!*/
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);
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
Name:- Swapnil Kulkarni Class:- MCA-SY--53
Employee No= 1Employee Name= SwapnilEmployee Address= Pune
Worker Name= AbhiWorker Working Hours= 8*/
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) {
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("\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*/
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;
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();
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 . . .
*/
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"); }}
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 . . .*/
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)); }
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();
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*/
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();
if(marks>100) throw new MarkOutOfBounds("Marks Are More Than 100!!!");
} catch(MarkOutOfBounds e) {
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!!!*/
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;
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!!!*/
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;
} else System.out.println("File Not Found!!!"); } else System.out.println("File Name Not Specified!!!");
}}
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 ) .*/
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;
/*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(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 ,
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!!!");
}}
Name:- Swapnil Kulkarni Class:- MCA-SY--53
/*Output:-
C:\Java>java Program_16 Test.txtUppercase Characters Are = 12Lowercase Characters Are = 94Special Characters Are = 12
*/
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.*/
System.out.println("\nEnter The File Name= "); fname=in.next();
oos.writeObject(fname);
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).*/