You are on page 1of 16

# Program No:1 Program to Find Factorial of N Command Line Arguments

Source Code:
/*** 1.PROGRAM TO FIND FACTORAIL OF LIST OF NUMBER READING INPT AS COMMAND LINE ARGUMENT ***/ class Fact { public static void main(String args[]) { int i,j,n,fact=1,count; // COUNTS THE LENGTH OF ARGUMENT count=args.length; for(i=0;i<count;i++) { // CONVERTS THE INTEGER TYPE INTO STRING TYPE n=Integer.parseInt(args[i]); fact=1; for(j=1;j<=n;j++) {

0_02\bin>javac Fact.0_02\bin>java Fact 3 4 2 THE FACTORAIL OF 3 IS 6 THE FACTORAIL OF 4 IS 24 THE FACTORAIL OF 2 IS 2 .println("\n\nTHE FACTORAIL OF "+n+" IS " +fact).6.out. } } } Output: D:\Program Files\Java\jdk1.6. } System.java D:\Program Files\Java\jdk1.fact=fact*j.

i++) { // IF THE RESULT IS 0 IT ENTERS THE LOOP ELSE IT RETURNS TRUE VALUE if(num%i==0) { value=false.Program No:2 Program to Find the Prime series upto N numbers Source Code: /*** 2.i<num. for(int i=2. } .PROGRAM TO FIND PRIME SERIES READING N AS COMMAND LINE ARGUMENT ***/ class Prime { // METHOD TO CHECK WEATHER THE NUMBER IS PRIME static boolean isprime(int num) { boolean value=true.

int count=0. for(int j=0.} return value. } public static void main(String args[]) { // CONVERTS INTEGER TYPE INTO STRING int num=Integer.i++) { if(isprime(i)) { // STORES THE PRIME NUMBER INTO ARRAY AT COUNT POSITION arr[count]=i. // INCREAMENT COUNT BY 1 count++.i<=num.out.parseInt(args[0]).j++) . } } System.println("\nTHE PRIME SERIES IS : \n \t").j<count. int arr[]=new int[num]. for(int i=2.

0_02\bin>javac Prime.6.println("\n\t" +arr[j]).{ System. } } } Output: D:\Program Files\Java\jdk1.out.6.0_02\bin>java Prime 20 THE PRIME SERIES IS : 2 3 5 7 11 13 17 19 .java D:\Program Files\Java\jdk1.

