Sie sind auf Seite 1von 34

WAP to print features of java through command line.

class comline
{
public static void main(String args[])
{
int i=0,count;
String mystring;
count =args.length;
System.out.println("Number of arguement= "+count);
while(i<count)
{
mystring=args[i];
i=i+1;
System.out.println(i+":"+"Java is"+mystring+"!");
}
}
}

Output:

WAP to print sum and reverse of n digit no.


class sumrev
{
public static void main(String args[])
{
int n,r=0,a,s=0;
n=Integer.parseInt(args[0]);
while(n!=0)
{
a=n%10;
n=n/10;
s=s+a;
r=r*10+a;
}
System.out.println("Sum= "+s+"\n Reverse= "+r);
}
}

Output:

WAP to find lcm and gcd of any no.


class lcmgcd
{
public static void main(String args[])
{int n,m,p,a,q,gcd,lcm;
m=Integer.parseInt(args[0]);
n=Integer.parseInt(args[1]);
p=m;
q=n;
if((m==0)||(n==0))
{
gcd=0;
lcm=0;
}
while(n!=0)
{
a=m%n;
m=n;
n=a;
}
gcd=m;
lcm=(p*q)/gcd;
System.out.println("GCD="+gcd+"\n LCM="+lcm); }
Output:

WAP to print the no is prime or not.


class prime
{
public static void main(String args[])
{
int p=1,n,a;
n=Integer.parseInt(args[0]);
for(a=2;a<n;a++)
{
if(n%a==0)
{
p=0;
break;
}
}
if(p==1)
System.out.println("no is prime");
else
System.out.println("no is not prime");
}
}
Output:

WAP to find prime no series between 1 to n no.


class primeno
{
public static void main(String args[])
{
int p=1,n,a,r;
n=Integer.parseInt(args[0]);
for(r=1;r<n;r++)
{
p=1;
for(a=2;a<r;a++)
{
if(r%a==0)
{
p=0;
break;
}
}
if(p==1)
System.out.println("Prime no="+r);
}}}
O}utput:
WAP to print factorial of no.
class fact
{
public static void main(String args[])
{
int i,fact=1,n;
n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial= "+fact);
}
}
Output:

WAP to state whether the no is Armstrong or not.


public class Armstrong
{
public static void main(String[] args)
{ int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 1 to 1000:");
for(int i = 1; i <= 1000; i++)
{ n = i;
while(n > 0)
{ b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
System.out.print(i+" ");
sum = 0;
}} }
Output:

WAP to print factorial series.


class facts
{
public static void main(String args[])
{
int i,fact=1,n;
n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
System.out.println("Factorial= "+fact);
}
}
}
Output:

EXP NO:02
Q1.WAP TO DISPLAY THE NAME OF THE PLAYERS WHO PLAYED MORE TEST MATCHES GIVEN THE
NAMES & NUMBER OF TEST MATCHES PLAYED OF 3 PLAYERS.
import java.io.*;
class player
{
String a;
int n;
void getdata()
{
BufferedReader stdin= new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter name:");
a=stdin.readLine();
System.out.println("enter no.of test matches:");
n=Integer.parseInt(stdin.readLine());
}
catch(Exception e){}
}
void putdata()
{
System.out.println("RAIS ZOYA JALIL");
System.out.println("\n name of player who played more test matches that is
"+n+"::"+a);
}
}
class play1
{
public static void main(String arg[])
{
player p[ ]= new player[3];
for(int i=0;i<3;i++)
p[i]=new player();
for(int i=0;i<3;i++)
p[i].getdata();
int i=0;
if(p[i].n>p[i+1].n && p[i].n> p[i+2].n)
p[i].putdata();
else if((p[i+1].n>p[i].n) && (p[i+1].n> p[i+2].n))
p[i+1].putdata();
else
p[i+2].putdata();
}
}

OUTPUT:

Q2. DEVELOP A PROGRAM TO DO THE ADDITION OF GIVEN TWO NUMBERS IF BOTH ARE GREATER
THAN 10.

class exp2
{
public static void main(String arg[])
{
int a,b,c;
System.out.println("RAIS ZOYA JALIL");
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[1]);
if(a>10 && b>10)
{
c=a+b;
System.out.println("Addition of two numbers:"+c);
}
else
{
System.out.println("Numbers are not greater than 10!!!");
}
}
}

OUTPUT:

Q3. DEVELOP A PROGRAM TO PRINT THE MULTIPLES OF 5 BETWEEN 20 AND 70.

