You are on page 1of 49

PROGRAMME USING JAVA

(Note : - W.A.P. Write a Program) Q.1. WAP to program arithmetic operations ?

Algorithm Start 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. Stop Program // Program to perform arithmetic operations class nice // class name { public static void main(string args[]) // method { // printing System.out.println(Definition of variables); char a=D; byte b=25; short c = 1234; int d = 123456789; long e = 12345678910L; float f = 12.34F; double g = 9.989898; // printing System.out.println(The value of a is +a); System.out.println(The value of b is +b); System.out.println(The value of c is +c); System.out.println(The value of d is +d); System.out.println(The value of e is +e); System.out.println(The value of f is +f); System.out.println(The value of g is +g); } // end of method } // end of class Step 1 :- Printing Step 2 :- Storing String Step 3 :- storing the value of a Step 4 :- storing the value of b Step 5 :- storing the value of c Step 6 :- storing the value of d Step 7 :- storing the value of e Step 8 :- storing the value of f Step 9 :- storing the value of g Step 10 :- Printing the value of a Step 11 :- Printing the value of b Step 12:- Printing the value of c Step 13 :- Printing the value of d Step 14 :- Printing the value of e Step 15 :- Printing the value of f Step 16 :- Printing the value of g

Output The value of a is 0 The value of a is 25 The value of a is 1234 The value of a is 123456788 The value of a is 12345678 10L The value of a is 12.34 F The value of a is 9.989898

Q.2. WAP to display the Sum, Product, Subtraction and modules of any two assigned number ? Algorithm Start 1. 2. 3. 4. 5. 6. stop Program // Program to display the sum, product, subtraction and // modulus of any two assigned number class pop // class name { public static void main(string args[]) // method { // Declaration and assignment of variables int a = 1234; int b = 4; // printing System.out.println(The sum of two numbers is +(a+b)); System.out.println(The product of two numbers is +a*b); System.out.println(The Subtraction of two numbers is +(a-b)); System.out.println(The modulus of two numbers is +a%b); } // end of method } // end of class Output The sum of Two Numbers is 1238 The product of Two Numbers is 4936 The subtraction of Two Numbers is 1230 The modulus of Two Numbers is 2 Step 1 :- storing int a Step 2 :- storing int b Step 3 :- printing the sum of a and b Step 4 :- printing the product of a and b Step 5 :- printing the subtraction of a and b Step 6 :- printing the modulus of a and b

Q.3. WAP to print the short hand notation ? Algorithm Start 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Stop Program // Program to demonstrate the shorthand notation class num // class name { public static void main(string args[]) // method { int a=10; float b=20; a+=10; b-=10; // printing System.out.println(a is +a); System.out.println(b is +b); a*=2; b/=2; // printing System.out.println(Now a is +(++a)); System.out.println(Now b is b); a%=10; b%=10; // printing System.out.println(Now a is +(++a)); System.out.println(Now b is +(++b)); } end of method } end of class Output a is 20 b is 10 Now a is 40 Step 1 : - Storing int a Step 2 :- Storing float b Step 3 :- adding 10 in a Step 4 :- subtracting 10 in b Step 5 :- printing a Step 6 :- printing b Step 7 :- a is multiply by 2 Step 8 :- b is divide by 2 Step 9 :- Printing a Step 10 :- Printing b Step 11 :- taking modules of a Step 12 :- taking modules of b Step 13 :- Print a by adding 1 Step 14 :- Printing b by adding 1

Now b is 5 Now a is 1 Now b is 6

Q.4. WAP to demonstrate the increment operations. Algorithm Start 1. 2. 3. 4. 5. 6. 7. 8. stop Step 1 : - Storing int a Step 2 :- Storing into b Step 3 :- Printing a before adding 1 to it Step 4 :- printing b before adding 1 to it Step 5 :- printing a Step 6 :- printing b Step 7 :- printing a after adding 1 to it Step 8 :- printing b after adding 1 to it

Program // Program to demonstrate the increment operators. class pupil // class { public static void main(String args[]) { int a=10; int b=20; System.out.println(a is +(a++)); System.out.println(b is +(b++)); System.out.println(Now a is +a); System.out.println(Now b is +b); System.out.println(Now a is+(++a)); System.out.println(Now b is +(++b)); } // end of method } // end of class Output a is 10 b is 20 Now a is 11 Now b is 21 Now a is 12

Now b is 22 Q.5. WAP to demonstrate the decrement operators. Algorithm Start 1. 2. 3. 4. 5. 6. 7. 8. Step Step Step Step Step Step Step Step 1 :- Storing int a 2 :- Storing int b 3 :- printing a before subtracting 1 to it 4 :- printing b before subtracting 1 to it 5 :- printing a 6 :- printing b 7 :- printing a after subtracting 1 to it 8 :- printing b after subtracting 1 to it

Stop Program // Program to demonstrate the decrement operators. class abc // class { public static void main(string args[]) // method { int a=10; int b=20; // printing System.out.println(a is +(a--)); System.out.println(b is +(b--)); System.out.println(Now a is +a); System.out.println(Now b is +b); System.out.println(Now a is+(--a)); System.out.println(Now b is +(--b)); } // end of method } // end of class Output a is 10 b is 20 Now a is 9 Now b is 19 Now a is 8 Now b is 18 Q.6. WAP to demonstrate the logical operators. Algorithm Start 1. Step 1 :- storing byte a

