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

BCA - Java Lab File

The document discusses several Java programs implemented as part of a lab practical file. It includes the aims, source code, and output for 6 programs covering topics like arithmetic operations using switch case, character checking, variable arguments in functions, wrapper classes, collections, and abstract classes/interfaces.

Uploaded by

SHUBHAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

BCA - Java Lab File

The document discusses several Java programs implemented as part of a lab practical file. It includes the aims, source code, and output for 6 programs covering topics like arithmetic operations using switch case, character checking, variable arguments in functions, wrapper classes, collections, and abstract classes/interfaces.

Uploaded by

SHUBHAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

GALGOTIAS UNIVERSITY

SCHOOL OF COMPUTING SCIENCE & ENGINEERING

Lab Practical File

Name: SHUBHAM KUMAR SINGH


Adm. Number: 21SCSE1040169
Course: Programming in JAVA
Course code: E1UA505C
Program: BCA
Semester: 5th

Submitted to: Dr. Pramod Kumar Soni


INDEX
S. Experiment Date Signature
No.

1 Write a Java Program to perform the arithmetic 9/08/2023


operations using switch case

2 Write a program to check the input character for 16/08/2023


uppercase, lowercase, no. of digits and other
characters
3 Demonstration of Programs to Understand 23/08/2023
variable arguments in function using for(…)

4 Write a Program to implement WrapperClass 6/09/2023

5 Write a Program to implementCollections. 11/09/2023

6 Demonstration and implementation of programs 18/10/2023


to understand abstract classes and interfaces.

7 Implementation of Programs to Understand the 25/10/2023


concept of Overriding.

8 Implement Bank Transaction code fordeposit and 1/11/2023


withdrawal but synchronization should be
implemented..
9 Implement Bank Transaction code for deposit 08/11/2023
and withdrawal using the concept of
Multithreading.
10 Write a Program to implement HashMap. 08/11/2023

11 WAP for removing repeated characters instring. 22/11/2023

12 WAP for counting frequency ofcharacters in 22/11/2023


String
Program No. 1

Aim: Write a Java Program to perform the arithmetic operations using switch case

Source Code (If Statement) :

import java.util.Scanner;

class Main {
public static void main(String[] args) {

char operator;
Double number1, number2, result;

// create an object of Scanner class


Scanner input = new Scanner(System.in);

// ask users to enter operator


System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);

// ask users to enter numbers


System.out.println("Enter first number");
number1 = input.nextDouble();

System.out.println("Enter second number");


number2 = input.nextDouble();

switch (operator) {

// performs addition between numbers


case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;

// performs subtraction between numbers


case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;

// performs multiplication between numbers


case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;

// performs division between numbers


case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;

default:
System.out.println("Invalid operator!");
break;
}

input.close();
}
}

Output :

Choose an operator: +, -, *, or /
*
Enter first number
3
Enter second number
9
3.0 * 9.0 = 27

Choose an operator: +, -, *, or /
+
Enter first number
21
Enter second number
8
21.0 + 8.0 = 29.0

Choose an operator: +, -, *, or /
-
Enter first number
9
Enter second number
3
9.0 - 3.0 = 6.0
Program No. 2

Aim: Write a program to check the input character for uppercase, lowercase, no. of
digits and other characters.

Source Code:

#include <stdio.h>
#include <ctype.h> /* Used for isupper() and islower() */

int main()
{
char ch;

/* Input character from user */


printf("Enter any character: ");
scanf("%c", &ch);

if(isupper(ch))
{
printf("'%c' is uppercase alphabet.", ch);
}
else if(islower(ch))
{
printf("'%c' is lowercase alphabet.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}

return 0;
}

Output:

Enter any character: C

'C' is uppercase alphabet


Program No. 3

Aim: Demonstration of Programs to Understand variable arguments in function using for(…)

Source Code :
package com.examples.experiments;

public class EXPVarargs {

static void fun(int... a)


{
System.out.println("Number of arguments: "
+ a.length);

// using for each loop to display contents of a


for (int i : a)
System.out.print(i + " ");
System.out.println();
}

// Driver code
public static void main(String args[])
{
// Calling the varargs method with
// different number of parameters

// one parameter
fun(100);

// four parameters
fun(1, 2, 3, 4);

// no parameter
fun();
}
}
Output:

Results: Program Executed Successfully.


Program No. 4

Aim: Write a Program to implement Wrapper Class

Source Code :
package com.examples.experiments;

import java.util.Scanner;

class Employee {
private int empid;
private String name;
private String empdept;

public int getEmpId()


{
return empid;
}
public void setEmpId(int empid)
{
this.empid = empid++;
}

public String getEmpName()


{
return name;
}
public void setEmpName(String name)
{
this.name = name;
}

public String getEmpDept()


{
return empdept;
}
public void setEmpDept(String empdept)
{
this.empdept = empdept;
}

public String toString()


{
String str = "\n1. Id: " + getEmpId() + "\n2. Name: " + getEmpName() + "\n3.
Department: " + getEmpDept();
return str;
}
}

public class EXP4 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
Employee emp = new Employee();
int i;
int ID = 101;
System.out.println("\t\tEmployee Details:\n");
for (i = 0; i<10; i++)
{
emp.setEmpId(ID);
ID++;
System.out.print("\nEnter Employee Name: ");
emp.setEmpName(sc.nextLine());
emp.setEmpDept("CSE");

System.out.println(emp.toString());
}

}
}
Output:

Results: Program Executed Successfully.


Program No. 5

Aim: Write a Program to implement Collections.

Source Code :
package com.example.collectiondemo;
import java.util.Hashtable;
import java.util.Vector;

public class CollectionDemo {


public static void main(String[] args){
int arr[] = new int[] { 1, 2, 3, 4 };
Vector<Integer> v = new Vector();
Hashtable<Integer, String> h = new Hashtable();

v.addElement(1);
v.addElement(2);
h.put(1, "Swapnesh");
h.put(2, "3Swapnesh");

System.out.println(arr[0]);
System.out.println(v.elementAt(0));
System.out.println(h.get(1));
}
}

Output:

Results: Program Executed Successfully.


Program No. 6

Aim: Demonstration and implementation of programs to understand abstract classes and


interfaces.

Source Code (Abstract Class):


package com.examples.experiments;

abstract class Shape {

// Declare fields
String objectName = " ";

// Constructor of this class


Shape(String name) { this.objectName = name; }

// Method
// Non-abstract methods
// Having as default implementation
public void moveTo(int x, int y)
{
System.out.println(this.objectName + " "
+ "has been moved to"
+ " x = " + x + " and y = " + y);
}

// Method 2
// Abstract methods which will be
// implemented by its subclass(es)
abstract public double area();
abstract public void draw();
}

// Class 2
// Helper class extending Class 1
class Rectangle extends Shape {

// Attributes of rectangle
int length, width;
// Constructor
Rectangle(int length, int width, String name)
{

// Super keyword refers to current instance itself


super(name);

// this keyword refers to current instance itself


this.length = length;
this.width = width;
}

// Method 1
// To draw rectangle
@Override public void draw()
{
System.out.println("Rectangle has been drawn ");
}

// Method 2
// To compute rectangle area
@Override public double area()
{
// Length * Breadth
return (double)(length * width);
}
}

// Class 3
// Helper class extending Class 1
class Circle extends Shape {

// Attributes of a Circle
double pi = 3.14;
int radius;

// Constructor
Circle(int radius, String name)
{
// Super keyword refers to parent class
super(name);
// This keyword refers to current instance itself
this.radius = radius;
}

// Method 1
// To draw circle
@Override public void draw()
{
// Print statement
System.out.println("Circle has been drawn ");
}

// Method 2
// To compute circle area
@Override public double area()
{
return (double)((pi * radius * radius));
}
}

public class EXP6Abstract {


public static void main(String[] args)
{
// Creating the Object of Rectangle class
// and using shape class reference.
Shape rect = new Rectangle(2, 3, "Rectangle");

System.out.println("Area of rectangle: "


+ rect.area());

rect.moveTo(1, 2);

System.out.println(" ");

// Creating the Objects of circle class


Shape circle = new Circle(2, "Circle");

System.out.println("Area of circle: "


+ circle.area());

circle.moveTo(2, 4);
}
}

Output (Abstract Class):

Source Code (Interface):


package com.examples.experiments;

interface Shapes {

// Abstract method
void draw();
double area();
}

// Class 1
// Helper class
class Rectangles implements Shapes {

int length, width;

// constructor
Rectangles(int length, int width)
{
this.length = length;
this.width = width;
}

@Override public void draw()


{
System.out.println("Rectangle has been drawn ");
}

@Override public double area()


{
return (double)(length * width);
}
}

