Oops PGM 2,3&4

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

EX NO:2 IMPLEMENTATION OF STACK

DATE:

Aim:
To Develop a Stack data structures using Classes and Objects.

Algorithm:
Step1: Start the program

Step2: Include the header files

Step3: Choose the choice from the menu

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

Step8: In the print() method, display the contents of the stack

Step9: Stop the program

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

public static void main(String[] args) {


Scanner input =new Scanner(System.in);
int size,option,element;
char chr;
System.out.print("Enter the maximum size of a stack = ");
size=input.nextInt();
StackExample obj= new StackExample(size);
do{

System.out.println("Enter any number to perform stack operations: -"


2
+ "\n 1. Insert an element "
+ "\n 2. Pop an element"
+ "\n 3. Top of the stack"
+ "\n 4. Display the elements of the stack");
option=input.nextInt();

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:

Enter the maximum size of a stack = 7


Enter any number to perform stack operations: -
1. Insert an element
2. Pop an element
3. Top of the stack
4. Display the elements of the stack
1
Enter the element to insert = 78
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
4. Display the elements of the stack
1
Enter the element to insert = 24
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
4. Display the elements of the stack
1
Enter the element to insert = 52
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
4. Display the elements of the stack
1
Enter the element to insert = 87
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
4. Display the elements of the stack
4
Elements in Stack:-
87
52
24
78
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
4. Display the elements of the stack
3
Top most element in Stack =87

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

4. Display the elements of the stack


2
Last element popped out
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
4. Display the elements of the stack
4
Elements in Stack:-
52
24
78
Want to continue? y or n

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 2: Include the header files

Step 3: Read the user choice

Step 4: In the insert() function, if the rear value is greater than the max size specified, print “Queue is full”

Step 5: Else insert the data and update the rear

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”

Step 9: Else display the values in the Queue

Step 10: Stop the program

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

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("Enter the Queue size ");
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");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. Queue size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter element to insert");
try
{
q.insert( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try
{
System.out.println("Removed Element in Queue = "+q.remove());
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
8
}
break;

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

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

Queue Operations
1. insert
2. remove

10
3. Display
4. check empty
5. check full
6. Queue size
1

Enter element to insert


53

Queue = 44 66 88 53

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

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

Do you want to continue (Type y or n)

Queue Operations
1. insert
2. remove
3. Display
4. check empty
5. check full
6. Queue size
6
Size = 3

Queue = 66 88 53

Do you want to continue (Type y or n)


12
Result:

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

from employee class.

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

0.1% of Basic Pay.

Step5: Calculate gross salary and net salary.

Step6: Generate payslip for all categories of employees.

Step7: Create the objects for the inherited classes and invoke the necessary methods to

display the payslip.

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

inheritance and the program was executed successfully.

20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy