You are on page 1of 22

TABLE OF CONTENTS

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:

public class prime {

public static void main(String[] args) {

int h = 39;
boolean s = false;

for (int i = 2; 1 <= h/ 2;) {


if (h % i==0)

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:

public class factorial {

public static void main(String[] args) {

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;

for (int i = str.length() - 1; i >= 0; i--) {


rev = rev + str.charAt(i);
}
if (str.equals(rev)) {
ans = true;
}
return ans;
}
public static void main(String[] args)
{

String str = "Sharma";


str = str.toLowerCase();
boolean A = isPalindrome(str);
System.out.println(A);
}
}

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:

public class kanak {


public static void main(String args[]) {
int a, b, c, temp, result ;
a=10;
b=20;
c=30;

temp = a < b ? a:b;


result = c > temp ? c:temp;
System.out.println("largest number is :: "+result);
}
}

8
Output:

9
Q. WAP to implement string functions.
Sol.
import java.util.*;

class StringOperation

public static void main(String[] args)

String first="",second="";

Scanner sc=new Scanner(System.in);

System.out.println("String Operation");

System.out.println();

System.out.print("Enter the first Sting: ");

first=sc.nextLine();

System.out.print("Enter the second Sting: ");

second=sc.nextLine();

System.out.println("The strings are: "+first+" , "+second);

System.out.println("The length of the first string is :"+first.length());

System.out.println("The length of the second string is :"+second.length());

System.out.println("The concatenation of first and second string is :"+first.concat(" "+second));

System.out.println("The first character of " +first+" is: "+first.charAt(0));

System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());

System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());

System.out.print("Enter the occurance of a character in "+first+" : ");

String str=sc.next();

char c=str.charAt(0);

System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first);

System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7));

System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));

boolean check=first.equals(second);

if(!check)

System.out.println(first + " and " + second + " are not same.");

else

System.out.println(first + " and " + second + " are same.");

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 {

public void print_Pine()

System.out.println("Pine");

class two extends one {

public void print_Apple() {

System.out.println("Apple");

class main {

public static void main(String[] args)

two g = new two();

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;
}

static double Multiply(double a, double b)


{
return a * b;
}
public static void main(String[] args)
{
System.out.println(poly.Multiply(5, 3));
System.out.println(poly.Multiply(2.3, 2.6));
}
}

16
Output:

17
Q.10 WAP to implement Abstraction through
Abstract Class.
Sol.

abstract class Animal {


public abstract void animalSound();
public void sleep() {
System.out.println("Zzzzzz");}}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: Bhow Bhow Bhow");}}
class Main {
public static void main(String[] args) {
Dog mydog = new Dog();
mydog.animalSound();
mydog.sleep();
}
}

18
Output:

19
Q.11 WAP to implement Encapsulation.
Sol. First Class is encapsulation:

package kanak;

class encapsulation {

private int age;

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}
}

Second Class is encap:

package kanak;

public class encap {

public static void main(String[] args)


{
encapsulation p1=new
encapsulation();
p1.setAge(18);
System.out.println("My age is " +
p1.getAge());
}
}

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

You might also like