class multi
{
public static void main(String arg[])
{
int i;
int res;
System.out.println("RAIS ZOYA JALIL");
System.out.println("multiples of 5 between 20 to 70");
for(i=4;i<=14;i++)
{
res=5*i;
System.out.println(+res);
}
}
}

OUTPUT:

Q4.WAP TO PRINT THE FIRST SEVEN 7 TERMS OF THE FIBONNACCI SERIES.

class fibo1
{
public static void main(String arg[])
{
int n=7;
int a=0,b=1,c;
System.out.println("SHEHREEN");
System.out.println(+a+"\n"+b);
for(int i=3;i<=n;i++)
{
c=a+b;
System.out.println("\n" +c);
a=b;
b=c;
}
}
}

Q5.WAP TO FIND THE NAME OF THOSE COMPANIES WHICH HAVE MORE THAN 10000 EMPLOYEES
GIVEN THE NAMES & NUMBER OF EMPLOYEES OF 4 COMPANIES.
import java.io.*;
class employee
{
String name;
int no;
void getdata()
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter company name:");
name=stdin.readLine();
System.out.println("enter no of employees:");
no=Integer.parseInt(stdin.readLine());
}
catch(Exception e){}
}
void putdata()
{
System.out.println("company name:"+name);
}
}
class emp2
{
public static void main(String arg[])
{
employee e[]=new employee[4];
for(int i=0;i<4;i++)
e[i]=new employee();
for(int i=0;i<4;i++)
e[i].getdata();
System.out.println("RAIS ZOYA JALIL");
for(int i=0;i<4;i++)
{
if(e[i].no>10000)
e[i].putdata();
}
}
}

OUTPUT:

Q6 . WRITE A PROGRAM TO DISPLAY SUM AND AVERAGE OF �n� NUMBERS USING COMMAND LINE.

class q1
{
public static void main(String arg[])
{
intr,n;
float sum=0,avg;
n=Integer.parseInt(arg[0]);
for(int i=1;i<=n;i++)
{
r=Integer.parseInt(arg[i]);
sum=sum+r;
}
avg=sum/n;
System.out.println("RAIS ZOYA JALIL");
System.out.println("sum:"+sum+" average:"+avg);
}
}

OUTPUT:
Q7. WRITE A PROGRAM TO DISPLAY �LCM� AND �GCD� OF 2 NON-NEGATIVE INTEGER NUMBER.

classlcmgcd
{
public static void main( String arg[])
{
intm,n,a,b,s,gcd,lcm;
m=Integer.parseInt(arg[0]);
n=Integer.parseInt(arg[1]);
a=m;
b=n;
if(m==0||n==0)
gcd=lcm=0;
else
{
while(n!=0)
{
s=m%n;
m=n;
n=s;
}
}
gcd=m;
lcm=(a*b)/gcd;
System.out.println("RAIS ZOYA JALIL");
System.out.println("lcm:"+lcm);
System.out.println("gcd:"+gcd);
}
}

OUTPUT:

Q8. WRITE A PROGRAM TO CHECK WHETHER THE ENTERED NUMBER IS EVEN OR ODD.

classevenodd
{
public static void main(String arg[])
{
int n;
n=Integer.parseInt(arg[0]);
System.out.println("RAIS ZOYA JALIL");
if((n%2)==0)
System.out.println("Number is even.");
else
System.out.println("Number is odd.");
}
}

OUTPUT:

Q9. WRITE A PROGRAM TO DISPLAY SUM OF ALL ODD AND EVEN NUMBERS BETWEEN 1 TO 100.

classeror
{
public static void main(String arg[])
{
inter=0,or=0;
System.out.println("RAIS ZOYA JALIL");
for(int i=1;i<=100;i++)
{
if((i%2)==0)
er=er+i;
else
or=or+i;
}
System.out.println("sum of even result:"+er);
System.out.println("sum of odd result:"+or);
}
}

OUTPUT:
Q10. WRITE A PROGRAM TO DISPLAY THE FACTORIAL OF ANY NUMBER
ENTERED BY THE USER.

class fact
{
public static void main(String arg[])
{
intn,fact=1;
n=Integer.parseInt(arg[0]);
System.out.println("RAIS ZOYA JALIL");
for(int i=1;i<=n;i++)
fact=fact*i;
System.out.println("Factorial of number:"+fact);
}
}

OUTPUT:

Q11. WRITE A PROGRAM TO DISPLAY THE FACTORIAL SERIES BETWEEN 1 TO 15.

class fact
{
public static void main(String arg[])
{
int fact=1;
System.out.println("RAIS ZOYA JALIL");
for(int n=1;n<=15;n++)
{
fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
System.out.println("Factorial of "+n+":"+fact);
}
}
}

