Java Lab
Java Lab
Java Lab
Experiment Name: Write a program to display default value of all primi ve data types of java.
CODE :
OUTPUT :
Default byte: 0
Default short: 0
Default int: 0
Default long: 0
Default char:
Experiment Name: Write a program to input and display all primi ve data types of java.
CODE:
import java.util.Scanner;
public class print_2 {
static byte b;
static short s;
static int i ;
static long l;
static float f ;
static double d;
static char c;
static boolean tf ;
public static void display(){
System.out.println("byte: "+b);
System.out.println("short: "+s);
System.out.println("int: "+i);
System.out.println("long: "+l);
System.out.println("float: "+f);
System.out.println("double: "+d);
System.out.println("char : "+c);
System.out.println("boolean: "+tf);
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter byte data: ");
b = input.nextByte();
System.out.println("enter short data: ");
s = input.nextShort();
System.out.println("enter the int value: ");
i = input.nextInt();
System.out.println("enter the long value: ");
l = input.nextLong();
System.out.println("enter the float value: ");
f = input.nextFloat();
System.out.println("enter the double value: ");
d = input.nextDouble();
System.out.println("enter char type value: ");
c = input.next().charAt(0);
System.out.println("enter the boolean type value: ");
tf = input.nextBoolean();
input.close();
display();
}
}
OUTPUT :
byte: -128
short: -32768
int: 2341645
long: 123456789
float: 123.34
double: 12345.23456
char : a
boolean: true
Experiment No: 03
CODE :
public class OperatorDemo {
public static void main(String[] args) {
// Increment and decrement operators
int a = 5;
System.out.println("Initial value of a: " + a);
a++; // Increment a by 1
System.out.println("After incrementing a: " + a);
a--; // Decrement a by 1
System.out.println("After decrementing a: " + a);
// Arithmetic operators
int x = 10;
int y = 3;
System.out.println("x + y = " + (x + y)); // Addition
System.out.println("x - y = " + (x - y)); // Subtraction
System.out.println("x * y = " + (x * y)); // Multiplication
System.out.println("x / y = " + (x / y)); // Division
System.out.println("x % y = " + (x % y)); // Modulus
// Relational operators
int num1 = 10;
int num2 = 20;
System.out.println("num1 == num2: " + (num1 == num2)); // Equal to
System.out.println("num1 != num2: " + (num1 != num2)); // Not equal to
System.out.println("num1 > num2: " + (num1 > num2)); // Greater than
System.out.println("num1 < num2: " + (num1 < num2)); // Less than
System.out.println("num1 >= num2: " + (num1 >= num2)); // Greater than or
equal to
System.out.println("num1 <= num2: " + (num1 <= num2)); // Less than or
equal to
// Bitwise operators
int m = 5; // Binary: 101
int n = 3; // Binary: 011
System.out.println("m & n = " + (m & n)); // Bitwise AND
System.out.println("m | n = " + (m | n)); // Bitwise OR
System.out.println("m ^ n = " + (m ^ n)); // Bitwise XOR
System.out.println("~m = " + (~m)); // Bitwise Complement
System.out.println("n << 1 = " + (n << 1)); // Left shift
System.out.println("n >> 1 = " + (n >> 1)); // Right shift
x%y=1
m&n=1
m|n=7
m^n=6
~m = -6
n << 1 = 6
n >> 1 = 1
Result: 10
Experiment No: 04
CODE :
public class controlFlow {
public static void main(String[] args) {
int a = 10 ;
int b = 15;
//if-else if-else statement
if(a == 10){
System.out.println("value of a is 10");
}else if(a < b){
System.out.println("a is less then b");
}else{
System.out.println("a is gratter then b");
}
// switch statement
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Well done!");
break;
default:
System.out.println("Invalid grade.");
//for loop
int i;
System.out.print(i+" ");
//while loop
while(i < 5) {
//do-while loop
System.out.println("using do-while");
do {
}
OUTPUT :
value of a is 10
Well done!
01234
using do-while
Experiment No: 05
CODE :
import java.util.Scanner;
public class Arraytpe {
public static void displayOneDarray(int[] arr,int size){
System.out.println("OUTPUT FOR 1D: ");
for(int i = 0 ; i < size ; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void displayTDarray(int [][]arr , int row,int col){
System.out.println("OUTPUT FOR 2D: ");
for(int i = 0 ; i < row ; i++){
for(int j = 0 ; j < col ; j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size , row , colum;
System.out.print("enter the size of OD arry: ");
size = input.nextInt();
int[] OD = new int[size];
System.out.print("enter array values: ");
for(int i =0 ; i < size ; i++){
OD[i] = input.nextInt();
}
System.out.print("enter the row and colum of an arry (using space): ");
row = input.nextInt();
colum = input.nextInt();
int[][] TD = new int[row][colum];
for(int i = 0 ; i < row ; i++){
System.out.println("enter "+colum+" ammount value (using space): ");
for(int j = 0 ; j < colum ; j++){
TD[i][j] = input.nextInt();
}
}
input.close();
displayOneDarray(OD , size);
displayTDarray(TD , row ,colum);
}
}
OUTPUT:
12
34
56
12345
Experiment No: 06
Experiment Name: Write a java program to demonstrate the concept of encapsula on.
CODE :
OUTPUT :
Age is: 21
Experiment No: 08
a. Single inheritance
b. Multilevel inheritance
c. Hierarchical inheritance
d. Multiple inheritance
CODE :
void setAnimalName(String n) {
name = n;
}
void printName() {
System.out.println("the name is: " + name);
}
}
// Single inheritance
public static class Dog extends Animal {
public void bark() {
System.out.println("the dog is barking");
}
}
// Multi-level inheritance
public static class BabyDog extends Dog {
public void weep() {
System.out.println("the baby dog is weeping");
}
}
// Hierarchical Inheritance
public static class Cat extends Animal {
public void meow() {
System.out.println("the cat is meowing");
}
}
// Multiple Inheritance
interface Cow {
public void eatGrass();
}
interface Goat {
public void eatGrass();
}
dog.bark();
BabyDog bd = new BabyDog();
bd.setAnimalName("Baby Dggy");
bd.printName();
bd.bark();
bd.weep();
Cat c = new Cat();
c.setAnimalName("tommy");
c.printName();
c.meow();
OUTPUT :
Experiment Name: Write a program to demonstrate the concept of private, public and protected access specifier.
CODE :
OUTPUT :
Experiment Name: Write a java program to demonstrate the concept of overloading (Constructor and Method)
CODE :
void display(){
System.out.println("default method");
}
}
}
OUTPUT :
default constructor
default method
CODE :
OUTPUT :
Now it is overrided.
Experiment No : 12
CODE :
public class cpy_constructor {
public String name;
public int age;
cpy_constructor(cpy_constructor anotherConstructor){
System.out.println("using copy constructor");
name = anotherConstructor.name;
age = anotherConstructor.age;
}
public static void main(String[] args)
{
cpy_constructor obj = new cpy_constructor("David",22);
cpy_constructor obj2 = new cpy_constructor(obj);
}
}
OUTPUT :
Experiment No: 13
Experiment Name: Write a java program to demonstrate abstract class and interface.
CODE :
interface swimmer{
void swim();
}
public class Dog extends Animal implements swimmer{
public void Sound(){
System.out.println("Dog barks");
}
public void swim(){
System.out.println("the animal is swimming");
}
}
public static void main(String[] args){
Dog d = new Dog();
}
}
public class Abstract_interface extends Dog {
public static void main(String[] args) {
Dog d = new Dog();
d.Sound();
d.swim();
}
}
OUTPUT :
Dog barks
Experiment No : 14
CODE :
OUTPUT :
1. Experiment Name: Write a java program to demonstrate the concept of try, catch, finally, throw and
throws keyword.
CODE :
import java.util.Scanner;
try {
validateNumber(number);
System.out.println("Entered number is valid!");
} catch (InvalidNumberException e) {
System.out.println("Caught InvalidNumberException: " +
e.getMessage());
} finally {
System.out.println("Finally block executed.");
scanner.close();
}
}
OUTPUT :
Enter a number:
0
Caught InvalidNumberExcep on: Number must be greater than zero.
Finally block executed.
Experiment No : 16
Experiment Name: Write a java program to handle Arithme cExcep on, ArrayIndexOutOfBoundsExcep on,
NullPointerExcep on, StringIndexOutOfBoundExcep ons
CODE :
// ArrayIndexOutOfBoundsException
try {
accessArrayOutOfBounds();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " +
e.getMessage());
}
// NullPointerException
try {
accessNullReference();
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " +
e.getMessage());
}
// StringIndexOutOfBoundsException
try {
accessStringOutOfBounds();
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Caught StringIndexOutOfBoundsException: " +
e.getMessage());
}
}
// Method to demonstrate ArithmeticException
public static int divideByZero() {
return 10 / 0;
}
OUTPUT :
CODE :
import java.util.ArrayList;
import java.util.List;
OUTPUT :
Integer List:
10
20
30
String List:
Hello
World
Experiment No : 18
Experiment Name: Define a Student class with a ributes such as name, roll number, address and contact no.
Implement methods to set all the informa on and another method to display details.
CODE :
import java.util.Scanner;
public class Student {
String name;
String address;
int roll;
long contactNo;
OUTPUT :
Name : David
Roll : 42
Address : Bangladesh
Contact Number : 1335456234
Experiment No : 19
Experiment Name : Define an Employee class with a ributes such as name, ID, and salary. Develop methods to
calculate annual salary and display employee details.
CODE :
import java.util.Scanner;
public class Employee {
int ID;
int salary;
String name;
OUTPUT :
NAME : lana
ID : 3
Annual SALARY : 120000
Experiment No : 20
Experiment Name: Design classes for different geometric shapes like Circle, Rectangle, and Triangle.
Implementmethods to calculate area and perimeter for each shape (Apply method overloading
concept).
CODE :
import java.util.Scanner;
public class geometricShapes {
double area;
double parameter;
public void calculation() {
System.out.println("Area : " + area + "\n" + "Parameter : " +
parameter);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
float radius;
float length;
float width;
float a;
float b;
float c;
byte option;
s
do {
System.out.print("Which Operation do you want to Run? \n 1.Circle
\n 2.Rectangle \n 3.Triangle \n 4.Exit \n Enter : ");
option = scanner.nextByte();
switch (option) {
case 1 -> {
System.out.print("Enter Radius : ");
radius = scanner.nextFloat();
Circle circle = new Circle();
circle.calculation(radius);
break;
}
case 2 -> {
System.out.print("Enter Length : ");
length = scanner.nextFloat();
System.out.print("Enter width : ");
width = scanner.nextFloat();
Rectangle rec = new Rectangle();
rec.calculation(length, width);
break;
}
case 3 -> {
System.out.print("Enter the value of a : ");
a = scanner.nextFloat();
System.out.print("Enter the value of b : ");
b = scanner.nextFloat();
System.out.print("Enter the value of c : ");
c = scanner.nextFloat();
Triangle triangle = new Triangle();
triangle.calculation(a,b,c);
break;
}
case 4 -> {
break;
}
default -> {
System.out.println("Enter correct value");
}
}
Experiment Name : Create a BankAccount class with a ributes like account number, balance, and owner name.
CODE :
import java.util.Scanner;
public class BankAccount {
private long accountNumber ;
private int balance;
private String ownerName;
public BankAccount(long a , int b , String n){
accountNumber = a;
balance = b;
ownerName = n;
}
public void deposit(int ammount){
balance += ammount;
System.out.println("deposit successfull "+ammount+"$" );
}
public void withdraw(int ammount){
if(ammount > balance){
System.out.println("sorry, you can't withdraw");
return;
}
balance -= ammount;
System.out.println("withdraw successfull");
}
void balance(){
System.out.println("the balance is: "+balance);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("enter your account number and your name : " );
BankAccount bank = new BankAccount(input.nextLong(),
2000,input.nextLine());
bank.balance();
System.out.print("enter the ammount you want to deposit: ");
bank.deposit(input.nextInt());
System.out.println("enter the ammount you want to withdraw: ");
bank.withdraw(input.nextInt());
bank.balance();
}
}
OUTPUT:
withdraw successfull
Experiment No : 22
Experiment Name : Create a Java class called BankAccount with proper es accountNumber (String),
accountHolderName (String), and balance (double). Implement a parameterized constructor that ini alizes these
proper es. Write a method to deposit money into the account and another method to withdraw money from
the account and a method to display the final balance.
CODE :
package lab3;
import java.util.Scanner;
public class BankAccount {
int accountNumber;
double balance;
String accountHolderName;
int withdraw = 0;
int deposit = 0;
public BankAccount(){
this.accountNumber = 0;
this.accountHolderName = "Unknown";
this.balance = 0.0;
}
public void deposit() {
balance += deposit;
}
public void withdraw() {
if(balance == 0){
System.out.println("Your account balance is Zero!!! \nWithdarwl is not
possible.");
}
else if(balance < withdraw){
System.out.println("Account balance is " + balance + ".\nwithdrawl is not
possible.");
}
else{
balance -= withdraw;
}
}
int option;
do {
System.out.print("Which service do you want? \n 1.Deposit \n 2.withdraw
\n 3.Display \n 4.Exit \n Enter : ");
option = scanner.nextInt();
switch (option) {
case 1 -> {
System.out.print("Enter ammount to deposit : ");
bank.deposit = scanner.nextInt();
bank.deposit();
break;
}
case 2 -> {
System.out.print("Enter ammount to withdraw : ");
bank.withdraw = scanner.nextInt();
bank.withdraw();
break;
}
case 3 -> {
bank.display();
break;
}
case 4 -> {
break;
}
default -> {
System.out.println("please!Enter correct value");
}
}
Experiment Name: Create a Java class called Employee with proper es id (int) and name (String) and a method
employeeDetails(). Implement a parameterized constructor that ini alizes these proper es. Create a subclass
Manager and a subclass Worker. Implement constructors in each subclass that call the superclass constructor to
ini alize the id and name. Demonstrate inheritance by crea ng instances of Manager and Worker and displaying
their details.
CODE :
package lab3;
import java.util.Scanner;
public class Main {
int id;
String name;
public void employeeinfo() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter ID : ");
id = scanner.nextInt();
System.out.print("Enter Name : ");
name = scanner.next();
}
public static void main(String args[]) {
Main main = new Main();
Scanner scanner = new Scanner(System.in);
byte op;
do {
System.out.println("Detail of \n 1.Manager \n 2.Worker \n 3.Exit \n
Enter : ");
op = scanner.nextByte();
switch (op) {
case 1 -> {
main.employeeinfo();
Manager manager = new Manager(main.id,main.name);
manager.employeeDetails();
}
case 2 -> {
main.employeeinfo();
Worker worker = new Worker(main.id, main.name);
worker.employeeDetails();
}
case 3 -> {
break;
}
default -> {
System.out.println("Enter correct value!");
}
}
} while (op != 3);
}
}
OUTPUT :
Detail of
1.Manager
2.Worker
3.Exit
Enter :
Enter ID : 2354
Empoyee's Id : 2354
Detail of
1.Manager
2.Worker
3.Exit
Enter :
Enter ID : 4455
Empoyee's Id : 4455
Detail of
1.Manager
2.Worker
3.Exit
Enter :
3
Experiment No : 24
Experiment Name : Create a Java class called Animal with proper es species (String). Implement a parameterized
constructor that ini alizes the species. Create subclasses Mammal, Bird, and Fish, each represen ng a specific
type of animal. Implement constructors in each subclass that call the superclass constructor to ini alize the
species. Demonstrate inheritance by crea ng instances of Mammal, Bird, and Fish and displaying their
proper es.
CODE :
package lab3;
public class MainAnimal {
public static void main(String args[]) {
Mammal mammal = new Mammal("Elephent");
Bird bird = new Bird("peacock");
Fish fish = new Fish("Dolphin");
}
}
public class Animal {
String species;
public Animal(String species){
this.species = species;
}
}
public class Bird extends Animal{
public Bird(String species){
super(species);
System.out.println("Bird : " + super.species);
}
}
public class Fish extends Animal{
public Fish(String species){
super(species);
System.out.println("Fish : " + super.species);
}
}
public class Mammal extends Animal{
public Mammal(String species){
super(species);
System.out.println("Mammal : " + super.species);
}
}
OUTPUT :
Mammal : Elephent
Bird : peacock
Fish : Dolphin
Experiment No : 25
Experimaent Name : Create a Java class called Account with proper es accountNumber (String) and balance
(double). Implement a parameterized constructor that ini alizes these proper es. Create subclasses
SavingsAccount and CheckingAccount, each represen ng a specific type of bank account. Implement
constructors in each subclass that call the superclass constructor to ini alize the account number and balance.
Demonstrate inheritance by crea ng instances of SavingsAccount and CheckingAccount and displaying their
proper es (i.e account number and balance).
CODE :
import java.util.Scanner;
public class Employee {
int id;
String name;
String department;
double salary;
public Employee(int id , String name, String department,double salary){
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
public Employee(){
this.id = 0;
this.name = "Unknown";
this.department = "Unknown";
this.salary = 0.0;
}
System.out.print("Enter ID : ");
emp1.id = scanner.nextInt();
System.out.print("Enter NAME : ");
emp1.name = scanner.next();
System.out.print("Enter DEPARTMENT : ");
emp1.department = scanner.next();
System.out.print("Enter SALARY : ");
emp1.salary = scanner.nextDouble();
NAME : Unknown
DEPARTMENT : Unknown
SALARY : 0.0
Enter ID : 50
ID : 50
NAME : MDY
DEPARTMENT : CSE
SALARY : 200000.0