Oops PGM 2,3&4
Oops PGM 2,3&4
Oops PGM 2,3&4
DATE:
Aim:
To Develop a Stack data structures using Classes and Objects.
Algorithm:
Step1: Start the program
Step4: In the push() method, if the max element is greater than the stack size, print “Stack is full”
Step5: Else get the data and increment the top of the stack
Step6: In the pop() method, if the top of the stack is NULL, print “Stack is empty”
Step7: Else get the node data from the top of the stack and pop it out. Decrement top of stack
1
Program:
import java.util.Scanner;
class StackExample {
int number[];
int top;
int limit;
public StackExample(int size){
top =-1;
limit =size;
number=new int[size];
}
void push(int num)
{
if(isFull())
System.out.println("Stack is full");
else
{
top=top+1;
number[top]=num;
}
}
void pop(){
if(isEmpty())
System.out.println("Stack is Empty");
else{
top=top-1;
System.out.println("Last element popped out");
}
}
void peek()
{
System.out.println("Top most element in Stack ="+number[top]);
}
boolean isFull(){
return top >=limit-1;
}
boolean isEmpty(){
return top==-1;
}
void print() {
System.out.println("Elements in Stack:-");
for(int i=top;i>=0;i--)
System.out.println(number[i]);
}
switch(option){
case 1:
System.out.print("Enter the element to insert = ");
element=input.nextInt();
obj.push(element);
break;
case 2:
obj.pop();
break;
case 3:
obj.peek();
break;
case 4:
obj.print();
break;
default:
System.out.println("Choose wrong option ");
}
System.out.println("Want to continue? y or n ");
chr=input.next().charAt(0);
}
while(chr=='y'||chr=='Y');
}
}
3
Output:
4
Want to continue? y or n
y
Enter any number to perform stack operations:-
1. Insert an element
2. Pop an element
3. Top of the stack
Result:
Thus the implementation of Stack using classes and objects is executed successfully and output is verified.
5
EX NO:3 IMPLEMENTATION OF QUEUE
DATE:
Aim:
To Develop a Queue data structures using Classes and Objects.
Algorithm:
Step 1: Start the program
Step 4: In the insert() function, if the rear value is greater than the max size specified, print “Queue is full”
Step 6: In the delete() function, if the rear value is equal to the front value, print “Queue is empty”
Step 7: Else shift the front pointer to point to the link of front, delete the node & update the rear node link
pointer to the new front
Step 8: In the display() function, if rear value is equal to the front value , print “ Queue is empty”
6
Program:
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 boolean isFull()
{
return front==0 && rear == size -1 ;
}
public int getSize()
{
return len ;
}
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;
7
}
else
front++;
return ele;
}
}
case 3 :
try
{
System.out.println("front Element in queue = "+q.peek());
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+q.isEmpty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.getSize());
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');
}
}
9
Output:
Enter the Queue size
4
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
1
Enter element to insert
44
Queue = 44
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
1
Enter element to insert
66
Queue = 44 66
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
1
Enter element to insert
88
Queue = 44 66 88
Queue Operations
1. insert
2. remove
10
3. Display
4. check empty
5. check full
6. Queue size
1
Queue = 44 66 88 53
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
1
Enter element to insert
75
Error : Overflow Exception
Queue = 44 66 88 53
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
3
front Element in queue = 44
Queue = 44 66 88 53
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
2
Removed Element in Queue = 44
11
Queue = 66 88 53
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
4
Empty status = false
Queue = 66 88 53
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
5
Full status = false
Queue = 66 88 53
Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
6
Size = 3
Queue = 66 88 53
Thus the implementation of Queue using classes and objects is executed successfully and output is verified
13
EX NO:4 PAYROLL SYSTEM USING INHERITANCE
DATE:
Aim:
To develop a java application to generate pay slip for different category of employees using
the concept of Inheritance.
Algorithm:
Step1:Create the class employee with name, Empid, address, mail id, mobile no as members.
Step2:Inherit the classes programmer, Asst professor, Associate professor and professor from
Step3: Add Basic Pay (BP) as the member of all the inherited classes.
Step4: Calculate DA as 97% of BP, HRA as 10% of BP, PF as 12% of BP, Staff club fund as
Step7: Create the objects for the inherited classes and invoke the necessary methods to
14
Program:
import java.util.Scanner;
class employee
{
int empid;
long mobile;
String name, address, mailid;
Scanner get = new Scanner(System.in);
void getdata()
{
System.out.println("Enter Name of the Employee");
name = get.nextLine();
System.out.println("Enter Mail id");
mailid = get.nextLine();
System.out.println("Enter Address of the Employee:");
address = get.nextLine();
System.out.println("Enter employee id ");
empid = get.nextInt();
System.out.println("Enter Mobile Number");
mobile = get.nextLong();
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
System.out.println("Mobile Number: "+mobile);
}
}
class programmer extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateprog()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR PROGRAMMER");
System.out.println("************************************************");
System.out.println("Basic Pay: Rs"+bp);
System.out.println("DA: Rs"+da);
System.out.println("PF: Rs"+pf);
System.out.println("HRA: Rs"+hra);
System.out.println("CLUB: Rs"+club);
System.out.println("GROSS PAY: Rs"+gross);
System.out.println("NET PAY: Rs"+net);
}
}
15
class asstprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateasst()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR ASSISTANT PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay: Rs"+bp);
System.out.println("DA: Rs"+da);
System.out.println("HRA: Rs"+hra);
System.out.println("PF: Rs"+pf);
System.out.println("CLUB: Rs"+club);
System.out.println("GROSS PAY: Rs"+gross);
System.out.println("NET PAY: Rs"+net);
}
}
class associateprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateassociate()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR ASSOCIATE PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay: Rs"+bp);
System.out.println("DA: Rs"+da);
System.out.println("HRA: Rs"+hra);
System.out.println("PF: Rs"+pf);
System.out.println("CLUB: Rs"+club);
System.out.println("GROSS PAY :Rs"+gross);
System.out.println("NET PAY: Rs"+net);
}
}
class professor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
16
void getprofessor()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateprofessor()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay: Rs"+bp);
System.out.println("DA: Rs"+da);
System.out.println("HRA: Rs"+hra);
System.out.println("PF: Rs"+pf);
System.out.println("CLUB: Rs"+club);
System.out.println("GROSS PAY: Rs"+gross);
System.out.println("NET PAY: Rs"+net);
}
}
class Salary
{
public static void main(String args[])
{
int choice,cont;
do
{
System.out.println("PAYROLL SYSTEM");
System.out.println(" 1.PROGRAMMER \t 2.ASSISTANT PROFESSOR \t "
+ "3.ASSOCIATE PROFESSOR \t 4.PROFESSOR ");
Scanner c = new Scanner(System.in);
choice=c.nextInt();
switch(choice)
{
case 1:
{
programmer p=new programmer();
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
}
case 2:
{
asstprofessor asst=new asstprofessor();
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();
break;
}
case 3:
17
{
associateprofessor asso=new associateprofessor();
asso.getdata();
asso.getassociate();
asso.display();
asso.calculateassociate();
break;
}
case 4:
{
professor prof=new professor();
prof.getdata();
prof.getprofessor();
prof.display();
prof.calculateprofessor();
break;
}
}
System.out.println(" Do u want to continue 0 to quit and 1 to continue ");
cont=c.nextInt();
}
while(cont==1);
}
}
18
Output:
19
Result:
Thus the java application to generate pay slips for different categories of employees was implemented using
20