OUTPUT:

Q12. WRITE A PROGRAM TO DISPLAY FIBONACCI SERIES OF ANY NUMBER ENTERED BY THE USER.

class fibo
{
public static void main(String arg[])
{
int a=0,b=1,c,n;
n=Integer.parseInt(arg[0]);
System.out.println("RAIS ZOYA JALIL");
System.out.print(+a);
System.out.print(" "+b);
for(int i=2;i<n;i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}

OUTPUT:
Q13. WRITE A PROGRAM TO DISPLAY SUM AND REVERSE OF N DIGIT NUMBER ENTERED BY THE
USER.

class rev
{
public static void main(String arg[])
{
int rem=0,sum=0,n,m,rev=0,rem1;
n=Integer.parseInt(arg[0]);
m=Integer.parseInt(arg[1]);
System.out.println("RAIS ZOYA JALIL");
while(n!=0)
{
rem=n%10;
n=n/10;
sum=sum+rem;
}
System.out.println("sum of n digit number:"+sum);
while(m>0)
{
rem1=m%10;
m=m/10;
rev=rev*10+rem1;
}
System.out.println("reverse of n digit number:"+rev);
}
}

OUTPUT:

Q14. WRITE A PROGRAM TO DISPLAY THE GIVEN YEAR IS LEAP YEAR OR NOT.

class leap
{
public static void main(String arg[])
{
int n;
n=Integer.parseInt(arg[0]);
System.out.println("RAIS ZOYA JALIL");
if((n%4==0) && (n%100!=0) || (n%400==0))
System.out.println("given year is leap year");
else
System.out.println("given year is not leap year");
}
}
OUTPUT:

Q15. WRITE A PROGRAM TO DISPLAY SUM OF ALL ODD AND EVEN NUMBERS BETWEEN 1 TO 100.

Class eror
{
public static void main(String arg[])
{
inter=0,or=0;
System.out.println("RAIS ZOYA JALIL");
for(int i=1;i<=100;i++)
{
if((i%2)==0)
er=er+i;
else
or=or+i;
}
System.out.println("sum of even result:"+er);
System.out.println("sum of odd result:"+or);
}
}

OUTPUT:
Q16. WRITE A PROGRAM TO DISPLAY THE GIVEN NUMBER IS PRIME NUMBERS FROM 1 TO 100.

class ppp
{
public static void main(String arg[])
{
int num,n,div,p=1;
System.out.println("RAIS ZOYA JALIL");
for(n=1;n<=100;n++)
{
p=1;
for(div=2;div<n;div++)
{
if((n%div)==0)
{
p=0;
break;
}
}
if(p==1)
System.out.print(+n+"\n");
}
}
}
OUTPUT:

import java.io.*;
class point{
int x,y;
void getdata()throws IOException{
System.out.println("please enter X and Y co-ordinate of a point");
DataInputStream a=new DataInputStream(System.in);
x=Integer.parseInt(a.readLine());
y=Integer.parseInt(a.readLine());
}
void putdata()throws IOException{
System.out.println("X CO-ORDINATE="+x+"\n Y CO-ORDINATE="+y);
}
public static void main(String args[])throws IOException{
int i=0;
point p1[]=new point[5];
for(i=0;i<5;i++)
p1[i]=new point();
for(i=0;i<5;i++)
p1[i].getdata();
for(i=0;i<5;i++)
p1[i].putdata(); }};
Q.2.Program to accomplish the following task using String and StringBuffer class:
a) To search a word inside a string

b) To search the last position of a substring

c) To compare two string

d) To print reverse of string

import java.io.*;
class main{
public static void main(String arg[]){
String s="";
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter the String : ");
s=stdin.readLine();
System.out.println("Enter the String you want to search :");
String s1=stdin.readLine();
if(s.indexOf(s1)>0)
System.out.println("String "+s1+" is present");
else
System.out.println("String is absent");
System.out.println("Enter a string to find last index of Substring:");
String s3=stdin.readLine();
System.out.println("The last index of string ="+s.lastIndexOf(s3));
System.out.println("Enter two strings to be compared: ");
String s4=stdin.readLine();
String s5=stdin.readLine();
if(s4.compareToIgnoreCase(s5)==0)
System.out.println("String are equal.");
else
System.out.println("Strings are not equal");
StringBuffer s6=new StringBuffer(s);

System.out.println("The reverse string of "+s6+" is :- "+s6.reverse());}


catch(Exception e){}
}
}
Q.1.WRITE A PROGRAM IN JAVA TO PERFORM SORT THE STRINGS
import java.util.*;
import java.io.*;
class Exp4_2{
public static void main(String args[]) {
String s1="", s2="";
int i, j, length;
DataInputStream in = new DataInputStream(System.in);
try{
System.out.println("Enter a string to print all substrings");
s1= in.readLine();
}
catch (Exception e){}
length = s1.length();
System.out.println("Substrings of "+s1+" are :-");
for( i= 0 ; i < length ; i++ ) {
for( j = 1 ; j<= length - i ; j++ ) {
s2 = s1.substring(i, i+j);
System.out.println(s2);}}}
}

