21bce2213 Java Ex4
21bce2213 Java Ex4
Exercise 4
2) Stack:
(i) Push()
(ii) Pop()
Code
import java.util.*;
public class EmployeeDataStack{
static class Stack {
private int arr[];
private int top;
private int capacity;
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
public void push(int x) {
if (top == capacity) {
System.out.println("Stack OverFlow");
return;
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
public int pop() {
if (top == -1) {
System.out.println("Stack is Empty");
return -1;
}
return arr[top--];
}
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}
}
public static void main(String args[]){
Stack stEmployeeID = new Stack(100);
Stack stEmployeeSalary = new Stack(100);
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Employees: ");
int n = sc.nextInt();
System.out.println("Enter employee info: ");
for(int i = 0;i<n;i++){
System.out.print("Employee ID of Employee " + (i + 1) + ": ");
stEmployeeID.push(sc.nextInt());
System.out.print("Employee Salary of Employee " + (i + 1) + ": ");
stEmployeeSalary.push(sc.nextInt());
}
System.out.println();
System.out.println("Employee information retrieved from stack: ");
for(int i = 0;i<n;i++){
System.out.println("Employee ID of Employee " + (i + 1) + " "+ stEmployeeID.pop());
System.out.println("Employee Salary of Employee " + (i + 1) +" " + stEmployeeSalary.pop());
}
}
}
Output
Q2) Create a class called Arithmetic that include data members Number1, Number2, Number3 and
methods that could implement the following tasks using delegates
(i) Addition
(ii) Subtraction
(iii) Multiplication
(iv) Division
Code
import java.util.*;
class ArithFns{
static class Delegates{
void add(float number1,float number2,float number3)
{
System.out.println(number1 + number2 + number3);
}
void subtract(float number1,float number2,float number3)
{
System.out.println(number1 - number2 - number3);
}
void multiply(float number1,float number2,float number3)
{
System.out.println(number1*number2*number3);
}
void divide(float number1,float number2,float number3)
{
System.out.println((number1/number2)/number3);
}
}
static class Arithmetic{
float number1;
float number2;
float number3;
Delegates a = new Delegates();
void add(float number1,float number2,float number3)
{
a.add(number1, number2, number3);
}
void subtract(float number1,float number2,float number3)
{
a.subtract(number1, number2, number3);
}
void multiply(float number1,float number2,float number3)
{
a.multiply(number1, number2, number3);
}
void divide(float number1,float number2,float number3)
{
a.divide(number1, number2, number3);
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
float number1 = sc.nextFloat();
System.out.print("Enter second number: ");
float number2 = sc.nextFloat();
System.out.print("Enter third number: ");
float number3 = sc.nextFloat();
Arithmetic a = new Arithmetic();
System.out.print("Addition result : ");
a.add(number1, number2, number3);
System.out.print("Subtraction result : ");
a.subtract(number1, number2, number3);
System.out.print("Multiplication result : ");
a.multiply(number1, number2, number3);
System.out.print("Divide result : ");
a.divide(number1, number2, number3);
}
}
Output
Q3) Design a class to represent a bank account including the following:
Data members:
• Name of the depositor
• Account Number’
• Type of Account
• Balance amount in the account
Methods
• To assign initial values
• To deposit an amount
• TO withdraw an amount after checking the balance
• To display the name and balance
Code
import java.util.*;
class BankAcc{
static class Bank{
String name_Holder;
int accno;
String typeOfAccount;
double balInAcc;
Scanner sc = new Scanner(System.in);
Bank(){
balInAcc = 0;
}
void assignValues(){
System.out.print("Enter name of depositor ");
name_Holder = sc.nextLine();
System.out.print("Enter type of account ");
typeOfAccount = sc.nextLine();
System.out.print("Enter account number ");
accno = sc.nextInt();
System.out.print("Enter balance in account ");
balInAcc = sc.nextDouble();
}
void deposit(){
System.out.print("Enter amount to deposit ");
double d = sc.nextDouble();
balInAcc += d;
}
void withdraw(){
System.out.println("Current balance in account " +balInAcc);
System.out.print("Enter amount to withdraw ");
double d = sc.nextDouble();
balInAcc -=d;
System.out.println(d + " was withdrawn from your account.");
}
void display(){
System.out.println("Account Holder's name : " +name_Holder);
System.out.println("Current Balance in the account : " +balInAcc);
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Bank bank = new Bank();
bank.assignValues();
System.out.println();
bank.deposit();
System.out.println();
bank.withdraw();
System.out.println();
bank.display();
}
}
Output
Q4) Write a program to demonstrate the working of the various class members
Code
import java.util.*;
public VariousClassMembers(){
nSC++;
sC++;
void printNumbers(){
void someStaticFunction(){
void someNonStaticFunction(){
int n = sc.nextInt();
for(int i = 0;i<n-1;i++){
object1.printNumbers();
object1.someNonStaticFunction();
object1.someStaticFunction();
sc.close();
}Output
Q5) Supermarket is a retail facility with a wide range of products under one roof, including full
groceries and general merchandise. They satisfy the customers in all their routine shopping needs in
one trip. Raja owns a supermarket in Delhi and he wanted to create software that contains the
purchase details of all the customers made on that day at the end of the day, he wants to calculate
the total amount received and the total number of items purchased by all the customers in a
database. Help him to do this by writing a java program. With following constraints
1. Create a class called customer details with the following data members like customer id, and
customer name, and create a method to get the details of the customer.
2. Create a child class called “purchase” and check the following constraints. · Minimum 3 products
purchased by the customer and total rate of the three products should be greater than 100
3 Create a driver class called Main. In the main method, get the details of n number of customers.
Print the string “Purchase Details:” and display all the customer details and total amount and the
total number of items details in the display method.
Code
import java.util.Scanner;
class customerDetails{
int id;
String name;
int items;
int amt;
this.name = name;
this.items = items;
this.amt = amt;
cost += items;
sold += amt;
void display(){
System.out.print(name+"\n");
void total(){
int n = scanner.nextInt();
arr[i].display();
}
arr[0].total();
Output