Name:- Nikita Dilip Patil
Rollno:- 67
Prn no.:- 24054491246505
Experiment No. 01
Title: Programs on Operators, Arithmetic Promotion, Method Calling
Theory:
Operators in Java are special symbols that perform specific operations on one, two, or three
operands and then return a result.
They are used to perform arithmetic, logical, relational, and bitwise operations.
Arithmetic promotion ensures smaller data types are converted to larger ones automatically
during operations.
Method calling in Java allows modularity and code reusability. It can be done by value or by
reference.
Program 1: Operators
class OperatorExample {
public static void main(String[] args) {
int a = 10, b = 5;
[Link]("Arithmetic Operators:");
[Link]("Addition: " + (a + b));
[Link]("Subtraction: " + (a - b));
[Link]("Multiplication: " + (a * b));
[Link]("Division: " + (a / b));
[Link]("Modulus: " + (a % b));
[Link]("\nRelational Operators:");
[Link]("a > b : " + (a > b));
[Link]("a == b : " + (a == b));
[Link]("\nLogical Operators:");
[Link]("(a > b) && (a != 0): " + ((a > b) && (a != 0)));
}
}
Program 2: Arithmetic Promotion
class ArithmeticPromotionExample {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
int c = a + b; // promoted to int
[Link]("Result (byte + byte → int): " + c);
int x = 10;
double y = 5.5;
double z = x + y; // int is promoted to double
[Link]("Result (int + double → double): " + z);
}
}
Experiment No. 02
Title: Programs on Classes: String and Math.
Theory:
The String class in Java is used to handle text data. Strings are immutable, meaning their values
cannot be changed after creation.
The Math class provides methods for performing basic numeric operations such as
exponentiation, logarithms, square roots, and trigonometric functions.
Program:
class StringMathExample {
public static void main(String[] args) {
String name = "Java Programming";
[Link]("Length: " + [Link]());
[Link]("Uppercase: " + [Link]());
double x = 16.0;
[Link]("Square Root: " + [Link](x));
[Link]("Power: " + [Link](2, 3));
[Link]("Random: " + [Link]());
}
}
Experiment No. 03
Title: Function overloading, Constructors, Default parameters, Returning by reference.
Theory:
Function overloading allows multiple methods with the same name but different parameters.
Constructors are special methods used to initialize objects.
Java supports default constructors, parameterized constructors, and copy constructors. Objects
can be passed and returned by reference.
Program:
class FunctionConceptExample {
int value;
FunctionConceptExample() {
value = 10;
}
FunctionConceptExample(int v) {
value = v;
}
void display() {
[Link]("Value: " + value);
}
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
public static void main(String[] args) {
FunctionConceptExample obj1 = new FunctionConceptExample();
FunctionConceptExample obj2 = new FunctionConceptExample(50);
[Link]();
[Link]();
[Link]("Sum (int): " + [Link](5, 10));
[Link]("Sum (double): " + [Link](5.5, 10.5));
}
}
Experiment No. 04
Title: Programs on dealing with Arrays.
Theory:
Arrays are used to store multiple values of the same type in a single variable. In Java, arrays are
objects that store elements in contiguous memory locations.
Program:
class ArrayExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : numbers) {
sum += num;
}
[Link]("Sum of array elements: " + sum);
}
}
Experiment No. 05
Title: Programs on Inheritance and Polymorphism.
Theory:
Inheritance allows one class to acquire properties of another. Polymorphism enables a single
function to behave differently based on the object that invokes it.
Program:
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Animal obj = new Dog();
[Link]();
}
}
Experiment No. 06
Title: Programs on Garbage collection, packaging, access Modifiers, static and abstract modifiers.
Theory:
Garbage collection automatically deallocates memory. Access modifiers define visibility (public,
private, protected).
Static variables/methods belong to the class rather than instances. Abstract classes contain
abstract methods that must be implemented by subclasses.
Program:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
}
public class ModifierExample {
static int count = 0;
public static void main(String[] args) {
Circle c = new Circle();
[Link]();
[Link]();
}
}
Experiment No. 07
Title: Programs on Interfaces, block initializers, final Modifier, static and dynamic binding.
Theory:
Interfaces define a contract for classes to implement. Static blocks initialize values before main().
Final variables cannot be changed.
Static binding happens at compile time, while dynamic binding occurs at runtime.
Program:
interface Vehicle {
void start();
}
class Car implements Vehicle {
final int speed = 100;
static {
[Link]("Static block executed");
}
public void start() {
[Link]("Car starts at speed " + speed);
}
}
public class InterfaceExample {
public static void main(String[] args) {
Vehicle v = new Car();
[Link]();
}
}
Experiment No. 08
Title: Programs on Exception Handling.
Theory:
Exception handling is used to manage runtime errors. Try, catch, throw, throws, and finally
blocks are used to handle exceptions and prevent abnormal termination.
Program:
class ExceptionExample {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
} finally {
[Link]("Finally block executed");
}
}
}
Experiment No. 09
Title: Creation and Accessing of Packages, Implementing Interfaces.
Theory:
Packages group related classes and interfaces together. 'import' is used to access packages.
Interfaces define methods that must be implemented by classes.
Program:
package mypackage;
public interface Greet {
void sayHello();
}
package test;
import [Link];
class Hello implements Greet {
public void sayHello() {
[Link]("Hello from package");
}
public static void main(String[] args) {
Hello h = new Hello();
[Link]();
}
}
Experiment No. 10
Title: Create Thread by Thread class and Runnable interface.
Theory:
Threads enable concurrent execution in Java. A thread can be created by extending the Thread
class or implementing the Runnable interface.
Program:
class MyThread extends Thread {
public void run() {
[Link]("Thread running using Thread class");
}
}
class RunnableExample implements Runnable {
public void run() {
[Link]("Thread running using Runnable interface");
}
}
public class ThreadExample {
public static void main(String[] args) {
new MyThread().start();
new Thread(new RunnableExample()).start();
}
}
Experiment No. 11
Title: Program to show the use of Synchronized keywords using threads.
Theory:
Synchronization ensures that only one thread accesses a resource at a time. The synchronized
keyword prevents data inconsistency in multithreading.
Program:
class SyncExample {
synchronized void printTable(int n) {
for (int i = 1; i <= 5; i++) {
[Link](n * i);
try { [Link](400); } catch (Exception e) {}
}
}
}
class Thread1 extends Thread {
SyncExample t;
Thread1(SyncExample t) { this.t = t; }
public void run() { [Link](5); }
}
class Thread2 extends Thread {
SyncExample t;
Thread2(SyncExample t) { this.t = t; }
public void run() { [Link](10); }
}
public class SynchronizedExample {
public static void main(String[] args) {
SyncExample obj = new SyncExample();
new Thread1(obj).start();
new Thread2(obj).start();
}
}