// Class 2
// Helper class
class Circles implements Shapes {

double pi = 3.14;
int radius;

// constructor
Circles(int radius) { this.radius = radius; }

@Override public void draw()


{
System.out.println("Circle has been drawn ");
}

@Override public double area()


{

return (double)((pi * radius * radius));


}
}

public class EXP6Interface {


public static void main(String[] args)
{
// Creating the Object of Rectangle class
// and using shape interface reference.
Shapes rect = new Rectangles(2, 3);

System.out.println("Area of rectangle: "


+ rect.area());

// Creating the Objects of circle class


Shapes circles = new Circles(2);

System.out.println("Area of circle: "


+ circles.area());
}
}

Output (Interface):

Results: Program Executed Successfully.


Program No. 7

Aim: Implementation of Programs to understand the concept of Overriding.

Source Code :
package com.examples.experiments;

class Employees {
public static int base = 10000;
int salary()
{
return base;
}
}

// Inherited class
class Manager extends Employees {
// This method overrides salary() of Parent
int salary()
{
return base + 20000;
}
}

// Inherited class
class Clerk extends Employees {
// This method overrides salary() of Parent
int salary()
{
return base + 10000;
}
}

public class EXP7 {


static void printSalary(Employees e)
{
System.out.println(e.salary());
}
public static void main(String[] args)
{
Employees obj1 = new Manager();

// We could also get type of employee using


// one more overridden method.loke getType()
System.out.print("Manager's salary : ");
printSalary(obj1);

Employees obj2 = new Clerk();


System.out.print("Clerk's salary : ");
printSalary(obj2);
}
}

Output:

Results: Program Executed Successfully.


Program No. 8

Aim: Implement Bank Transaction code for deposit and withdrawal but synchronization should
be implemented..

