Anda di halaman 1dari 33

/*

AIM:Write a program to calculate the sum of the following series :


1)1+(1/2)+(1/3)+.....+(1/n)
2)1-(1/2)+(1/3)-.....+(1/n)
3)1+(1/2^2)+(1/3^3)+.....+(1/n^n)
4)1+(1/3!)+(1/5!)+.....+(1/(2n-1)!)

*/

import java.io.*;
class Series
{
public static void main(String args[]) throws IOException
{
BufferedReader obj =new BufferedReader(new
InputStreamReader(System.in));
int i;
float s=0.0f;
System.out.println("Enter the no.: ");
int n=Integer.parseInt(obj.readLine());

for(i=1;i<=n;i++)
{
s=s+(1.0f/i);
}
System.out.println("Sum of series1 is : "+s);
s=0;
int sign =1;
for(i=1;i<=n;i++)
{
if (i%2==0)
{
sign=sign*(-1);
}
else
{
}

sign=1;

s=s+sign*(1.0f/i);
}
System.out.println("Sum of series2 is : "+s);
s=0;
for(i=1;i<=n;i++)
{

s=s+(1.0f/(i*i));

}
System.out.println("Sum of series3 is : "+s);
s=0;
int j;
int fact =1;
for(i=1;i<=n;i++)
{
for(j=1;j<=((2*i)-1);j++)
{
fact=fact*j;
}
s=s+(1.0f/fact);
}
System.out.println("Sum of series4 is : "+s);
}
}
/*

OUTPUT:

Enter the no.: 2


Sum of series1 is : 1.5
Sum of series2 is : 0.5
Sum of series3 is : 1.25
Sum of series4 is : 1.1666666

*/

.................................................................................................................
/*

Aim: Write a program to find greatest of 3 numbers using command line arguments

Class: SE-IT
Batch: B3

Roll No.:

*/

import java.io.*;
class GreatestNumber
{
public static void main(String args[])
{
int a,b,c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
if (a>b)
{
if (a>c)
{
System.out.println("The greatest number is "+a);
}
else
{
System.out.println("The greatest number is "+c);
}
}
else
{
if (b>c)
{
System.out.println("The greatest number is "+b);
}
else
{
System.out.println("The greatest number is "+c);
}
}

}
}
/*

Output: java GreatestNumer 1 2 3


The greatest number is 3
java GreatestNumer 1 3 2
The greatest number is 3
java GreatestNumer 3 1 3
The greatest number is 3

*/

...................................................................................................................................

/* Aim-Write a program that will read a double type value from the keyboard and print
the following output :a) Small Integer not less than the number.
b) Given Number.
c) Largest Integer not greater than the number. */
import java.io.*;
class Exp3
{public static void main(String args[]) throws IOException
{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a double value : ");
double d = Double.parseDouble(br.readLine());
System.out.println("Smaller Value is : "+Math.floor(d));
System.out.println("Value is : "+d);
System.out.println("Greater Value is : "+Math.ceil(d));
}
}
/*

OUTPUT :

Enter a double value : 3.8


Smaller Value is : 3.0
Value is : 3.8
Greater Value is : 4.0

*/

.......................................................................................................................
/*

AIM : WAP on Calculator using switch case & operators

import java.io.*;

*/

class Calculator
{
public static void main(String args[]) throws IOException
{
int a,b;
double c;
char d;
DataInputStream in = new DataInputStream(System.in);
System.out.println("Enter the first number: ");
a = Integer.parseInt(in.readLine());
System.out.println("Enter the second number: ");
b = Integer.parseInt(in.readLine());
System.out.println("Numbers are "+a+" and "+b);
System.out.println(" +.Addition ");
System.out.println(" -.Subtract ");
System.out.println(" *.Multiply ");
System.out.println(" /.Divide ");
System.out.println(" Enter the choice you want ");
d=(char)System.in.read();
switch (d)
{
case '+':
c=a+b;
System.out.println(" Sum is "+c);
break;
case '-':
c=a-b;
System.out.println(" Difference is "+c);
break;
case '*':
c=a*b;
System.out.println(" Product is "+c);
break;
case '/':

c=(double)a/b;
System.out.println(" Remainder is "+c);
break;
default : System.out.println(" Entered wrong choice ");
}
}
}
/* Output
Enter the first number:
5
Enter the second number:
10
Numbers are 5 and 10
+.Addition
-.Subtract
*.Multiply
/.Divide
Enter the choice you want
/
Remainder is 0.5

*/