EX.1.WAP TO FIND WHETHER THE ENTERED STRING IS PALINDROME OR NOT.


import java.io.*;
class Exp4_3{
public static void main(String arg[]){
String s1="";
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter any string:");
s1=in.readLine();
}catch(Exception e){}
int n=s1.length();
char ch1,ch2;
int p=1,i,j=n-1;
for(i=0;i<n;i++){
ch1=s1.charAt(i);
ch1=Character.toUpperCase(ch1);
ch2=s1.charAt(j);
ch2=Character.toUpperCase(ch2);
j--;
if(ch1!=ch2){
p=0;
break;}}
if(p==1)
System.out.println("The Entered String "+s1+" is a Plaindrome String");
else
System.out.println("The Entered String "+s1+" is not a Plaindrome String");}}
EX.2 . WRITE A PROGRAM IN JAVA TO PERFORM SORTING ON DIFFERENT STRINGS.
import java.io.*;
class sot{
int n;
String a[]=new String[20];
void accept(){
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter the Number of Names you want to enter:");
n=Integer.parseInt(in.readLine());
System.out.println("Enter Names you want to Sort:");
for(int i=0;i<n;i++)
a[i]=in.readLine();
}
catch(Exception e){}
}
void display(){
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i].compareTo(a[j])>0){
String temp=a[i];
a[i]=a[j];
a[j]=temp;}
}
}
System.out.println("*****************");
System.out.println("After sorting the names:");
for(int i=0;i<n;i++)
System.out.println(a[i]);}
}

class Exp4_4
{
public static void main(String arg[])
{
sot s1=new sot();
s1.accept();
s1.display();
}
}

EX.3.IMPLEMENT THE JAVA PROGRAM USING String / StringBuffer.


i. ACCEPT A PASSWORD FROM USER.

ii. CHECK IF PASSWORD IS CORRECT THEN DISPLAY "GOOD PASSWORD" ELSE DISPLAY
"INCORRECT PASSWOR".

iii. 'append' THE PASSWORD WITH THE STRING "Welcome to Java!!!".

iv. DISPLAY PASSWORD IN 'reverse' ORDER.


v. 'replace' THE CHARACTER '!' IN PASSWORD WITH '*' CHARACTER.

import java.io.*;
public class Exp4_6{
public static void main(String []args) {
String sb1="";
String sb2="";
StringBuffer sb=new StringBuffer();
int con=0,vow=0,dig=0,s=0,t=0;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter password:--\t");
sb1=in.readLine();
StringBuffer sb3=new StringBuffer(sb1);
System.out.println("Re-enter password:--\t");
sb2=in.readLine();
sb=sb3;
}catch(Exception e){}
if(sb1.equals(sb2))
System.out.println("Good Passowrd");
else
System.out.println("WRONG Passowrd");
sb.append(" Welcome To Java!!!");
System.out.println(" "+sb);
StringBuffer p=new StringBuffer(sb1);
System.out.println("Reverse of Password"+p.reverse());
for (int index = 0; index < sb.length(); index++) {
if (sb.charAt(index) == '!') {
sb.setCharAt(index, '*'); }

}
System.out.println("Replace ! with * "+sb);
}
}

EX.4.WAP IN JAVA WHICH WILL ENTER THE LINE OF TEXT AS AN I/O AND PRINT NUMBER OF
VOWELS, CONSONANTS, DIGITS, SPACES &amp; TABS.
import java.io.*;
public class Exp4_5{
public static void main(String []args) {
String sb1="";
int con=0,vow=0,dig=0,s=0,t=0;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter text:--\t");
sb1=in.readLine();
}catch(Exception e)
{System.out.println(e);}
StringBuffer sb=new StringBuffer(sb1);
for (int i=0;i<sb.length();i++) {
char ch1=sb.charAt(i);
char ch=Character.toUpperCase(ch1);
if(ch=='A'||ch=='I'||ch=='O'||ch=='E'||ch=='U')
vow++;
else if(ch >= '0' && ch <= '9')
dig++;
else if(ch==' ')
s++;
else if(ch=='\t')
t++;
else
con++;
}
System.out.print("Text of line contain\n");
System.out.print("Vowel=\t"+vow+"\nCon
=\t"+con+"\nDigit=\t"+dig+"\nWhiteS=\t"+s+"\nTab =\t"+t+"\n");
}
}

