Professional Documents
Culture Documents
S.no. Page
Title No.
WAP to Find Whether a 1,2
1. given Number is Prime
Number or Not.
WAP to find Factorial of 3
2. a Number.
WAP to check whether a
3. Given String is
4,5
Palindrome.
WAP to build 6,7
4. Calculator using If-
else Ladder.
WAP to Find Largest 8,9
5. among three No using
Ternary Operator.
WAP to implement string 10,11
6. functions.
Create a Java Program 12,13
7. to Sort an Array of
Numeric or String Data.
WAP to implement 14,15
8. Inheritance.
WAP to implement 16,17
9. Polymorphism.
WAP to implement 18,19
10. Abstraction through
Abstract Class.
WAP to implement 20
11. Encapsulation.
WAP to implement 21
12. Exception Handling.
Q1 WAP to find Whether a given Number is Prime
Number or Not.
Sol:
int h = 39;
boolean s = false;
s= true;
break;
}
if (s!=true)
System.out.println(h +" is a prime number.");
else
System.out.println(h + " is not a prime number.");
}
1
Output:
2
Q2 WAP to find Factorial of a Number.
sol:
int num = 6;
int factorial = 1;
for(int i =1; i<=num; ++i)
{
factorial = factorial * i;
}
System.out.printf("factorial of %d = %d", num, factorial);
}
}
Output:
3
Q3 WAP to check whether a given string is
palindrome.
Sol:
import java.io.*;
class kanak{
public static boolean isPalindrome(String str)
{
String rev = "";
boolean ans = false;
4
Output:
5
Q4 WAP to build calculator using if -else
ladder.
Sol:
import java.util. Scanner;
public class calculator {
public static void main(String[] args) {
try (Scanner i = new Scanner(System.in)) {
System.out.println("Enter First Number 1 and Second Number 2");
double a= i.nextDouble();
double b=i.nextDouble();
System.out.println("Enter 0 For Sum ,1 For Sub, 2 For multi, 3 For Div , 4 For
Rem ");
double c = 0;
int kanak= i.nextInt();
if(kanak==0) c=a+b;
else if(kanak==1) c=a-b;
else if(kanak==2) c=a*b;
else if(kanak==3) c=a/b;
else if(kanak==4) c=a%b;
else System.out.println("invalid symbol so incorrect output");
System.out.println("The " +kanak +" of " +a +" " +b +" is " +c);
}
}
}
6
Output:
7
Q5 WAP to find the largest among three numbers
using ternary operator.
Sol:
8
Output:
9
Q. WAP to implement string functions.
Sol.
import java.util.*;
class StringOperation
String first="",second="";
System.out.println("String Operation");
System.out.println();
first=sc.nextLine();
second=sc.nextLine();
String str=sc.next();
char c=str.charAt(0);
System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7));
boolean check=first.equals(second);
if(!check)
else
10
Output:
11
Q7 Create a java program to sort an array of Numeric or
string data
Sol:
import java.util.Arrays;
public class Exercise1 {
public static void main(String[] args){
int[] my_array1 = {
1789, 2035, 1899, 1456, 2013, };
String[] my_array2 = {
"Java",
"Python",
"PHP",
"C#",
"C Programming",
"C++"
};
Arrays.sort(my_array1);
System.out.println("Sorted numeric array : "+Arrays.toString(my_array1));
Arrays.sort(my_array2);
System.out.println("Sorted string array : "+Arrays.toString(my_array2));
}
}
12
Output:
13
Q8 WAP to implement inheritance.
Sol:
import java.io.*;
import java.lang.*;
import java.util.*;
class one {
System.out.println("Pine");
System.out.println("Apple");
class main {
g.print_Pine();
g.print_Apple();
g.print_Pine();
14
Output:
15
Q.9 WAP to implement Polymorphism.
Sol.
class poly {
static int Multiply(int a, int b)
{
return a * b;
}
16
Output:
17
Q.10 WAP to implement Abstraction through
Abstract Class.
Sol.
18
Output:
19
Q.11 WAP to implement Encapsulation.
Sol. First Class is encapsulation:
package kanak;
class encapsulation {
package kanak;
Output:
20
Q.12 WAP to implement Exception Handling.
Sol.
public class error {
public static void main(String[] args) {
try{
int data=100/0;
}
catch(ArithmeticException i)
{
System.out.println(i);
}
System.out.println("Please read the code again");
}
}
Output:
21