-------------------------------------------------------------------------------------/*AIM : To display the following pattern :


*
***
*****
***

*/

import java.io.*;
class Pattern1
{public static void main(String [] args)throws IOException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,n;
System.out.print("Enter the No. of stars in middle line : ");

n=Integer.parseInt(br.readLine());
n=n/2+1;
for(i=1;i<=n;i++)
{
for(j=n-i;j>0;j--)
System.out.print(" ");
for(j=2*i-1;j>0;j--)
System.out.print("*");
System.out.println();
}
for(i=2;i<=n;i++)
{
for(j=i-1;j>0;j--)
System.out.print(" ");
for(j=2*(n-i)+1;j>0;j--)
System.out.print("*");
System.out.println();
}
}
}
/*

OUTPUT :

Enter the No. of stars in middle line : 5


*
***
*****
***

*/

------------------------------------------------------------------------------------/*

AIM : To print following pattern :


A
ABA
ABCBA
ABCDCBA

*/

class Pattern2
{public static void main(String args[])
{ int i,j,k;
int n=Integer.parseInt(args[0]);
for(i=0;i<n;i++)
{

for(j=n-1;j>=i;j--)
System.out.print(" ");
for(k=65;k<(65+i);k++)
System.out.print((char)(k));
for(;k>=65;k--)
System.out.print((char)(k));
System.out.println();

}
}
}
/*

OUTPUT :

D:\OOPM>java Pattern2 4
A
ABA
ABCBA
ABCDCBA

*/

------------------------------------------------------------/* AIM : WAP To check whether the entered character is lowercase or uppercase, numeric or
symbolic.

*/

import java.io.*;
class CharCheck
{
public static void main(String args[]) throws IOException
{
char c;
System.out.println("Enter the character:");
c=(char)System.in.read();
if((c>='a') && (c<='z'))
System.out.println("it is lowercase");

else if((c>='A') && (c<='Z'))


System.out.println("it is uppercase");
else if((c>='0') && (c<='9'))
System.out.println("it is numeric");
else
System.out.println("it is a symbol");
}
}
/*

OUTPUT

Enter the character:


a
it is lowercase
Enter the character:
A
it is uppercase
Enter the character:
9
it is numeric
Enter the character:
/
it is a symbol

*/

---------------------------------------------------------------------------/*

AIM : To find factorial of given number using recursion.

import java.io.*;
class Factorial
{public static void main(String args[]) throws IOException
{ DataInputStream din = new DataInputStream(System.in);
System.out.print("Enter n = ");
int n = Integer.parseInt(din.readLine());
int fact = factorial(n);
System.out.print("Factorial of "+n+" = "+fact);
}
public static int factorial(int n)
{ if(n==0||n==1)

*/

return 1;
else

return n*factorial(n-1);

}
}
/*

OUTPUT :

Enter n = 4
Factorial of 4 = 24

*/

--------------------------------------------------------------------------------------/*

AIM : WAP to sort integers using bubble sort algorithm.

*/

import java.io.*;
class BubbleSort
{public static void main(String args[]) throws IOException
{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter array size : ");
int n = Integer.parseInt(buf.readLine());
int a[] = new int[n];
System.out.println("Enter array elements : ");
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(buf.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{

int temp = a[j];


a[j] = a[j+1];
a[j+1] = temp;

}
}
}
System.out.println("\nSorted array elements : ");
for(int i=0;i<n;i++)
System.out.print(" "+a[i]);

}
}
/*

OUTPUT :

Enter array size : 5


Enter array elements :
34
21
54
23
6
Sorted array elements : 6 21 23 34 54 */
---------------------------------------------------------------------------/*AIM : To implement sequential search algorithm.

*/