EX.5.WAP IN JAVA TO ACCEPT STRING FROM USER & COUNT ALL THE OCCURRENCES OF A
PARTICULAR WORD IN A STRING
import java.io.*;
class Exp4_6{
public static void main(String arg[]){
DataInputStream in=new DataInputStream(System.in);
String base,search;
try{
System.out.println("Enter the String:");
base=in.readLine();
System.out.println("Enter the String you want to search:\t\t");
search=in.readLine();
int len=search.length();
int result=0;
if(len>0){
int start=base.indexOf(search);
while(start!=-1){
result++;
start=base.indexOf(search,start+len);
}
}
System.out.println("\n Occurence of word "+search+" in String is:"+result);
}
catch(Exception e){}
}
}
}

Q1: WAP to implement a vector that accepts 5 elements from the command line and
store them in the vector and display the objects stored in a vector.

import java.util.*;
public class Exp5_1
{
public static void main(String arg[])
{
Vector list=new Vector();
System.out.println(" Vector implemented: ");
for(int i=0;i<arg.length;i++)
list.addElement(arg[i]);
System.out.println(" Current vector: " +list);
System.out.println(" Vector length after initialization: "+list.size());
}
}

Q2:implement a program to accomplish the following task using vector class :


i)to add two integer ,two floats.two character,two string obj
ii)to search a particular object of the vector .
iii) to display all obj along with their index position.
iv) to delete all objects from vector.

import java.util.*;
public class Exp5_2{
public static void main(String a[]){
Vector list=new Vector();
boolean b;
System.out.print("Initializing vector....\n");
for(int i=0;i<2;i++)
list.addElement(new Integer(i+1));
list.addElement(new Float(21.78));
list.addElement(new Float(25.90));
list.addElement("A");
list.addElement("B");
list.addElement(new String ("NMA"));
list.addElement(new String ("JAVA"));
System.out.println("Starting Vector: " +list);
System.out.println("Vector size: " +list.size());
System.out.println("the object JAVA is"+list.contains("JAVA"));
for(int i=0;i<list.size();i++)
System.out.println("element at index"+i+"\t"+list.elementAt(i));
list.clear();
System.out.println(list);
System.out.println("Vector size: " +list.size());
}
}

Q3:WAP to create and store 5 integer objects, 3 string objects, 2 char objects, 2
float objects such that to accomplish the following tasks:
1. To add object at the end of vector

2. To remove 2nd object from vector

3. To search for particular object

4. To display first and last element of the vector

5. To display the vector.

import java.util.*;
public class Exp5_3
{
public static void main(String a[])
{
Vector list=new Vector();
System.out.print("Initializing vector....\n");
for(int i=0;i<4;i++)
list.addElement(new Integer(i+1));
list.addElement(new String ("C"));
list.addElement(new String ("C++"));
list.addElement(new String ("JAVA"));
list.addElement("A");
list.addElement("B");
list.addElement(new Float(23.78));
list.addElement(new Float(88.90));
System.out.println("Starting Vector: " +list);
System.out.println("Vector size: " +list.size());
System.out.println("Vector capacity: " +list.capacity());
list.addElement(new String("VB"));
System.out.println("Add object at the end of vector...");
System.out.println("Current vector: " +list);
list.removeElementAt(1);
System.out.println("Remove second object from the vector...");
System.out.println("Current vector: " +list);
if(list.contains(new Float(88.90)))
System.out.print("Searching....\nObject found!\n");
else
System.out.print("Searching....\nObject not found!\n");
System.out.println("Display first element of vector: " +list.elementAt(0));
System.out.println("Display last element of vector: " +list.elementAt(list.size()-
1));

System.out.println("Current vector: " +list);


}
}

Q1.Program to create a class �Book� having data members �Author�, �Title�&


�Publisher�. Derive a class �Book Info� having data member �price� and �stock
position� and a method �show�. Initialize and display it for three objects of
"bookinfo".

import java.io.*;
class Book{
String author,title,publisher;
void get(int i) {
BufferedReader in=new BufferedReader(new InputStreamReader
(System.in));
try{
System.out.println("Enter author name ,title and publisher of book"+(i+1));
author=in.readLine();
title=in.readLine();
publisher=in.readLine();
}
catch(Exception e){}
}
void show(){
System.out.print("\t\n \t"+author+"\t"+title+"\t"+publisher);
}
}
class BookInfo extends Book{
double price;
String stock_position;
void get1(int i) {
super.get(i);

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));


