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++) {

} System.out.6.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).java D:\Program Files\Java\jdk1.fact=fact*j.6. } } } Output: D:\Program Files\Java\jdk1.

i<num.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.i++) { // IF THE RESULT IS 0 IT ENTERS THE LOOP ELSE IT RETURNS TRUE VALUE if(num%i==0) { value=false. for(int i=2. } .Program No:2 Program to Find the Prime series upto N numbers Source Code: /*** 2.

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

out.println("\n\t" +arr[j]).6.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. } } } Output: D:\Program Files\Java\jdk1.0_02\bin>javac Prime.{ System.

for(int i=0.j<n.length.j++) { if(a[i]>a[j]) { int t=a[i]. } try { for(int i=0.i++) { for(int j=i+1. a[i]=a[j].parseInt(args[i]).i++) { a[i]=Integer.parseInt(args[i]). int a[]=new int[n].i<n. a[j]=t.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.i<n-1. int d[]=new int[n]. n=args. d[i]=Integer. } } .

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

java D:\Program Files\Java\jdk1.0_02\bin>javac Sort.6.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 .

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

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

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

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

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

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

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

class width=400 height=200> <PARAM NAME="String" //Supplies user defined parameters VALUE="APPLET"> </APPLET> </BODY> </HTML> //Ending of a HTML file OUTPUT: .} } HTML Code: /* * PROGRAM TO IMPLEMENT AN APPLET BY PASSING PARAMETER TO HTML. * *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.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.