import java.io.*;
class LinearSearch
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no.of elements : ");
int n = Integer.parseInt(obj.readLine());
int a[] = new int[n];
System.out.println("Enter array elements : ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(obj.readLine());
System.out.print("Enter element to be searched : ");
int ele=Integer.parseInt(obj.readLine());
int i=0;
boolean flag=false;
for(i=0;i<n;i++)
{
if (a[i]==ele)
{
flag=true;

break;
}
else
flag=false;
}
if(flag)
System.out.println("Element Found at index : "+i);
else

System.out.println("Element not found");

}
}
/*

OUTPUT :

Enter no.of elements : 4


Enter array elements :
23
14
5
32
Enter element to be searched : 8
Element not found
Enter no.of elements : 4
Enter array elements :
43
13
4
32
Enter element to be searched : 13
Element Found at index : 1

*/

-------------------------------------------------------------------/*AIM : The annual examination results of 5 students are tabulated as


RollNo.

Subject1

Subject2

Subject3

Write a program to read the data and determine:


(i)Total marks obtained by each student
(ii)The student who obtained the highest total marks. */
import java.io.*;

follows :-

class StudentRecord
{
public static void main(String args[]) throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Enter the no. of students : ");
int n = Integer.parseInt(ob.readLine());
int student[][]=new int[n][5];
int i,j,total_marks,max=0,m=0;
for(i=0;i<n;i++)
{
System.out.print("\nEnter the roll no. : ");
student[i][0]=Integer.parseInt(ob.readLine());
total_marks=0;
for(j=1;j<=3;j++)
{
System.out.print("Enter marks of sub"+j+" = ");
student[i][j]=Integer.parseInt(ob.readLine());
total_marks=total_marks+student[i][j];
}
student[i][4]=total_marks;
}
System.out.println("\nThe marks of students are : ");
System.out.println("Roll\tSub1\tSub2\tSub3\tTotal");
for(i=0;i<n;i++)
{
for(j=0;j<5;j++)
{
System.out.print(student[i][j]+"\t");
}
System.out.println();
}

for(i=0;i<n-1;i++)
{
if (student[i][4]<student[i+1][4])
{
max=student[i+1][4];
m = student[i+1][0];
}
}
System.out.println("\nHighest total marks are "+max+" obtained by Roll No. "+m);
}
}
/*

OUTPUT :

Enter the no. of students : 3


Enter the roll no. : 1
Enter marks of sub1 = 23
Enter marks of sub2 = 22
Enter marks of sub3 = 12
Enter the roll no. : 2
Enter marks of sub1 = 32
Enter marks of sub2 = 19
Enter marks of sub3 = 32
Enter the roll no. : 3
Enter marks of sub1 = 14
Enter marks of sub2 = 23
Enter marks of sub3 = 21
The marks of students are :
Roll

Sub1

Sub2

Sub3 Total

23

22

12

57

32

19

32

83

14

23

21

58

Highest total marks are 83 obtained by Roll No. 2

*/