try{
System.out.println("Enter price and stock position of book"+(i+1));
price=Double.parseDouble(in.readLine());
stock_position=in.readLine();
}
catch(Exception e){}
}
void show(){
super.show();
System.out.print("\t\t"+price+"\t"+stock_position);
}
}
class mymain{
public static void main(String arg[]){
BookInfo b[]=new BookInfo[3];
for(int i=0;i<3;i++){
b[i]=new BookInfo();
}
for(int i=0;i<3;i++){
b[i].get1(i);
}
System.out.println("\tAuthor"+"\tTitle"+"\tPublisher"+"\tprice"+"\tstock
position");
for(int i=0;i<3;i++){

b[i].show();}
Q2.WAP TO CREATE A CLASS 'bird' WITH DATA MEMBERS AS 'category' & A METHOD
'show()'.DERIVE A CLASS 'parrot' WITH DATA MEMBER 'no_of_parrot' & A METHOD
'show()'.INITIALIZE USING CONSTRUCTOR AND DISPLAY THE INFORMATION .

class Bird{
String category;
Bird(String cat){
category=cat;
}
void show(){
System.out.println("Category:"+category);
ss}

}
class Parrot extends Bird
{
int no;
Parrot(int n,String C)
{super(C);
no=n;
}
void show()
{
super.show();
System.out.println("No. of parrots:"+no);
}
}
class main1
{
public static void main(String arg[])
{
Parrot p=new Parrot(1210,"Parrot");
p.show();}}
Q3.WAP TO CREATE A CLASS 'fruits' WITH DATA MEMBER 'countable(VALUES=YES/NO)' & A
METHOD 'display()'.DERIVE A CLASS 'watermelon' HAVING DATA MEMBERS 'price',
'quantity' & METHOD 'show()'.INITIALIZE & DISPLAY THE INFORMATION FOR 3 OBJECTS OF
WATERMELON.

import java.io.*;
class Fruits{
String countable;
void get(){
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter whether the fruit is countable or not");
countable=in.readLine();
}
catch (Exception e){}
}
void display(){
System.out.print("\n"+"\t"+countable);
}
}
class Watermelon extends Fruits{
int price,quantity;
void get(int i){
super.get();
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter price and quantity of object"+(i+1));
price=Integer.parseInt(in.readLine());
quantity=Integer.parseInt(in.readLine());
}
catch (Exception e){}
}
void show(){
super.display();
System.out.print("\t\t"+price+"\t"+quantity);
}
}

class main2
{
public static void main(String arg[])
{
Watermelon w[]=new Watermelon[3];

for(int i=0;i<3;i++)
{
w[i]=new Watermelon();
}
for(int i=0;i<3;i++)
{
w[i].get(i);
}
System.out.print("\t"+"category"+"\t"+"price"+"\t"+"quantity");
for(int i=0;i<3;i++)
{
w[i].show();
}
}
}

Q4. WAP TO IMPLEMENT THE FOLLOWING FIGURE.

import java.io.*;
interface cassette
{
void accept_title();
}
interface cd
{
void accept();
}
class mobile implements cassette,cd
{
String title;
String n;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
public void accept_title()
{
try
{
System.out.println("enter cassete title:");
title=stdin.readLine();
}
catch(Exception e){}
}
public void accept()
{
try
{
System.out.println("enter cd name:");
n=stdin.readLine();
}
catch(Exception e){}
}
void disp_cd()
{
System.out.println("cd name::"+n);
}
void disp_cas()
{
System.out.println("cassette title::"+title);
}
}
class mob
{
public static void main(String arg[])
{
int w=1;
mobile m=new mobile();
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter for which you want to enter data:");
System.out.println("0:CASSETTE");
System.out.println("1:CD");
w=Integer.parseInt(stdin.readLine());
}
catch(Exception e){}
switch(w)
{
case 1:
m.accept();
m.disp_cd();
break;
case 0:
m.accept_title();
m.disp_cas();
break;
}
}
}

Q8. WAP TO IMPLEMENT THE FOLLOWING FIGURE.

