Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Program -1

Question: Write a program to find factorial of a number.

Source Code:

import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
Program -2

Question: Write a program to check for palindrome.

Source Code:

import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("The string is a palindrome.");
else
System.out.println("The string isn't a palindrome.");
}
}
Program -3(a)
Question: Write a program to implement stack.
Source Code:
import java.util.*;
class arrayStack
{ protected int arr[];
protected int top, size, len;
public arrayStack(int n)
{ size = n;
len = 0;
arr = new int[size];
top = -1;
}
public boolean isEmpty()
{ return top == -1;
}
public int peek()
{ if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return arr[top];
}
public void push(int i)
{ if(top + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(top + 1 < size )
arr[++top] = i;
len++ ;
}
public int pop()
{ if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
len-- ;
return arr[top--];
}
public void display()
{ System.out.print("\nStack = ");
if (len == 0)
{ System.out.print("Empty\n");
return ;
}
for (int i = top; i >= 0; i--)
System.out.print(arr[i]+" ");
System.out.println();
}
}
public class StackImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Stack Test\n");
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
arrayStack stk = new arrayStack(n);
char ch;
do{
System.out.println("\nStack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. display");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to push");
stk.push( scan.nextInt() );
break;
case 2 :
System.out.println("Popped Element = " + stk.pop());
break;
case 3 :
System.out.println("Peek Element = " + stk.peek());
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
stk.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Program -3(b)
Question: Write a program to implement queue.
Source Code:
import java.util.*;
class arrayQueue
{ protected int Queue[] ;
protected int front, rear, size, len;
public arrayQueue(int n)
{ size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
public boolean isEmpty()
{ return front == -1;
}
public int peek()
{ if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
public void insert(int i)
{ if (rear == -1)
{ front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
public int remove()
{ if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
{ len-- ;
int ele = Queue[front];
if ( front == rear)
{ front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
public void display()
{ System.out.print("\nQueue = ");
if (len == 0)
{
System.out.print("Empty\n");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i]+" ");
System.out.println();
}
}
public class QueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Queue Test\n");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
arrayQueue q = new arrayQueue(n);
char ch;
do{
System.out.println("\nQueue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. display");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
q.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Removed Element = "+q.remove());
break;
case 3 :
System.out.println("Peek Element = "+q.peek());
break;
default : System.out.println("Wrong Entry \n ");
break;
}
q.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Program -4(a)

Question: Write a program to implement static polymorphism.

Source Code:

class DemoOverload{
public int add(int x, int y){ //method 1
return x+y;
}
public int add(int x, int y, int z){ //method 2
return x+y+z;
}
public int add(double x, int y){ //method 3
return (int)x+y;
}
public int add(int x, double y){ //method 4
return x+(int)y;
}
}
class StaticPolymorphism{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
System.out.println(demo.add(2,3)); //method 1 called
System.out.println(demo.add(2,3,4)); //method 2 called
System.out.println(demo.add(2,3.4)); //method 4 called
System.out.println(demo.add(2.5,3)); //method 3 called
}
}
Program -4(b)

Question: Write a program to implement dynamic polymorphism.

Source Code:

class Vehicle{
public void move(){
System.out.println("Vehicles can move");
}
}
class MotorBike extends Vehicle{
public void move(){
System.out.println("MotorBike can move and accelerate too");
}
}
class DynamicPolymorphism{
public static void main(String[] args){
Vehicle vh=new MotorBike();
vh.move(); // prints MotorBike can move and accelerate too!!
vh=new Vehicle();
vh.move(); // prints Vehicles can move!!
}
}

You might also like