Source Code :
public class Bank {
int total = 100;
void withdrawn(String name, int withdrawal)
{
if(total >= withdrawal)
{
System.out.println(name + "'s Withdrawal Amount : " + withdrawal);
total = total - withdrawal;
System.out.println("Remaining Amount : " + total);

try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
System.out.println(name + " you cannot withdraw : " + withdrawal);
System.out.println("Remaining Balance : " + withdrawal);

try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void deposit(String name, int deposit)
{
System.out.println(name + " Deposited : " + deposit);
total = total + deposit;
System.out.println("Remaining Balance : " + total);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class RookieApproach {
public static void main(String[] args)
{
Bank obj = new Bank();

obj.withdrawn("Swapnesh", 70);
obj.deposit("Swapnesh", 90);
obj.withdrawn("Swapnesh", 30);
obj.deposit("Swapnesh", 70);
obj.withdrawn("Swapnesh", 40);
}
}

Output:

Results: Program Executed Successfully.


Program No. 9

Aim: Implement Bank Transaction code for deposit and withdrawal using the concept of
Multithreading.

Source Code :
package com.examples.multithreadingapproach;

public class Bank {


int total = 100;

void withdrawn(String name, int withdrawal)


{
if(total >= withdrawal)
{
System.out.println(name + "'s Withdrawal Amount : " + withdrawal);
total = total - withdrawal;
System.out.println("Remaining Balance : " + total);

try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
System.out.println(name + " you cannot withdraw : " + withdrawal);
System.out.println("Remaining Balance : " + withdrawal);

try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void deposit(String name, int deposit)
{
System.out.println(name + " Deposited : " + deposit);
total = total + deposit;
System.out.println("Remaining Balance : " + total);

try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public class ThreadDeposit extends Thread{

Bank object;
String name;
int dollar;

ThreadDeposit(Bank ob, String name, int money)


{
this.object = ob;
this.name = name;
this.dollar = money;
}

public void run()


{
object.deposit(name, dollar);
}
}

public class ThreadWithdrawal extends Thread {

Bank object;
String name;
int dollar;
ThreadWithdrawal(Bank ob, String name, int money)
{
this.object = ob;
this.name = name;
this.dollar = money;
}

public void run()


{
object.withdrawn(name, dollar);
}
}

public class MultithreadingApproach {


public static void main(String[] args)
{
Bank obj = new Bank();

ThreadWithdrawal t1 = new ThreadWithdrawal(obj, "Swapnesh", 20);


ThreadDeposit t2 = new ThreadDeposit(obj, "Swapnesh", 40);
ThreadWithdrawal t3 = new ThreadWithdrawal(obj, "Swapnesh", 35);
ThreadDeposit t4 = new ThreadDeposit(obj, "Swapnesh", 80);
ThreadWithdrawal t5 = new ThreadWithdrawal(obj, "Swapnesh", 40);

t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
Output:

Results: Program Executed Successfully.


Program No. 10

Aim: Write a Program to implement Hash Map.

Source Code :
package com.example.module1;

public class WrapperEX {


public static void main(String args[]){
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;

//Autoboxing: Converting primitives into objects


Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;

//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Unboxing: Converting Objects to Primitives
byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;

//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}
}

Output:
Source Code :
package com.example.hashmap;
import java.util.HashMap;
import java.util.Map;

public class HashMap2 {


public static void main(String args[])
{
HashMap<Integer, String> hm = new HashMap<Integer, String>();
System.out.println("Initial list of Elements : "+hm);
hm.put(100,"Swapnesh");
hm.put(101,"Rohit");
hm.put(102,"Vrushabh");
hm.put(103,"Vishal");

System.out.println(("After Invoking put() method : "));


for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey() + " " + m.getValue());
}

hm.putIfAbsent(104, "Aaditya");
System.out.println("After invoking putIfAbsent() method : ");
for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey() + " " + m.getValue());
}

HashMap<Integer, String> map = new HashMap<Integer, String>();

map.put(105, "Gautam");
map.putAll(hm);

System.out.println("After Invoking putAll() method : ");


for(Map.Entry m : map.entrySet())
{
System.out.println(m.getKey() + " " + m.getValue());
}
}
}
Output:

Source Code :
package com.example.hashmap;

import java.util.HashMap;

public class HashMap3 {


public static void main(String args[])
{
HashMap<Integer, String> hm = new HashMap<Integer, String>();

hm.put(100, "Swapnesh");
hm.put(101, "Rohit");
hm.put(102, "Vrushabh");
hm.put(103, "Vishal");
System.out.println("Initial list of Elements : " + hm);

hm.remove(102);
System.out.println("Update list of Elements : " + hm);

hm.remove(103);
System.out.println("Update list of Elements : " + hm);

hm.remove(101, "Rohit");
System.out.println("Update list of Elements : " + hm);
}
}

Output:

Source Code :
package com.example.hashmap;

import java.util.HashMap;

public class HashMap4 {


public static void main(String args[])
{
HashMap<Integer, String> hm = new HashMap<Integer, String>();

hm.put(100, "Swapnesh");
hm.put(101, "Rohit");
hm.put(102, "Vrushabh");
hm.put(103, "Vishal");

System.out.println("Initial list of Elements : " + hm);


hm.replace(102, "Koli");
System.out.println("Update list of Elements : " + hm);

hm.replace(103, "Vishal", "Adhikari");


System.out.println("Update list of Elements : " + hm);

hm.replaceAll((k,v) -> "Swapnesh");


System.out.println("Update list of Elements : " + hm);
}
}

Output:

Results: Program Executed Successfully.


Program No. 11

Aim: WAP for removing repeated characters in string.

Source Code :
package com.examples.removeduplicatestring;

import java.util.*;
import java.util.Arrays;

public class removeDuplicate1 {


static String removeDuplicate(char str[], int n)
{
int index = 0;

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


{
int j;
for(j = 0; j < i; j++)
{
if(str[i] == str[j])
{
break;
}
}

if(j == i)
{
str[index++] = str[i];
}
}
return String.valueOf(Arrays.copyOf(str, index));
}

public static void main(String[] args)


{
char str[] = "aapapalaea".toCharArray();
int n = str.length;
System.out.println("Original String : ");
System.out.println(str);
System.out.println("Result : " + removeDuplicate(str, n));
}
}

Output:

Results: Program Executed Successfully.


Program No. 12

Aim: WAP for counting frequency of characters in String

Source Code :
package com.examples.stringfrequency;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class StringFrequency2 {


public static void prCharWithFreq(String s)
{
Map<Character, Integer> d = new HashMap<Character, Integer>();

for (int i = 0; i<s.length();i++)


{
if (d.containsKey(s.charAt(i)))
{
d.put(s.charAt(i), d.get(s.charAt(i) + 1));
}
else
{
d.put(s.charAt(i), 1);
}
}

for (int i = 0; i<s.length(); i++)


{
if(d.get(s.charAt(i)) != 0)
{
System.out.println(s.charAt(i));
System.out.println(d.get(s.charAt(i)) + " ");
d.put(s.charAt(i), 0);
}
}
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String str= sc.nextLine();
prCharWithFreq(str);
}
}

Output:

Results: Program Executed Successfully.

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