---------------------------------------------------------------------------------------/*AIM : Write an object oriented program to arrange names of students in descending order

of their total marks.


Input data consists of student details such as name, Id no and marks obtained in
Mathematics, Physics and Chemistry. Use the concept of array of objects.*/
import java.io.*;
class Marks
{
int std_id;
String name;
double math,chem,phy,total;
void getData()throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("Enter the id & name of student : ");
std_id=Integer.parseInt(ob.readLine());
name=ob.readLine();
System.out.println("Enter marks of phy, chem & maths : ");
math=Double.parseDouble(ob.readLine());
chem=Double.parseDouble(ob.readLine());
phy=Double.parseDouble(ob.readLine());
}
void processData()
{
total=math+chem+phy;
}
void displayData()
{
System.out.println(std_id+"\t"+name+"\t"+total);
}
}
class Student
{
public static void main(String args[])throws IOException
{
int i,j;

DataInputStream ob=new DataInputStream(System.in);


System.out.print("Enter the no. of students : ");
int n = Integer.parseInt(ob.readLine());
Marks s[]=new Marks[n];
for(i=0;i<n;i++)
{
s[i]= new Marks();
s[i].getData();
s[i].processData();
}
Marks temp=new Marks();
for(i=0;i<n;i++)
{
for( j=0;j<n-1;j++)
{
if (s[j].total<s[j+1].total)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
System.out.println("Details of the students are : \n");
System.out.println("I.D.\tName\tTotal");
for(i=0;i<n;i++)
{
s[i].displayData();
}
}
}
/*OUTPUT
Enter the no. of students : 3
Enter the id & name of student :

1
abc
Enter marks of phy, chem & maths :
23
23
23
Enter the id & name of student :
2
xyz
Enter marks of phy, chem & maths :
12
21
13
Enter the id & name of student :
3
lmn
Enter marks of phy, chem & maths :
31
21
32
Details of the students are :
I.D.

Name Total

lmn

84.0

abc

69.0

xyz

46.0

*/

-----------------------------------------------------------------------------/*AIM : Write a program to check whether a given string is Palindrome or not. */


import java.io.*;
class Palindrome
{
public static void main(String args[]) throws IOException
{
String str;
DataInputStream ds=new DataInputStream(System.in);

System.out.println("Enter a String: ");


str=ds.readLine();
int i,j,len = str.length();
boolean flag=true;
System.out.println("\nString : "+str);
for(i=0,j=len-1;i<=len/2;i++,j--)
{
if(str.charAt(i)==str.charAt(j))
flag=true;
else
{
flag=false;
break;
}
}
if(flag==true)
System.out.println("String is palindrome");
else
System.out.println("String is not palindrome");
}
}
/*OUTPUT
Enter a String: MALAYALAM
String : MALAYALAM
String is palindrome

*/

------------------------------------------------------------------------/*AIM : WAP to capitalize the first character of each word from a given sentence.
(Example: all the best All The Best)*/
import java.io.*;
class CapitalFirstChar
{public static void main(String args[]) throws IOException
{
String s=null;
DataInputStream in = new DataInputStream(System.in);

System.out.println("Enter a sentence : ");


s = in.readLine();
s =Character.toUpperCase(s.charAt(0)) + s.substring(1);
for(int i =0;i<s.length();i++)
{
if(s.charAt(i) == ' ')
s=s.substring(0,i+1) + Character.toUpperCase(s.charAt(i+1)) + s.substring(i+2);
}
System.out.println(s);
}
}
/* OUTPUT :
Enter a sentence
all the best
All The Best */
---------------------------------------------------------------------/*AIM : WAP to count no. of uppercase ,lowercase letters, & no. of digits and blank spaces in
a given string.*/
import java.io.*;
class StringCheck
{public static void main(String args[]) throws IOException
{ DataInputStream dis = new DataInputStream(System.in);
System.out.print("Enter string : ");
String str = dis.readLine();
int l=0,u=0,n=0,s=0;
for(int i=0;i<str.length();i++)
{

if(Character.isUpperCase(str.charAt(i)))
u++;
else if(Character.isLowerCase(str.charAt(i)))
l++;
else if(Character.isDigit(str.charAt(i)))
n++;
else if(Character.isSpace(str.charAt(i)))
s++;

}
System.out.print("\nUpper case = "+u+"\nLower case = "+l+"\nDigits = "+n+"\nBlank
Spaces = "+s);
}
}
/*OUTPUT :
Enter string : HI.. 123 how are you?
Upper case = 2
Lower case = 9
Digits = 3
Blank Spaces = 4

*/

----------------------------------------------------------------------------------/* AIM : Write a program to accept student names from the command line & store them in a
vector.

*/

import java.util.*;
class Vector_Demo
{public static void main(String args[])
{
Vector v = new Vector(4,2);
System.out.println("Intial size is: " +v.size());
System.out.println("Intial capacity is: " + v.capacity());
for(int i =0; i<args.length; i++)
{

v.addElement(new String(args[i])); }

Enumeration e = v.elements();
System.out.println("\nElements in vector:");
while(e.hasMoreElements())
{

System.out.println(e.nextElement()+ " ");

}
System.out.println("Final size is: " +v.size());
System.out.println("Final capacity is: " + v.capacity());
}
}
/* OUTPUT
D:\OOPM>java Vector_Demo deep ramesh komal raj neel

Intial size is: 0


Intial capacity is: 4
Elements in vector:
deep
ramesh
komal
raj
neel
Final size is: 5
Final capacity is: 6

*/

---------------------------------------------------------------------------------------------/*AIM : Write a Program to accept and display the month number. Throw an exception if
improper month number is entered. Make your own exception class to handle this exception.
*/
import java.io.*;
class NumberRangeException extends Exception
{

String msg;
NumberRangeException()
{
msg = new String("Exception : Enter a number between 1 and 12");
}

}
class My_Exception
{
public static void main (String args []) throws IOException
{
try{

BufferedReader obj = new BufferedReader(new

InputStreamReader(System.in));
System.out.print("Enter month number : ");
int x = Integer.parseInt(obj.readLine());
if (x >= 1 && x <=12)
System.out.println(x+" is a valid month number");
else throw new NumberRangeException();

}
catch (NumberRangeException e)
{
System.out.println (e.msg);
}
}
}
/*OUTPUT :
D:\OOP>java My_Exception
Enter month number : 33
Exception : Enter a number between 1 and 12
D:\OOP>java My_Exception
Enter month number : 6
6 is a valid month number

*/

--------------------------------------------------------------------------------/*AIM : 1.

Consider a class network given. The class 'Admin' derives information from

the class 'Account' which in turn derives information from the 'Person' class. Write a program
to display 'Admin' object.*/
class Person

//super class

{
protected String name;
protected int code;
Person(String n, int c)
{
name=n;
code=c;
}
public void display()
{
System.out.println("Name of person is: "+name+"\nCode is: "+code);
}
}
class Account extends Person
{

//intermediate super class

protected int pay;


Account(String n, int c, int p)
{
super(n,c);
pay=p;
}
public void display()
{
super.display();
System.out.println("Salary : "+pay);
}
}
class Admin extends Account

//sub class

{
protected int exp;
Admin(String n,int c,int p,int e)
{
super(n,c,p);
exp=e;
}
public void display()
{
super.display();
System.out.println("Experience : "+exp);
}
}
class CompanyRecords
{
public static void main( String args[])
{
Admin m=new Admin("ABC",1001,10000,5);
m.display();
}
}

/* OUTPUT :
Name of person is: ABC
Code is: 1001
Salary : 10000
Experience : 5 */
------------------------------------------------------------------/*AIM : Write a program to display volume of sphere and hemisphere. Make use of abstract
class.

*/

import java.util.Scanner;
abstract class Calculate
{
protected double r;
protected double pi = 3.14;
abstract double volume();
}
class Sphere extends Calculate
{
double volume()
{
return((4.0/3.0) * pi * r* r * r);
}
}
class Hemisphere extends Calculate
{
double volume()
{
return((2.0/3.0) * pi * r * r * r);
}
}
class Volume
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

Sphere s = new Sphere();


Hemisphere h = new Hemisphere();
System.out.print("Enter the radius of sphere : ");
s.r = sc.nextDouble();
System.out.println("Volume of sphere is : " +s.volume());
System.out.print("\nEnter the radius of hemisphere : ");
h.r= sc.nextDouble();
System.out.println("Volume of hemisphere is : " +h.volume());
}
}
/* OUTPUT :
Enter the radius of sphere : 3.2
Volume of sphere is : 137.18869333333336
Enter the radius of hemisphere : 2.4
Volume of hemisphere is : 28.938239999999997

*/

---------------------------------------------------------------------------------------------/*AIM :Interface Matrix


{
final static int M=5, N=5; //Matrix indices
void readMatrix(); //Read a Matrix
void displayMatrix(); //Display a Matrix
void addMatrix(); // Add two Matrix
void multMatrix(); //Multiply two matrix
void transposeMatrix() //Transpose of Matrix
}
Implement the above interface using a suitable JAVA class program and also develop the
main program.

*/

import java.util.Scanner;
interface Matrix
{
final static int M=5, N=5;
public void readMatrix();
public void displayMatrix();
public void addMatrix();

public void multMatrix();


public void transposeMatrix();
}
class MatrixOperations implements Matrix
{
int x[][]=new int[3][3];
int y[][]=new int[3][3];
int z[][]=new int[3][3];
public void readMatrix()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the elements for Matrix 1 : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
x[i][j]=sc.nextInt();
}
}
System.out.println("Enter the elements for Matrix 2 : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
y[i][j]=sc.nextInt();
}
}
}
public void displayMatrix()
{
System.out.println("Displaying Matrix 1 : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)

{
System.out.print(x[i][j]+ " ");
}
System.out.println();
}
System.out.println("Displaying Matrix 2 : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(y[i][j]+ " ");
}
System.out.println();
}
}
public void addMatrix()
{
System.out.println("Displaying Addition of Matrices : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
z[i][j]=x[i][j]+y[i][j];
System.out.print(z[i][j]+ " ");
}
System.out.println();
}
}
public void multMatrix()
{
System.out.println("Displaying Multiplication of Matrices :");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)

z[i][j]=0;
for(int k=0;k<3;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
System.out.print(z[i][j]+ " ");

}
System.out.println();
}
}
public void transposeMatrix()
{
System.out.println("Displaying Transpose of Matrix 1 : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(x[j][i]+ " ");
}
System.out.println();
}
}
}
class MatrixDemoInterface
{
public static void main(String args[])
{
MatrixOperations a =new MatrixOperations();
a.readMatrix();
a.displayMatrix();
a.transposeMatrix();
a.addMatrix();
a.multMatrix();
}

}
/*OUTPUT
Enter the elements for Matrix 1 :
1
2
1
3
3
2
2
1
5
Enter the elements for Matrix 2 :
3
2
4
1
2
6
2
1
1
Displaying Matrix 1 :
1 2 1
3 3 2
2 1 5
Displaying Matrix 2 :
3 2 4
1 2 6
2 1 1
Displaying Transpose of Matrix 1 :
1 3 2
2 3 1
1 2 5

Displaying Addition of Matrices :


4 4 5
4 5 8
4 2 6
Displaying Multiplication of Matrices :
7 7 17
16 14 32
17 11 19

*/

-----------------------------------------------------------------------------/* AIM : Write an applet to display the following :

*/
// face.html
<html>
<applet CODE = "FaceExample.class" WIDTH=200 HEIGHT=150>
</applet>
</html>
// FaceExample.java
import java.awt.*;
import java.applet.*;
public class FaceExample extends Applet
{
public void paint(Graphics g)
{
g.drawOval(20,20,70,30);
g.drawOval(110,20,70,30);
g.drawLine(100,50,100,80);
g.drawLine(70,100,130,100);
}
}
OUTPUT :
D: \OOP>javac FaceExample.java

D:\OOP>appletviewer face.html

---------------------------------------------------------------------------------------/*AIM : Write a program to design & import a package. */


// Hello.java ( Inside folder named world )
package world;
public class Hello
{
public void display()
{
System.out.println("Hello !!! This is inside package : world ");
}
}
// HelloDemo.java
import world.*;
class HelloDemo
{
public static void main(String args[])
{
Hello h=new Hello();
h.display();
}
}
/*OUTPUT :
Hello !!! This is inside package : world

*/

-----------------------------------------------------------------------------/*AIM : Write a program to print 1A2B3C4D5E6F7G8H9I10J using two child threads.


*/
class A extends Thread
{
public void run()

{
for(int i=1;i<=10;i++)
{

System.out.print(i);
try{
Thread.sleep(500);
}
catch(Exception e){}

}
}
}
class B implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{

System.out.print((char)(i+64));
try{
Thread.sleep(550);
}
catch(Exception e){}

}
}
}
class Multithreading
{
public static void main(String args[])
{
A a =new A();
a.start();
B b =new B();
Thread t=new Thread(b);
t.start();
}
}
/*OUTPUT :

1A2B3C4D5E6F7G8H9I10J

*/

Anda mungkin juga menyukai