2. 3. 4. 5. 6. 7. 8. 9. Stop Program

Step Step Step Step Step Step Step Step

2 :- storing byte b 3 :- storing byte c 4 :- storing byte d 5 :- checking condition No. 1 6 :- printing if condition No. 1 is satisfied 7 :- checking condition No. 8 :- printing if condition No. 21 is satisfied 9 :- printing the result of condition No. 3

// Program to demonstrate the // usage of logical operators class pp // class { public static void main(string args[]) // method { byte a=10; int b=20; int c=90; int d=-19; if (a>=10 && b<30) {// printing System.out.println(a is +(+a)); System.out.println(b is +(+b)); } if (c>10 || d<10) {// printing System.out.println(a<b); System.out.println(d<=c); } System.out.println(!(a<b)); } // end of method } // end of class Output a is 10 b is 20 true true false Q.7. WAP to demonstrate the conditional operator. Algorithm Start 1. 2. Step 1 :- storing byte a Step 2 :- storing int b

3. 4. 5. Stop Program

Step 3 :- storing int c Step 4 :- checking condition Step 5 :- printing the value of c

// Program to demonstrate the // usage of conditional operator class cond // class name { public static void main(string args[]) // method { byte a=10; int b=20; int c; c=(a>b)? a:b; // printing System.out.println(The value of c is +c); } // end of method } // end of class Output The value of c is 20 Q.8. WAP to demonstrate the usage of implicit or automatic type conversion. Algorithm Start 1. 2. 3. 4. Stop Program // Program to demonstrate the // usage of implicit or automatic type conversion class one // class name { public static void main(string args[]) // method { byte amount=10; float account = 12.32F; double sum = (double) (amount * account); // printing Step Step Step Step 1 :- storing byte amount 2 :- storing float amount 3 :- product of stored values 4 :- printing

System.out.println(The value of sum is +sum); } // end of method } // end of class Output The value of sum is 123.2

Q.9. WAP to demonstrate the usage of explicit type conversion Algorithm Start 1. 2. 3. Stop Program // Program to demonstrate the // usage of explicit type conversion class pp // class name { public static void main(string args[]) // method { byte amount=10; float account = (int) (amount*100); System.out.println(The value of account is +account); } // end of method } // end of class Output The value of sum is 1000 Step 1 :- Storing byte amount Step 2 :- multiplying amount with 100 Step 3 :- printing the above value

Q.10. WAP to print the given string Algorithm Start

1. Stop Program

Step 1 :- printing the string

// Program { public static void main(string args[]) //method { // printing System.out.println(Hail The World !); } // end of method } // end of class Output **************** Q.11. WAP to input any number and print the some in reverse. Algorithm Start 1. 2. 3. 4. 5. 6. 7. Stop Step 1 :- getting input Step 2 :- storing the value Step 3 :- storing the modeling in digit Step 4 :- storing the remainder Step 5 :- input is divided by 10 Step 6 :- condition Step 7 :- printing the reverse

Program // Write a Program to input any number and print the // same in reverse import java.io.*; // package import.java.lang.*; // package class p5 // class name { // method public static void main(string args[]) throws Exception

{ InputStreamReader in = new InputStreamReader(system.in); BufferedReader input=new BufferedReader(in); int number = Integer.parseInt(input.readLine()) int digit=0, c=0, number 1=0; int n1=number; do { digit = number% 10; number1 = number1 *10 + digit; number = number/10; } while (number!=0); // printing System.out.println(the reverse of the number +n1+ is number1); } // end of method } // end of class Output The reverse of the number 55 is 55

Q.12. WAP to complete the sum of two integer of input. Algorithm Start 1. 2. 3. 4. 5. Stop Program import java.io.*; // package class sumofintegers // class name { public static void main(String[] args) throws IOException // Method { InputStreamReader abc = new InputStreamReader(system.in); Step Step Step Step Step 1 :- Printing 2 :- getting the input a 3 :- printing 4 :- getting the input b 5 :- printing the sum

// abc is an object, InputStreaReader is a class BufferedReader bbc = new BufferedReader(abc); // bbc is an object System.out.println(Enter the first number); int a = Integer.parseInt(bbc.readLine()); // integer is a class and parseInt() is method System.out.println(Enter the second number); int b = Integer.parseInt(bbc.readLine()); System.out.println(Sum: + (a+b)); } // end of method } // end of class Output Enter First Number : 55 Enter Second Number : 5 Sum: 60

Q.13. WAP to demonstrate the usage of if statement. Algorithm Start 1. 2. 3. 4. Stop Program // Program to demonstrate the // usage of if statement { public static void main(String args[]) // Method { byte amount=10; float account = 12.32F; if (amount==10 && account<20) // if condition { // printing System.out.println(The value is +account); } } // end of method } // end of class Step Step Step Step 1 :- Storing byte amount 2 :- Storing float amount 3 :- checking condition if 4 :- printing statement if condition satisfied

Output The value 12.32 Q.14. WAP to find the greatest number Algorithm Start 1. 2. 3. 4. 5. Stop Program // Program to demonstrate the // usage of find the greatest number public class IfStatementDemo{ // class public static void main(string[] args) // Method int a = 10, b = 20; if (a > b) { // printing System.out.println(a > b); if (a < b) { // printing System.out.println(b > a); } // end of method } // end of class Output b>a Q.15. WAP to demonstrate the usage of if else statement Algorithm Start 1. 2. 3. 4. 5. Step Step Step Step Step 1 :- Storing byte amount 2 :- Storing float account 3 :- checking condition 4 :- printing if condition is satisfied 5 :- printing if condition is not satisfied Step Step Step Step Step 1 :- Storing the values of a and b 2 :- checking (a>b) or not 3 :- printing if (a>b) 4 :- checking (a<b) or not 5 :- printing if (a<b)

Stop Program // Program to demonstrate the // usage of if else statement class ppp // class name { public static void main(string args[]) // Method { byte amount=10; float account = 12.32F; if (amount==10 && account<10) // if condition // printing System.out.println(The value is +account); } else { // printing System.out.println(The value is +amount); } // end of method } // end of class Output The value is 10

Q.16. WAP to print the greatest value Algorithm Start 1. 2. 3. 4. 5. 6. 7. 8. 9. Stop Program // Program to demonstrate the // usage of if else if statement class aaa // class name { public static void main(string args[]) // Method { int a=10, b=20, c=30; Step Step Step Step Step Step Step Step Step 1 :- Storing the values of a, b, c 2 :- printing 3 :- checking condition 4 :- checking condition 5 : - printing if condition is satisfied 6 :- printing if condition is not satisfied 7 :- checking condition 8 :- printing if condition is satisfied 9 :- printing if condition is not satisfied

// printing System.out.println(The greatest value is ); if (a>b) // if condition { if (a>c) // if condition { System.out.println(a); } else { // printing System.out.println(c); } } else { if (c>b) { // printing System.out.println( c); } else { // printing System.out.println(b); } } } // end of method } // end of class Output The greatest value is 30 Q.17. WAP to print the numbers from 1 to 10 Algorithm Start 1. 2. 3. Stop Program // Program to demonstrate the // usage of switch and for class shruti // class name { public static void main(string[] args) // Method { int a Step 1 :- Storing int a Step 2 :- working of for loop till ( a<=10) Step 3 :- printing a

for (a=1; a<=10; a=a+1) { // printing System.out.println(a); } } // end of method } // end of class

Output 1 2 3 4 5 6 7 8 9 10

Q.18. WAP to calculate the factorial of any number Algorithm Start 1. 2. 3. 4. Stop Program class ant // class name { public static void main(string args[]) // Method { int x, fact=1; int number = 10; for (x=1; x<=number; x=x+1) // for loop { fact=fact*x; } // end of for loop // printing System.out.println(The factorial of +number+ is); System.out.println(fact); // printing } // end of method } // end of class Step Step Step Step 1 :- Storing intigers 2 :- working of for loop till x<=number 3 :- storing the new value of fact 4 :- printing the factorial

Output The factorial of 10 is 3628800

Q.19. WAP to print the sum of following series :Step = a/1 + a/2 + a/3 + + a/n Where a = 2 and n= 10 Algorithm Start 1. 2. 3. 4. Stop Program //Program to display the sum of series class work // class name { public static void main(string args[]) // Method { int x, a=2, n=10; float sum=0.0F; for (x=1; x<=n; x=x+1) // for loop { sum=sum+(float) (a/x); } // end of for loop // printing System.out.println(The sum of the series is +sum); } // end of method } // end of class Output The sum of the series is 3 Q.20. WAP to print the first ten terms of fibonari series Algorithm Start 1. Step 1 :- Storing the values of a, b and c Step Step Step Step 1 :- storing the values 2 :- working of for loop till x <= n 3 :- Storing the new value of sum 4 :- printing the sum

2. 3. 4. 5. 6. Stop Program

Step Step Step Step Step

2 :- working of while loop till (count <= 10) 3 :- printing a 4 :- Storing c = b + a 5 :- Storing c= b + a 6 :- Storing b = a, a=c and count + =1

//Program to display the first ten terms // of the Fibonacci series class fibo // class name { public static void main(string args[]) // Method { int count=1, a=1, b=0, c=0; while(count<=10) // while loop { System.out.println(a); // printing c=b+a; b=a; a=c; count=count+1; } // end of while loop } // end of method } // end of class

Output 1 1 2 3 5 8 13 21 34 55

Q.21. WAP the print the sum of 100 natural numbers Algorithm Start

1. 2. 3. 4. 5. Stop Program

Step Step Step Step Step

1 :- storing the integer 2 :- adding the value a in sum 3 :- Adding one in a 4 :- working of while loop till (a < = 10) 5 :- printing the sum

//Program to print the sum of first ten // natural numbers using do-while class sumnum // class name { public static void main(string args[]) // Method { int sum=0, a=1; do // do - while loop { sum=sum+a; a=a+1; } while(a<=10); // end of loop System.out.println(The sum of natural numbers from 1-10 is +sum); // printing } // end of method } // end of class Output The sum of natural numbers from 1-10 is 55 Q.22. WAP to print fibonacci series using do while loop Algorithm Start 1. 2. 3. 4. 5. 6. 7. Stop Program public class Fibonacci { // class name Step Step Step Step Step Step Step 1 :- printing 2 :- storing the 3 double values 3 :- printing first 2 double values 4 :- working of do while loop till (file < 5000) 5 :- add first 2 double values in temp 6 :- printing the temp 7 :- replacing the numbers

public static void main(string args[]) // method System.out.println(Display a set of Fibonacci sequence); // printing double fib1 = 0; double fib2 = 1; double temp = 0; // printing System.out.println(fib1); System.out.println(fib2); do { // do- while loop temp = fib1 + fib2; System.out.println(temp); // printing fib1 = fib2; // Replace 2nd with first number fib2 = temp; // Replace temp number with 2nd number } while (fib2 < 5000); // end of loop } // end of method } // end of class

Output 0.0 1.0 2.0 3.0 5.0 8.0 13.0 21.0 34.0 55.0 89.0 144.0 233.0 377.0 610.0 987.0 1597.0 2584.0 4181.0 6765.0

Q.23. WAP to print week days using switch Algorithm Start 1. 2. Step 1 :- storing int day Step 2 :- using switch function

3. 4. 5. 6. 7. 8. 9. 10. 11. Stop

Step Step Step Step Step Step Step Step Step

3 :- If case 1 is selected then printing of case 1 will be done 4 :- If case 2 is selected then printing of case 2 will be done 5 :- If case 3 is selected then printing of case 3 will be done 6 :- If case 4 is selected then printing of case 4 will be done 7 :- If case 5 is selected then printing of case 5 will be done 8 :- If case 6 is selected then printing of case 6 will be done 9 :- If case 7 is selected then printing of case 7 will be done 10 :- If any other case is selected then default printing will be done 11 :- printing

Program class day // class name { public static void main(string args[]) // method { int day=2; switch(day) { case1 : System.out.println( SUNDAY-HOLIDAY); // printing break; case 2 : System.out.println( MONDAY) ; // printing break; case 3 : System.out.println( TUESDAY) ; // printing break; case 4 : System.out.println( WEDNESDAY) ; // printing break; case 5 : System.out.println( THURSDAY) ; // printing break; case 6 : System.out.println( FRIDAY) ; // printing break; case 7 : System.out.println( SATURDAY) break; default: System.out.println( Sorry, Wrong entry); // printing } System.out.println( Program ends); // printing } // end of method } end of class

Output Case 1 : SUNDAY Holiday Case 2 : MONDAY Case 3 : TUESDAY Case 4 : WEDNUSDAY Case 5 : THURSDAY Case 6 : FRIDAY Case 7 : SATURDAY Default : Sorry, wrong entry Program ends Q.24. WAP to demonstrate break and continue Algorithm Start 1. 2. 3. 4. 5. 6. 7. Stop Step Step Step Step Step Step Step 1 :- working of for loop No. 1 2 :- Printing i 3 :- if condition and break condition 4 :- working of for loop No.2 5 :- Printing 6 : - if and continue condition 7 :- closing of loops

Program // Program class ppp // class name { public static void main(string args[]) // method { loopone: for (int i=1; i<5; i++) { System.out.println(i); if (i>=8) break; for(int k=1;k<=5;k++) { System.out.println(Hello+k); if (i<k) continue loopone; } } } // end of method } // end of class Output Hello 1 Hello22 Hello 1 Hello2Hello33 Hello 1 Hello2Hello3Hello44

Q.25. WAP for declaration of a class Algorithm Start 1. 2. 3. 4. 5. 6. 7. 8. Stop Step Step Step Step Step Step Step Step 1 :- declaration of class 2 :- instant variables 3 :- method function 4 :- Storing double volume 5 :- returning volume 6 :- end of class 7 :- calling function in another class 8 :- printing

Program // Program with class in JAVA class container // declaration of class container

{ double width; // instance variables double height; double depth; double vol(double w, double h, double d) // method definition { double volume=w*h*d; return (volume); } } class c1 { public static void main(string args[]) { container c1=new container(); // creating object c1 // printing System.out.println(The volume of the container is +c1.vol(2,3,4)); } // end of method } // end of class Output The volume of the container is 24 Q.26. WAP for accessing member elements of the class Algorithm start 1. 2. 3. 4. 5. 6. 7. stop Step Step Step Step Step Step Step 1 :- Storing double values 2 :- Storing double volume 3 :- Printing volume 4 :- creating object 5 :- Accessing variables 6 :- Accessing method vol() 7 :- Printing

Program // Program for accessing member elements of the class class container { //method double width; double height; double depth; double vol() {

double volume=width*height*depth; return (volume); } } class c1 { public static void main(string args[]) { container c1=new container(); // creating object c1 // Accessing elements of the class //Accessing instance variable width c1.width=10; //Accessing instance variable height c1.height=20; //Accessing instance variable depth c1.depth=30; //Accessing the method vol() System.out.println(The volume of the container is +c1.vol()); } // end of method } // end of class

Output The volume of the container is 6000

Q.27. WAP of a class with a method with no return value Algorithm start 1. 2. 3. 4. 5. 6. 7. stop Step Step Step Step Step Step Step 1 :- Storing double values 2 :- creating method with no return type 3 :- Storing double volume 4 :- Printing 5 :- creating object 6 :- accessing variables 7 :- accessing method vol ()

Program // Program of a class having method of no return type class container { //method double width; double height; double depth; // Definition of a method with no return type void vol() { double volume=width*height*depth; System.out.println(The volume of the box is +volume); } } class c1 { public static void main(String args[]) { container c1=new container(); // creating object c1 // Accessing elements of the class //Accessing instance variable width c1.width=1; //Accessing instance variable height c1.height=2; //Accessing instance variable depth c1.depth=3; //Accessing the method vol() c1.vol(); } // end of method } // end of class Output The volume of the box is 6 Q.28. WAP having return type method with in a class Algorithm start 1. 2. 3. 4. 5. 6. 7. 8. stop Step Step Step Step Step Step Step Step 1 :- Storing double values 2 :- creating method with return type 3 :- Storing double volume 4 :- returning volume 5 :- creating object 6 :- accessing variables 7 :- accessing method vol () 8 :- printing

Program // Program of a class having method of a return type class container { //method double width; double height; double depth; // Definition of a method with return type double vol() { double volume=width*height*depth; return volume; } } class c1 { public static void main(string args[]) { container c1=new container(); // creating object c1 // Accessing elements of the class //Accessing instance variable width c1.width=12; //Accessing instance variable height c1.height=22; //Accessing instance variable depth c1.depth=32; //Accessing the method vol() System.out.println(The volume is +c1vol()); } // end of method } // end of class Output The volume is 8448

Q.29. WAP to define a class with five marks as instance variables and methods total () and average() for computation. Algorithm start 1. 2. 3. 4. 5. 6. 7. 8. Step Step Step Step Step Step Step Step 1 :- creating class 2 :- Storing double marks of five subjects 3 :- creating method with no return type 4 :- Storing double total 5 :- printing total 6 :- creating double average() 7 :- Storing double average 8 :- creating object

9. Step 10. Step 11. Step 12. Step 13. Step stop Program

9 :- accessing variables 10 :- accessing method total() 11 :- printing 12 :- Accessing method average() 13 :- print

// Program of a class having method total() and average() to // compute the same of any five marks. class boy { //method double marks1; double marks2; double marks3; double marks4; double marks5; // Definition of method with no return type double total() { double total=marks1+ marks2+ marks3+ marks4+ marks5; return total; } double average() { double average=total()/5; return average; } } class c1 { public static void main(String args[]) { boy c1=new boy(); // crating object c1 // Accessing elements of the class // Accessing instance variable marks c1.marks1=80; c1.marks2=90; c1.marks3=89; c1.marks4=56; c1.marks5=76; // Accessing the method total() System.out.println(The total marks is +c1.total()); // Accessing the method average() System.out.println(The average marks is +c1.average()); } // end of method } // end of class Output

The total marks is 391 The average marks is 78.2

Q.30. WAP with instance variable and instance method Algorithm start 1. Step 2. Step 3. Step 4. Step 5. Step 6. Step 7. Step 8. Step 9. Step 10. Step 11. Step stop Program // class Program with instance variables and instance methods class car { // Declaration of instance variables String make; String color; Boolean stateofengine; // Declaration of instance methods void start() { if (stateofengine==true) System.out.println(The car is ready to move) else { stateofengine=true; System.out.println(The car is now ready to move); } } void cartype() 1 :- declaration of instance variable 2 :- declaration of instance method 3 :- checking if condition 4 :- printing if condition satisfy 5 :- printing if condition not satisfy 6 :- printing 7 :- checking if condition 8 :- printing if condition satisfy 9 :- printing if condition not satisfy 10 :- creating object 11 :- accessing elements of class

{ System.out.println(This car has +color+ colour); System.out.println(This car has +make); if (stateofengine==true) System.out.println(The car is ready to move) else System.out.println(The car has been locked); } } class c1 { public static void main(String args[]) { car c1=new car(); // creating object c1 // Accessing elements of the class // Accessing instance variable width c1.make=Maruti ZEN; // Accessing instance variable height c1.color=WHITE; // Accessing the method c1.start(); c1.cartype(); } // end of method } // end of class Output The car is now ready to move This car has WHITE colour This car is a Maruti ZEN The car is ready to move Q.31. WAP to define usage of method in a class Algorithm start 1. 2. 3. 4. 5. 6. 7. 8. 9. stop Step Step Step Step Step Step Step Step Step 1 :- inputing values 2 :- Storing int a & b 3 :- returning a * b 4 :- inputing values 5 :- putting c = n 6 :- returning a * b * c 7 :- creating class 8 :- calling function 9 :- printing

Program // Program to define usage of method in a class class work { int a; int b; work(int l, int m) { a=l; b=m; } int cal() { return (a*b); } } class jobwork extends work { int c; jobwork(int l, int m, int n) { super(l,m); c=n; } int cal2() { return (a*b*c); } } class p4 { public static void main(String args[]) { jobwork job= new jobwork(2,3,4); int val1 = job.cal(); int val2 = job.cal2(); System.out.println(Value one is +val1); System.out.println(Value two is +val2); } // end of method } // end of class Output Value one is 6 Value two is 24

Q.32 WAP to demonstrate the usage of string method (to lower case())

Algorithm start 1. 2. 3. 4. 5. 6. stop Program // Program to define usage // of string method (toLowercase()) class work { Static String name1=new String(Shruti Mehrotra); public static void main(string args[]) { System.out.println(The value in lower case is +name1.toLowerCase()); } // end of method } // end of class Output shruti mehrotra (ii) toUpperCase; Step Step Step Step Step Step 1 :- creating class 2 :- creating object with string 3 :- creating method 4 :- printing to lower case() 5 :- end of method 6 :- end of class

Q.33. WAP to demonstrate the usage of string method (toUpprCase()) Algorithm start 1. 2. 3. 4. Step Step Step Step 1 :- declaring class 2 :- creating object with string 3 :- creating method 4 :- printing to upper case()

5. Step 5 :- end of method 6. Step 6 :- end of class stop

Program // Program to define usage of // string method (toUpperCase()) class A4 { Static String name1=new String(Shruti Mehrotra); public static void main(String args[]) { System.out.println(The value in upper case is + name1.toUpperCase()); } // end of method } // end of class Output SHRUTI MEHROTRA (iii) replace();

Q.34. WAP to demonstrate the usage of string method replace() Algorithm start 1. 2. 3. 4. 5. 6. stop Program // Program to define // usage of string method replace() Step Step Step Step Step Step 1 :- declaring class 2 :- creating object with string 3 :- creating method 4 :- printing - replace() 5 :- end of method 6 :- end of class

class ppp { Static String name1=new String(Shruti Mehrotra); public static void main(String args[]) { System.out.println(The value replacing all e by f is +name1.replace(e,f)); } // end of method } // end of class Output Shruti Mfhrotra (iv) trim(); Q.35. WAP to demonstrate the usage of string method trim() Algorithm start 1. 2. 3. 4. 5. 6. stop Step Step Step Step Step Step 1 :- declaring class 2 :- creating object object string 3 :- creating method 4 :- printing - trim() 5 :- end of method 6 :- end of class

Program // Program to define usage of string method trim() class p4 { Static String name1=new String(Shruti Mehrotra); public static void main(String args[]) { System.out.println(The value deleting space is +name1.trim()); } // end of method } // end of class

Output The value after deleting spaces is SHRUTI MEHROTRA (v) equals();

Q.36. WAP to demonstrate the usage of string method equals() Algorithm start 1. 2. 3. 4. 5. 6. stop Program // Program to define usage of string method equals() class p4 { Static String name1=new String(Shruti Mehrotra); Static String name2=new String(Shruti Mehrotra); public static void main(String args[]) { System.out.println(The value deleting equals() method is +name1.equals(name2)); } // end of method } // end of class Output The value equals() method is true (vi) length(); Step Step Step Step Step Step 1 :- declaring class 2 :- creating object with their string 3 :- creating method 4 :- printing - equals() 5 :- end of method 6 :- end of class

Q.37. WAP to demonstrate the usage of string method count() Algorithm start 1. 2. 3. 4. 5. 6. stop Program // Program to define usage of string method concat() Step Step Step Step Step Step 1 :- declaring class 2 :- creating object with their string 3 :- creating method 4 :- printing including - count() 5 :- end of method 6 :- end of class

class p4 { Static String name1=new String(Shruti); Static String name2=new String(Mehrotra); public static void main(String args[]) { int n=4; name1=name1.concat(name2); System.out.println(name1); System.out.println(name2.concat(name1)); } // end of method } // end of class Output ShrutiMehrotra MehrotraShrutiMehrotra (ix) substring(); Q.38. WAP to demonstrate the usage of string method substring() Algorithm start 1. 2. 3. 4. 5. 6. stop Program // Program to define usage of string method substring() class p4 { Static String name1=new String(Shruti); public static void main(String args[]) { int n=4; System.out.println(name1.substring(n)); } // end of method } // end of class Output ti (x) substring(); [another format] Q.39. WAP to demonstrate the usage of indexof() Step Step Step Step Step Step 1 :- declaring class 2 :- creating object with string 3 :- creating method 4 :- printing - substring() 5 :- end of method 6 :- end of class

Algorithm start 1. 2. 3. 4. 5. 6. Step Step Step Step Step Step 1 :- declaring class 2 :- creating object with string 3 :- creating method 4 :- printing - indexof() 5 :- end of method 6 :- end of class

stop Program // Program to define usage of string method indexof() class p4 { Static String name1=new String(ShrutiMehrotra); public static void main(String args[]) { System.out.println(The position of the character+ M is+name1.indexof(M)); } // end of method } // end of class Output The position of the character M is 7 (xii) indexOf();[another format] Q.40. WAP to demonstrate the usage of string method compare to () Algorithm start 1. 2. 3. 4. 5. 6. stop Program // Program to define usage of string method compareTo() class p4 { Static String name1=new String(ShrutiMehrotra); Static String name2=new String(ShrutiMehrotra); public static void main(String args[]) { Step Step Step Step Step Step 1 :- declaring class 2 :- creating object with their strings 3 :- creating method 4 :- printing compare to() 5 :- end of method 6 :- end of class

System.out.println(name1.compareTo(name2)); } // end of method } // end of class Output 0

Q.41. WAP to demonstrate the usage of some of the mathematical function Algorithm start 1. Step 2. Step 3. Step 4. Step 5. Step 6. Step 7. Step 8. Step 9. Step 10. Step 11. Step 12. Step 13. Step 14. Step 15. Step 16. Step 17. Step stop Program // Program to define usage of all mathematical functions class working { //method public static void main(String args[]) { double a=.235F; double b=4.0F; double c=2.4F; // printing System.out.println(sin of a is +Math.sin(a)); System.out.println(cos of a is +Math.cos(a)); System.out.println(tan of a is +Math.tan (a)); System.out.println(asin of a is +Math.asin(a)); System.out.println(acos of a is +Math.acos(a)); 1 :- Storing double values of a, b and c 2 :- printing Math.sin(a) 3 :- printing Math.cos(a) 4 :- printing Math.tan(a) 5 :- printing Math.asin(a) 6 :- printing Math.acos(a) 7 :- printing Math.atan(a) 8 :- printing Math.pow(a,b) 9 :- printing Math.exp(a) 10 :- printing Math.log(a) 11 :- printing Math.sqrt(a) 12 :- printing Math.ceil(a) 13 :- printing Math.floor(a) 14 :- printing Math.rint(a) 15 :- printing Math.abs(a) 16 :- printing Math.max(a,c) 17 :- printing Math.min(a,c)

System.out.println(atan of a is +Math.atan(a)); System.out.println(pow of a and b is +Math.pow(a,b)); System.out.println(exp of a is +Math.exp(a)); System.out.println(log of a is +Math.log(a)); System.out.println(sqrt of a is +Math.sqrt(b)); System.out.println(ceil of a is +Math.ceil(a)); System.out.println(floor of a is +Math.floor(a)); System.out.println(rint of a is +Math.rint(a)); System.out.println(abs of a is +Math.abs(a)); System.out.println(max of a and c is +Math.max(a,c)); System.out.println(min of a and c is +Math.min(a,c)); } // end of method } // end of class Output sin of a is 0.232843 cos of a is 0.972514 tan of a is 0.239424 asin of a is 0.237219 acos of a is 1.33358 atan of a is 0.230812 pow of a and b is 0.0030498 exp of a is 1.26491 log of a is -1.44817 sqrt of b is 2 ceil of a is 1 floor of a is 0 rint of a is 0 abs of a is 0.235 max of a and c is 2.4 min of a and c is 0.235 Q.42. WAP to utilize on array to compute the maximum and minimum of the stored number Algorithm start 1. 2. 3. 4. 5. 6. 7. 8. stop Step Step Step Step Step Step Step Step 1 :- storing the elements in array 2 :- storing the integer value 3 :- working of for 4 :- checking if condition 5 :- storing max value 6 :- checking if condition 7 :- Storing min value 8 :- Printing max.and min.value

Program // Program to utilize an array to compute the maximum and // minimum of ten stored numbers class array { //method public static void main(String args[]) { int num[]={5,10,7,6,12,4,14,15,17,3}; int len=num.length; int max=num[0]; int min=num[0]; for(int i=0;i<len;i++) { if (num[i]>max) max=num[i]; if (num[i]<min) min=num[i]; } System.out.println(Maximum is +max); System.out.println(minimum is +min); } // end of method } // end of class

Output Maximum is 17 Minimum is 3

Q.43. WAP to demonstrate class to store N elements in an array and compute the total number of time M occurs. where N and M are the inputs along with the elements of the array. Algorithm start 1. 2. 3. 4. 5. 6. 7. 8. 9. Step Step Step Step Step Step Step Step Step 1 :- creating object with array 2 :- storing the integer values 3 :- working of for loop 4 :- checking if condition 5 :- If condition is true, adding 1 to c 6 :- printing mmm & c 7 :- working of for loop 8 :- Storing a[i] 9 :- changing values

stop Program class freqarray { private int a[]=new int[10]; private int nnn,mmm; public void cal() { nnn=10; int c=0,I; for (i=0;i<nnn;i++) if (a[i]==mmm) c=c+1; System.out.println(The frequency of +mmm+ in the array is +c); } public void work1(int nn,int mm,int num[]) { for(int i=0;i<nn;i++) {a[i]=num[i]; } nnn=nn; mmm=mm; } // end of method } // end of class Output The frequency of 4 in the array is 3 Q.44. WAP to display the sum of rows and columns of an entered array Algorithm start 1. 2. 3. 4. 5. 6. 7. 8. stop Program // Displaying the sum of row and columns of a double dimension array. class arraycheck { int a[][]=new int[3][3]; Step Step Step Step Step Step Step Step 1 :- creating object with array 2 :- working of for loop 3 :- storing array 4 :- end of for loop 5 :- printing 6 :- working of for loop 7 :- printing array 8 :- end of for loop

int 1[][]=new int[3][3]; arraycheck() { int c=1; for (int i=0;i<3;i++) { for(int j=0;j<3;j++) {a[i][j]=c; c++;} } } void cal() { int srow=0,scol=0; // Display of the array System.out.println(The original array is ); System.out.println(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.println((a[i][j]+ ); } System.out.println(); } // Computing sum of rows for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { frow=srow+a[i][j]; } System.out.println(The sum of row +(i+1)+is+srow); srow=0; } int k=0; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { scol=scol+a[j][i]; k=j+1; } System.out.println(The sum of column +k+ is +scol); scol=0; } } // end of method } // end of class

Output The original array is

123 456 789 The sum of row 1 is 6 The sum of row 2 is 15 The sum of row 3 is 24 The sum of column 3 is 12 The sum of column 3 is 15 The sum of column 3 is 18

Q.45. WAP using insertion sorting technique Algorithm start 1. Step 2. Step 3. Step 4. Step 5. Step 6. Step 7. Step 8. Step 9. Step 10. Step 11. Step stop Program // Insertion Sort Method class linear{ protected int M[] = new int[10]; public void input(int num[]) // Entry of array elements { for (int i=0;i<10;i++) M[i]=num[i]; } public void sort() { int x=0,i=0,j=0,n=10,flag=0; // Display of array 1 :- creating object 2 :- working of for loop 3 :- printing 4 :- end of loop 5 :- working of for loop 6 :- storing values 7 :- working of while loop 8 :- storing array 9 :- end of while loop 10 :- end of for loop 11 :- printing array

System.out.println(The array is ); for(i=0;i<n;i++) { System.out.println(M[i]); } System.out.println(); for(int k=1;k>n;k++) { flag = M[k]; j=k-1; while(flag<M[j]) { M[j + 1] = M[j]; j=j-1; if (j<0) break; } M[j + 1]=flag; } System.out.println(The sorted array is ); for(i=0;i<n;i++) { System.out.println(M[i]+ ); } } // end of method } // end of class Output The array is 9876543210 The sorted array is 0 1 2 3 4 5 6 7 8 9 Q.46. WAP using election sorting technique Algorithm start 1. 2. 3. 4. 5. 6. Step Step Step Step Step Step 1 :- creating object 2 :- working of for loop 3 :- storing array 4 :- end of loop 5 :- working of for loop 6 :- printing array

7. Step 7 :- end of for loop stop

Program // Selection Sort Method class linear{ protected int M[] = new int[10]; public void input(int num[]) // Entry of array elements { for (int i=0;i<10;i++) M[i]=num[i]; } public void sort() { int x=0,i=0,j=0,n=10,flag=0; // Display of array System.out.println(The array is ); for(i=0;i<n;i++) { System.out.println(M[i]); } System.out.println(); // Selection Sort for(int k=0;k<n;k++) { for(int kk=k+1;kk<n;kk++) { if(M[k]>M[kk]) { flag = M[k]; M[k]=M[kk]; M[kk]=flag; } } } System.out.println(The sorted array is ); for(i=0;i<n;i++) { System.out.println(M[i]+ ); } } // end of method } // end of class

Output The array is 0123456789 The sorted array is 0 1 2 3 4 5 6 7 8 9 Q.47. WAP using bubble sorting technique Algorithm start 1. 2. 3. 4. 5. 6. 7. stop Program // Bubble Sort Method class linear{ protected int M[] = new int[10]; public void input(int num[]) // Entry of array elements { for (int i=0;i<10;i++) M[i]=num[i]; } public void sort() { int x=0,i=0,j=0,n=10,flag=0; // Display of array System.out.println(The array is ); Step Step Step Step Step Step Step 1 :- creating object 2 :- working of for loop 3 :- storing array 4 :- end of loop 5 :- working of for loop 6 :- printing array 7 :- end of for loop

for(i=0;i<n;i++) { System.out.println(M[i]); } System.out.println(); // Bubble Sort for(int k=0;k<n;k++) { for(int kk=0;kk<n;kk++) { if(M[k]>M[kk+1]) { flag = M[kk]; M[kk]=M[kk+1]; M[kk+1]=flag; } } } System.out.println(The sorted array is ); for(i=0;i<n;i++) { System.out.println(M[i]+ ); } } // end of method } // end of class Output The array is 9786543210 The sorted array is 0 1 2 3 4 5 6 7 8 9

Q.48. WAP to demonstrate multiple exception Algorithm start 1. Step 1 :- Storing value 2. Step 2 :- printing

3. 4. 5. 6. 7. stop

Step Step Step Step Step

3 :- storing value in c 4 :- removing error 5 :- printing 6 :- removing errors 7 :- printing

Program class ECC { //method public static void main(String args[]) { int a=50,b=0,c; try { System.out.println(args[0]); c=a/b; } catch(ArithmeticException e) { System.out.println(Division by 0 is not allowed); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(/n The argument through command line is not given); } } // end of method } // end of class

Output abc Division by 0 is not allowed If the command line instruction is given as: java ECC Output shall be : Argument through command line is not given Q.49. WAP to print area of rectangle and circle using interfaces Algorithm

start 1. 2. 3. 4. 5. 6. 7. 8. stop Program // definition of an interface as work interface work { final static flat p=2.322F; flat compute(flat x,flat y); } // definition of the class with interface implementation class rectangle implements work { public float compute(float x, flat y) { return (x*y); } } class circle implements work { public float compute(flat x,float y) { return (p*x*x); } } class p2 { public static void main(String args[]) { rectangle rect = new rectangle(); circle cir = new circle(); work area; area=rect; System.out.println(Area of rectangle =+Area.computer(5,6)); area = cir; System.out.println(Area of circle = +Area.compute(5,5)); } // end of method } // end of class Output Step Step Step Step Step Step Step Step 1 :- Storing value of float P 2 :- getting inputs 3 :- returing (X * Y) 4 :- getting inputs 5 :- returning ( P * X * X) 6 :- creating object 7 :- calling function 8 :- printing areas

Area of rectangle = 30 area of circle = 58.05 Q.50. WAP to find the factorial using recreation Algorithm start 1. 2. 3. 4. 5. 6. 7. stop Step Step Step Step Step Step Step 1 :- getting input n 2 :- checking if condition 3 :- if condition is true returning 4 :- storing result 5 :- returning result 6 :- creating object 7 :- printing the factorial

Program class Factorial { int fact(int n) { int result; if ( n ==1) return 1 ; result = fact (n-1) * n; return result; } } class Recursion { //method public static void main (String args[]) { Factorial f =new Factorial(); System.out.println(Factorial of 3 is + f.fact(3)); System.out.println(Factorial of 3 is + f.fact(4)); System.out.println(Factorial of 3 is + f.fact(5)); } // end of method } // end of class Output Factorial of 3 is 6 Factorial of 4 is 24 Factorial of 5 is120

You might also like