import java.io.*;
class employee
{
String name;
int bs;
void getdata()
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter name of employee:");
name=stdin.readLine();
System.out.println("enter basic salary:");
bs=Integer.parseInt(stdin.readLine());
}
catch(Exception e){}
}
}
interface gross
{
final int ta=400;
final int da=2000;
void gsal();
}
class salary extends employee implements gross
{
int hra;
int gs;
public void gsal()
{
gs=bs+da+hra;
}
void disp_sal()
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try
{
getdata();
System.out.println("enter hra:");
hra=Integer.parseInt(in.readLine());
}
catch(Exception e){}
gsal();
System.out.println("name of employee::"+name);
System.out.println("basic salary::"+bs);
System.out.println("ta::"+ta);
System.out.println("da::"+da);
System.out.println("hra::"+hra);
System.out.println("gross salary::"+gs);
}
}
class m_sal
{
public static void main(String arg[])
{
salary s=new salary();
s.disp_sal();
}
}

OUTPUT:

package useFul;
public class UseMe{
public double area(double l,double b)
{return l*b;}
public double area(double r)
{return 3.14*r*r;}
public double sal(int bs,int da,int hra)
{return bs+da+hra;}
public double per(int tot,int mar)
{return (mar/tot*100);}
}

import useFul.*;
import java.io.*;
class PackageUse{
public static void main (String args[]){
double l=0.0,b=0.0,r=0.0,ar=0.0,ac=0.0;
UseMe u=new UseMe();
try{
DataInputStream in=new DataInputStream (System.in);
System.out.println("Enter length and breadth of rectangle");
l=Double.parseDouble(in.readLine());
b=Double.parseDouble(in.readLine());
System.out.println("Enter radius of circle");
r=Double.parseDouble(in.readLine());
}catch(Exception e){}
ar=u.area(l,b);
ac=u.area(r);
System.out.print("\tArea\n Circle \t="+ac+"\n Rectangle \t="+ar);
}}

import useFul.*;
import java.io.*;
class Manager{
public static void main (String args[]){
double bs=0,da=0,hra=0,ts=0;
UseMe u=new UseMe();
try{
DataInputStream in=new DataInputStream (System.in);
System.out.println("Enter Basic Salary");
bs=Double.parseDouble(in.readLine());
System.out.println("Enter DA");
da=Double.parseDouble(in.readLine());
System.out.println("Enter HRA");
hra=Double.parseDouble(in.readLine());
}catch(Exception e){}
ts=u.sal(bs,da,hra);
System.out.print("\tSalary\n Total Salart\t="+ts); }
}

import useFul.*;
import java.io.*;
class Student{
public static void main (String args[]){
double s1=0,s2=0,s3=0,total=0,t=300,per=0;
UseMe u=new UseMe();
try{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter 1st Subject Marks");
s1=Double.parseDouble(in.readLine();
System.out.println("Enter 2nd Subject Marks");
s2=Double.parseDouble(in.readLine());
System.out.println("Enter 3rd Subject Marks");
s3=Double.parseDouble(in.readLine());
}catch(Exception e){}
total=s1+s2+s3;
per=u.per(t,total);
System.out.println("Percetage of Student =\t"+per+"%"); }}

package useful.usefulltoo;
public class UseMeToo{
public void display(){
System.out.println("THIS IS FROM INSIDE PACKAGE"); }
}

import useful.usefulltoo.UseMeToo;
class SubPackageUse {
public static void main (String args[]){
UseMeToo u=new UseMeToo();
u.display();
}
}

import java.io.*;
class MyException extends Exception{
MyException(String msg){
super(msg); }}
class User{
public static void main (String args[]) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String u_name=" ";
String u_pass=" ";
String m_pass=" ";
try{ System.out.println("Enter User Name:--");
u_name=br.readLine();
System.out.println("Enter Password:--");
u_pass=br.readLine();
System.out.println("Re-Enter Password:--");
m_pass=br.readLine();
if(!u_pass.equals(m_pass))
throw new MyException("Authentication failed!!!!");else
throw new MyException("Authentication Passed!"); } catch(MyException e)
{ System.out.println(e); } catch(IOException e){} }}
import java.io.*;
class MyException extends Exception{
MyException(String msg){
super(msg); }}
class User{
public static void main (String args[]) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int no=0;
try{
System.out.println("Enter Number:--");
no=Integer.parseInt(br.readLine());
if((no%2)!=0)
throw new MyException("NUMBER IS NOT EVEN");
else
throw new MyException("NUMBER IS EVEN");}
catch(MyException e){System.out.println(e);}
catch(IOException e){}
}
}

class myexception extends Exception{


private int detail;
myexception(int a)
{
detail=a;}
public String toString()
{
return "My Exception [" +detail+ "] ";
}
}
class throws1
{
static void compute(int a) throws myexception
{
System.out.println("called compute (" +a+ ")");
if(a>10)
throw new myexception(a);
System.out.println("Normal Exit");
}
public static void main(String arg[])
{
try
{
compute(1);
compute(20);
}
catch(myexception e)
{
System.out.println("Caught: "+e);
}
}
}