Program No:3 Program to Sort the numbers in Ascending and Descending order with exception handling Source Code: class Sort { public static void main(String args[]) { int n.length. for(int i=0.i++) { a[i]=Integer.i<n. } try { for(int i=0. a[j]=t. n=args.j++) { if(a[i]>a[j]) { int t=a[i].parseInt(args[i]).j<n. int a[]=new int[n].parseInt(args[i]). a[i]=a[j].i<n-1. } } . int d[]=new int[n]. d[i]=Integer.i++) { for(int j=i+1.

out. System.out. for(int i=0. } } } Output: .} for(int i=0.println(a[i]).i<n.i<n. } } } System. d[j]=t.j<n.println(s). } catch(Exception e) { String s=e.println("After sorting in ascending order=").i++) System.out.toString().out. d[i]=d[j].println(d[i]).j++) { if(d[i]<d[j]) { int t=d[i].i++) System.out. for(int i=0.i++) { for(int j=i+1.println("After sorting in descending order="). System.i<n-1.

D:\Program Files\Java\jdk1.java D:\Program Files\Java\jdk1.6.0_02\bin>java Sort 4 3 2 1 5 After sorting in ascending order= 1 2 3 4 5 After sorting in descending order= 5 4 3 2 1 .6.0_02\bin>javac Sort.

println("Both are Not equal").Mulla ".println("After concatination="+s1).out. } void strLowerCase() { s1=s1.println("Both are equal").println("Length of string="+a).println("After removing white spaces from starting and beginning="+s1). } void strConcat() { String sname=". } void strTrim() { s1=s1.println("After converting to Lower case="+s1). void strLength(String s) { int a=s. } void strComp(String s) { if(s1. System.concat(sname).equals(s)) { System.Program No:4 Program to perform String Operations Source Code: class Cstring{ public String s1="Inam".toLowerCase(). System.trim(). } else System.out. s1=s1. .out. System. System.out.out.length().out.

println("After converting to Upper case="+s1). if(s1.} void strUpperCase() { s1=s1.println("Both are equal if we ignore case of characters").charAt(n). System. System.out.println("After replacement="+s1). } void strReplace() { String s2="BCA".println(n+"th Position character is:"+a).substring(n).mulla". } void strToString() { String s2="inam.toUpperCase().strLength("Inam").println("Sub String from "+n+"th Position is:"+s1). } } public class StringOp { public static void main(String[] args) { Cstring str=new Cstring(). System.replace(s1.s2).println("Both are Not equal even if we ignore case of characters").out.equalsIgnoreCase(s2)) { System.out. s1=s1. . } void strChartAt(int n) { char a=s1. } else System. System.out. str. } void strSubString(int n) { s1=s1.out.out.

strTrim(). str.str.6.Mulla 3th Position character is:u Both are Not equal After converting to Lower case=m.strChartAt(3).mulla After converting to Upper case=M.strReplace().0_02\bin>java StringOp Length of string=4 After concatination=Inam. } } Output: D:\Program Files\Java\jdk1.strUpperCase().MULLA After replacement=BCA . str.strComp("Inam. str.strConcat().strSubString(3). str. str.Mulla").strLowerCase().Mulla Sub String from 3th Position is:m.0_02\bin>javac StringOp.Mulla After removing white spaces from starting and beginning=Inam.6. str.java D:\Program Files\Java\jdk1. str.

int b) //This is a CONSTRUCTOR with 2 parameters { super (l.int d2) //This is a constructor with 2 parameters { dim1=d1. //Invoke the CONSTRUCTOR of the base class } int area() //Function that calculates the area { return dim1*dim2. } } class square extends shape { public square(int x) //This is a CONSTRUCTOR with 1 parameter { super(x.b).Program No:5 Program to Find Area of the Geometrical Figures Source Code: class shape { int dim1. return 0. //Invokes the CONSTRUCTOR of the base class } int area() //Function that calculates the area { return dim1*dim2.println("AREA UNDEFINED"). public shape(int d1. } } .out. // Assigning values to the INSTANCE VARIABLES } int area () { System.x). // Assigning values to the INSTANCE VARIABLES dim2=d2. } } class Rectangle extends shape { public Rectangle(int l.dim2.

15). //creating REFERENCE to class shape //Creates a REFERENCE to rect1 System.public class Geometry { public static void main(String args[]) { shape shape1=new shape(10.println("AREA: " + shape1.area()). s=rect1.6. //CONSTRUCTOR with 2 parameters square sqr1=new square(10).java D:\Program Files\Java\jdk1.out.area()).println("SQUARE AREA: " + s.6. System.0_02\bin>javac Geometry. //CONSTRUCTOR with 2 parameters Rectangle rect1=new Rectangle(10. shape s.0_02\bin>java Geometry AREA UNDEFINED AREA: 0 RECTANGLE AREA: 200 SQUARE AREA: 100 .out. } } Output: D:\Program Files\Java\jdk1. //CONSTRUCTOR with 1 parameter System.20).println("RECTANGLE AREA: " + s. s=sqr1.out.area()).

out. } Constructor(int p. System. .println("a="+a+" b="+b). b=c. } Constructor(int x) { a=x.out. Constructor c3=new Constructor(10. System. b=0.out. b=x. Constructor() { a=0. System. b=q.b.b.Program No:6 Program to Show Constructor Overloading Source Code: class Constructor { float a. } Constructor(Constructor c) { a=c.println("a="+a+" b="+b). Constructor c4=new Constructor(c3).println("a="+a+" b="+b).println("a="+a+" b="+b). System.a.20).out. Constructor c2=new Constructor(10).int q) { a=p. } } public class ConsOver { public static void main(String[] args) { Constructor c1=new Constructor().

if(str==null) { str="JAVA".0 b=20.0 b=0.0_02\bin>javac ConsOver.0 a=10.0 a=10. */ import java.0_02\bin>java ConsOver a=0.6. import java.*.drawString(str.awt.} } Output: D:\Program Files\Java\jdk1.100).6. public class HelloApplet extends Applet { String str. public void init() { str=getParameter("String").0 b=20.0 b=10.java D:\Program Files\Java\jdk1.0 Java source code: / * PROGRAM TO IMPLEMENT AN APPLET BY PASSING PARAMETER TO HTML. //Receiving parameter value //Using the value //paint() method with Graphics .0 a=10.applet. } str="Hello"+str.100.*. } public void paint(Graphics g) object { g.

* *Suhasini Upadhye Roll No:27 University No:1052838039 * Date : 21/08/07 */ <HTML> //Begining of a HTML file <HEAD> //This tag include details about the web page <TITLE>Welcome to Java Applets</TITLE> //The text contained in it will appear in the title bar of the browser <HEAD> <BODY> //This tag contain the main text of the web page <Applet //This tag declares the applet details as its attributes code=HelloApplet.} } HTML Code: /* * PROGRAM TO IMPLEMENT AN APPLET BY PASSING PARAMETER TO HTML.class width=400 height=200> <PARAM NAME="String" //Supplies user defined parameters VALUE="APPLET"> </APPLET> </BODY> </HTML> //Ending of a HTML file OUTPUT: .