0% found this document useful (0 votes)
45 views

21bce2213 Java Ex4

The document contains 5 questions related to Java programming. For question 1, the student wrote programs to implement a List and Stack to store employee data using generics. For question 2, the student created an Arithmetic class using delegates to perform addition, subtraction, multiplication and division. For question 3, the student designed a BankAccount class with methods to assign values, deposit, withdraw and display balance. For question 4, the student demonstrated the use of static and non-static class members. For question 5, the student wrote a program to calculate total sales at a supermarket by getting customer purchase details.

Uploaded by

THE EXPLAINER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

21bce2213 Java Ex4

The document contains 5 questions related to Java programming. For question 1, the student wrote programs to implement a List and Stack to store employee data using generics. For question 2, the student created an Arithmetic class using delegates to perform addition, subtraction, multiplication and division. For question 3, the student designed a BankAccount class with methods to assign values, deposit, withdraw and display balance. For question 4, the student demonstrated the use of static and non-static class members. For question 5, the student wrote a program to calculate total sales at a supermarket by getting customer purchase details.

Uploaded by

THE EXPLAINER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

BCSE103E- COMPUTER PROGRAMMING:

JAVA (EMBEDDED LAB)


L15 + L16 + L29 + L30

Exercise 4

NAME: Ishaan Purohit


REGISTRATION NUMBER: 21BCE2213
Q1) Write a java program to implement following data structure using generics for
storing employee personal information
1) List:
(i) Add
(ii) Insert
Code
import java.util.*;
public class EmployeeDataList{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
List<Integer> lst1 = new ArrayList<Integer>();
List<Integer> lst2 = new ArrayList<Integer>();
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) + ": ");
list1.add(sc.nextInt());
System.out.print("Employee Salary of Employee " + (i + 1) + ": ");
list2.add(sc.nextInt());
}
System.out.println();
System.out.println("Employee information retrieved from List ");
for(int i = 0;i<n;i++){
System.out.println("Employee ID of Employee " + (i + 1) + " "+ lst1.get(i));
System.out.println("Employee Salary of Employee " + (i + 1) +" " + lst2.get(i));
}
}
}
Output

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 class VariousClassMembers{

private int nSC = 0;

private static int sC = 0;

public VariousClassMembers(){

nSC++;

sC++;

void printNumbers(){

System.out.println("nonStaticCounter after n increments is : " + nSC);

System.out.println("staticCounter after n increments is : " + sC);

void someStaticFunction(){

System.out.println("Square of staticCounter is: "+sC*sC);

void someNonStaticFunction(){

System.out.println("Square of non-static counter is: " + nSC*nSC);

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of iterations: ");

int n = sc.nextInt();

VariousClassMembers object1 = new VariousClassMembers();

for(int i = 0;i<n-1;i++){

object1 = new VariousClassMembers();

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;

class purchase extends customerDetails{

int items;

int amt;

static int cost;

static int sold;

void setDetails(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");

System.out.println("No. of items purchased: " + items);

System.out.println("Purchase amount: " + amt);

void total(){

System.out.println("Total amount received: " + cost);

System.out.println("Total number of items sold: " + sold);

public class q5{

public static void main(String[] args){

Scanner scanner = new Scanner(System.in);

System.out.println("Enter no. of Customer: ");

int n = scanner.nextInt();

purchase arr[] = new purchase[n];

for(int i=0; i<n; i++){

System.out.println("Enter the name of the customer: ");

String name = scanner.next();

System.out.println("Enter the No of Items purchased: ");

int items = scanner.nextInt();

System.out.println("Enter the purchase amount: ");

int amt = scanner.nextInt();

arr[i] = new purchase();

arr[i].setDetails(name, items, amt);

System.out.println("Purchase Details: ");

for(int i=0; i<n; i++){

System.out.print("Customer "+ (i+1)+": ");

arr[i].display();

}
arr[0].total();

Output

You might also like