import java.io.*;
class MyException extends Exception{
MyException(String msg){
super(msg); }}
class Exp8_3{
public static void main (String args[]) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int no=0;
try{
System.out.println("Enter Number:--");
no=Integer.parseInt(br.readLine());
int p=1;
for(int i=2;i<no;i++){
if(no%i==0){
p=0;
break; }}
if(p==0)
throw new MyException("Number is not Prime");
System.out.println("Numer is Prime");}
catch(MyException e){
System.out.println(e); }
catch(IOException e){} }}

import java.io.*;
class My extends Exception{
My(String msg){
super(msg); }}
class Circle {
double area,x,y,r;
Circle(double a,double b,double c)throws My{ if(c<=0)
throw new My("Exception:-Radius is negative");
else {
x=a;
y=b;
r=c;
area(); }
}
void area(){
area=3.14*r*r;
System.out.println("Area of circle=\t"+area);
System.out.println("X co-ordinate:- "+x+"/nY co-ordinate:- "+y); }
}
class Exp8_4{
public static void main (String args[]) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
double r,x,y;
try{
Circle c;
System.out.println("Enter Radius:--");
r=Integer.parseInt(br.readLine());
System.out.println("Enter X");
x=Integer.parseInt(br.readLine());
System.out.println("Enter Y");
y=Integer.parseInt(br.readLine());
c=new Circle(x,y,r);
}
catch(My e){
System.out.println(e);
}
catch(IOException e){}
}}
Q1. WAP TO CREATE 2 THREAD SUCH THAT ONE THREAD WILL PRINT ODD NUMBERS AND OTHER
THREAD WILL PRINT EVEN NUMBERS BETWEEN 1 TO 20 NUMBERS.

class mythread1 extends Thread{


public void run(){
for(int n=1;n<=20;n++){
if(n%2==0)
System.out.println("Even thread"+n);}}
}
class mythread2 extends Thread{
public void run(){
for(int n=1;n<=20;n++){
if(n%2!=0)
System.out.println("Odd thread"+n);}}
}
class evenodd{
public static void main(String arg[]){
new mythread1().start();
mythread2 a=new mythread2();
a.run();}

Q2. WAP TO CREATE 2 THREADS, ONE THREAD PRINTS NUMBERS FROM 1 TO 10 AND OTHER
THREAD PRINTS NUMBER FROM 10 TO 1.THE FIRST THREAD SHOULD TRANSFER THE CONTROL TO
SECOND THREAD OFTEN PRINTING SECOND NUMBER.
class th extends Thread {
public void run(){
try{for(int i=1;i<=10;i++){
System.out.println("Ascend Thread "+i);
if(i%2==0)
Thread.sleep(200);}}
catch(Exception e){}}}
class th2 extends Thread{
public void run(){
try{for(int i=10;i>=1;i--){
System.out.println("Decending Thread "+i);
Thread.sleep(200);}}
catch(Exception e){}}}
class order{public static void main(String arg[]){
th t=new th();
th2 t2=new th2();
t.start();
t2.start();}}
Q3. WAP TO CREATE 2 THREADS HIGHER
THAN THE OTHER. DISPLAY SUITABLE OUTPUT.

class thread1 extends Thread{


public void run(){
System.out.println("thread1");}
}
class thread2 extends Thread{
public void run(){
System.out.println("thread2");}
}
class threadq{
public static void main(String arg[]){
thread1 t1=new thread1();
thread2 t2=new thread2();
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.run();}
}

EXT 1. WAP TO CREATE 2 THREAD SUCH THAT ONE THREAD WILL PRINTS PRIME NUMBERS AND
OTHER THREAD WILL PRINTS NONPRIME NUMBERS BETWEEN 1 TO 10 NUMBERS.

class prime extends Thread{


public void run(){
try{for(int n=1;n<=10;n++){
int p=1;
for(int i=2;i<n;i++){
if(n%i==0){
p=0;
break;}}
if(p==1)
System.out.println("\tPrimeThread"+n);
Thread.sleep(500);}}
catch(InterruptedException e){}}
}
class notprime extends Thread{
public void run(){
try {for(int n=1;n<=10;n++){
int p=1;
for(int i=2;i<n;i++){
if(n%i==0){
p=0;
break;}}
if(p==0)
System.out.println("\t not prime Thread" +n);
Thread.sleep(500);}}
catch(InterruptedException e){}}}
class primenotprime{
public static void main(String arg[]){
new prime().start();
new notprime().start();}
}

Das könnte Ihnen auch gefallen