BCA - Java Lab File
BCA - Java Lab File
Aim: Write a Java Program to perform the arithmetic operations using switch case
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
switch (operator) {
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;
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:
Source Code :
package com.examples.experiments;
// 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:
Source Code :
package com.examples.experiments;
import java.util.Scanner;
class Employee {
private int empid;
private String name;
private String empdept;
System.out.println(emp.toString());
}
}
}
Output:
Source Code :
package com.example.collectiondemo;
import java.util.Hashtable;
import java.util.Vector;
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:
// Declare fields
String objectName = " ";
// 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)
{
// 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));
}
}
rect.moveTo(1, 2);
System.out.println(" ");
circle.moveTo(2, 4);
}
}
interface Shapes {
// Abstract method
void draw();
double area();
}
// Class 1
// Helper class
class Rectangles implements Shapes {
// constructor
Rectangles(int length, int width)
{
this.length = length;
this.width = width;
}
// Class 2
// Helper class
class Circles implements Shapes {
double pi = 3.14;
int radius;
// constructor
Circles(int radius) { this.radius = radius; }
Output (Interface):
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;
}
}
Output:
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:
Aim: Implement Bank Transaction code for deposit and withdrawal using the concept of
Multithreading.
Source Code :
package com.examples.multithreadingapproach;
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();
}
}
}
Bank object;
String name;
int dollar;
Bank object;
String name;
int dollar;
ThreadWithdrawal(Bank ob, String name, int money)
{
this.object = ob;
this.name = name;
this.dollar = money;
}
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
Output:
Source Code :
package com.example.module1;
//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;
hm.putIfAbsent(104, "Aaditya");
System.out.println("After invoking putIfAbsent() method : ");
for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey() + " " + m.getValue());
}
map.put(105, "Gautam");
map.putAll(hm);
Source Code :
package com.example.hashmap;
import java.util.HashMap;
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;
hm.put(100, "Swapnesh");
hm.put(101, "Rohit");
hm.put(102, "Vrushabh");
hm.put(103, "Vishal");
Output:
Source Code :
package com.examples.removeduplicatestring;
import java.util.*;
import java.util.Arrays;
if(j == i)
{
str[index++] = str[i];
}
}
return String.valueOf(Arrays.copyOf(str, index));
}
Output:
Source Code :
package com.examples.stringfrequency;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
Output: