0% found this document useful (0 votes)
69 views16 pages

Java Lab

help for programmer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views16 pages

Java Lab

help for programmer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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++) {

fact=fact*j; } System.out.println("\n\nTHE FACTORAIL OF "+n+" IS " +fact); } } }

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

Program No:2 Program to Find the Prime series upto N numbers


Source Code: /*** 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; for(int i=2;i<num;i++) { // IF THE RESULT IS 0 IT ENTERS THE LOOP ELSE IT RETURNS TRUE VALUE if(num%i==0) { value=false; }

} return value; }

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

{ System.out.println("\n\t" +arr[j]); } } }

Output:
D:\Program Files\Java\jdk1.6.0_02\bin>javac Prime.java D:\Program Files\Java\jdk1.6.0_02\bin>java Prime 20 THE PRIME SERIES IS : 2 3 5 7 11 13 17 19

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

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

Output:

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

Program No:4 Program to perform String Operations


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

} void strUpperCase() { s1=s1.toUpperCase(); System.out.println("After converting to Upper case="+s1); }

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

str.strConcat(); str.strTrim(); str.strSubString(3); str.strChartAt(3); str.strComp("Inam.Mulla"); str.strLowerCase(); str.strUpperCase(); str.strReplace(); } }

Output:
D:\Program Files\Java\jdk1.6.0_02\bin>javac StringOp.java D:\Program Files\Java\jdk1.6.0_02\bin>java StringOp Length of string=4 After concatination=Inam.Mulla

After removing white spaces from starting and beginning=Inam.Mulla Sub String from 3th Position is:m.Mulla 3th Position character is:u Both are Not equal After converting to Lower case=m.mulla After converting to Upper case=M.MULLA After replacement=BCA

Program No:5 Program to Find Area of the Geometrical Figures


Source Code:
class shape { int dim1,dim2; public shape(int d1,int d2) //This is a constructor with 2 parameters { dim1=d1; // Assigning values to the INSTANCE VARIABLES dim2=d2; // Assigning values to the INSTANCE VARIABLES } int area () { System.out.println("AREA UNDEFINED"); return 0; } } class Rectangle extends shape { public Rectangle(int l,int b) //This is a CONSTRUCTOR with 2 parameters { super (l,b); //Invokes 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,x); //Invoke the CONSTRUCTOR of the base class } int area() //Function that calculates the area { return dim1*dim2; } }

public class Geometry { public static void main(String args[]) { shape shape1=new shape(10,15);

//CONSTRUCTOR with 2 parameters

Rectangle rect1=new Rectangle(10,20); //CONSTRUCTOR with 2 parameters square sqr1=new square(10); //CONSTRUCTOR with 1 parameter

System.out.println("AREA: " + shape1.area()); shape s; s=rect1; //creating REFERENCE to class shape //Creates a REFERENCE to rect1

System.out.println("RECTANGLE AREA: " + s.area()); s=sqr1; System.out.println("SQUARE AREA: " + s.area()); } }

Output:

D:\Program Files\Java\jdk1.6.0_02\bin>javac Geometry.java D:\Program Files\Java\jdk1.6.0_02\bin>java Geometry AREA UNDEFINED AREA: 0 RECTANGLE AREA: 200 SQUARE AREA: 100

Program No:6 Program to Show Constructor Overloading


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

} }

Output:
D:\Program Files\Java\jdk1.6.0_02\bin>javac ConsOver.java D:\Program Files\Java\jdk1.6.0_02\bin>java ConsOver a=0.0 b=0.0 a=10.0 b=10.0 a=10.0 b=20.0 a=10.0 b=20.0

Java source code:

/ * PROGRAM TO IMPLEMENT AN APPLET BY PASSING

PARAMETER TO HTML. */
import java.awt.*; import java.applet.*; public class HelloApplet extends Applet { String str; public void init() { str=getParameter("String"); if(str==null) { str="JAVA"; } str="Hello"+str; } public void paint(Graphics g) object { g.drawString(str,100,100);

//Receiving parameter value

//Using the value //paint() method with Graphics

} }

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.class width=400 height=200> <PARAM NAME="String" //Supplies user defined parameters VALUE="APPLET"> </APPLET> </BODY> </HTML> //Ending of a HTML file

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy