JAVA PROGRAMMING
Program. 1
Set up the Java Development Kit (JDK) and compile & execute a
basic Java program.
CODE:
class Main {
public static void main(String[] args) {
[Link]("Hello my name is gagandeep singh!");
}
}
OUTPUT:
Program.2
Write a Java program to demonstrate variables, data types, and
type casting.
CODE:
class Main {
public static void main(String[] args) {
int a = 10;
double b = a; // Implicit casting
double x = 9.8;
int y = (int)x; // Explicit casting
[Link]("a=" + a + " b=" + b + " y=" + y);
}
}
OUTPUT:
Program.3
Implement control flow structures (if-else, switch-case, loops) in
Java.
CODE:
class Main {
public static void main(String[] args) {
int n = 3;
if (n > 0) [Link]("Positive");
else [Link]("Negative");
switch (n) {
case 1: [Link]("One"); break;
case 2: [Link]("Two"); break;
default: [Link]("Other");
}
for (int i = 1; i <= 5; i++)
[Link](i + " ");
}
}
OUTPUT:
Program.4
Write a program to manipulate strings using String and
StringBuffer classes.
CODE:
class Main {
public static void main(String[] args) {
String s = "Java";
[Link]("Length: " + [Link]());
StringBuffer sb = new StringBuffer("Hello");
[Link](" Java");
[Link](sb);
}
}
OUTPUT:
Program.5
Develop an array-based Java program to perform basic operations
(insert, delete, search).
CODE:
class ArrayOps {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
int n = [Link];
[Link]("Search 3:");
for (int i = 0; i < n; i++)
if (arr[i] == 3) [Link]("Found at " + i);
[Link]("After Deleting 3:");
for (int i = 0; i < n; i++)
if (arr[i] != 3) [Link](arr[i] + " ");
}
}
OUTPUT:
Program.6
Implement a Java class with constructors and object instantiation.
CODE:
class Student {
String name;
int roll;
Student(String n, int r) {
name = n; roll = r;
}
void show() { [Link](name + " " + roll); }
public static void main(String[] args) {
Student s1 = new Student("Gagan", 101);
Student s2 = new Student("ramesh", 101);
[Link](); [Link]();
}
}
OUTPUT:
Program.7
Demonstrate the use of method overloading and recursion.
CODE:
class OverloadRecursion {
int sum(int a, int b) { return a + b; }
int sum(int a, int b, int c) { return a + b + c; }
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
OverloadRecursion or = new OverloadRecursion();
[Link]("sum(2,3) = " + [Link](2,3));
[Link]("sum(1,2,3) = " + [Link](1,2,3));
[Link]("5! = " + [Link](5));
}
}
OUTPUT:
Program.8
Create a simple calculator program using user-defined functions and
access modifiers.
Code:
import [Link];
class Calculator {
public int add(int a, int b) { return a + b; }
protected int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; } // package-private
private double divide(int a, int b) { return b==0 ? [Link] :
(double)a/b; }
public double operate(char op, int x, int y) {
switch(op) {
case '+': return add(x,y);
case '-': return subtract(x,y);
case '*': return multiply(x,y);
case '/': return divide(x,y);
default: throw new IllegalArgumentException("Invalid op");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Calculator c = new Calculator();
[Link]("Enter expression (e.g. 5 + 3): ");
int a = [Link](); char op = [Link]().charAt(0); int b = [Link]();
double res = [Link](op, a, b);
[Link]("Result = " + res);
[Link]();
}
}
Output:
Program.9
Implement single and multilevel inheritance using Java classes.
Code:
// Single inheritance
class Animal {
void eat() { [Link]("Animal eats"); }
}
class Dog extends Animal {
void bark() { [Link]("Dog barks"); }
}
// Multilevel inheritance: Animal -> Mammal -> Human
class Mammal extends Animal {
void walk() { [Link]("Mammal walks"); }
}
class Human extends Mammal {
void speak() { [Link]("Human speaks"); }
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); [Link]();
Human h = new Human();
[Link](); [Link](); [Link]();
}
}
OUTPUT:
Program.10
Implement polymorphism and method overriding in Java.
Code:
class Shape {
void draw() { [Link]("Drawing shape"); }
}
class Circle extends Shape {
void draw() { [Link]("Drawing circle"); }
}
class Rectangle extends Shape {
void draw() { [Link]("Drawing rectangle"); }
}
public class PolymorphismDemo {
static void render(Shape s) { [Link](); } // runtime polymorphism
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();
render(s1); // Drawing circle
render(s2); // Drawing rectangle
}
Output:
Program.11
Develop an application that demonstrates the working of interfaces.
Code:
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
public void start() { [Link]("Car started"); }
public void stop() { [Link]("Car stopped"); }
}
class Bike implements Vehicle {
public void start() { [Link]("Bike started"); }
public void stop() { [Link]("Bike stopped"); }
}
public class InterfaceDemo {
public static void main(String[] args) {
Vehicle v1 = new Car();
Vehicle v2 = new Bike();
[Link](); [Link]();
[Link](); [Link]();
}
}
OUTPUT:
Program.12
Write a program to handle exceptions using try-catch-
finally blocks.
Code:
import [Link];
public class ExceptionDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter numerator: ");
int a = [Link]();
[Link]("Enter denominator: ");
int b = [Link]();
[Link]("Result: " + (a / b));
}
catch (ArithmeticException ae) {
[Link]("Error: Division by zero!");
} catch (Exception e) {
[Link]("Invalid input.");
} finally {
[Link]("This runs always. Closing scanner.");
[Link]();
}
}
}
OUTPUT:
Program.13
Create and handle user-defined exceptions in Java.
Code:
// custom exception
class AgeTooYoungException extends Exception {
AgeTooYoungException(String msg) { super(msg); }
}
public class UserExceptionDemo {
static void checkAge(int age) throws AgeTooYoungException {
if (age < 18) throw new AgeTooYoungException("Age must be 18 or
above.");
[Link]("Access granted.");
}
public static void main(String[] args) {
try {
checkAge(16);
} catch (AgeTooYoungException e) {
[Link]("Caught: " + [Link]());
}
}
}
OUTPUT:
Program.14
Write a Java program to define and use custom packages.
Code:
package mypkg;
public class Utils {
public static String greet(String name) {
return "Hello, " + name + "!";
}
public static int square(int n) {
return n * n;
}
}
Second File:
import [Link];
public class MainApp {
public static void main(String[] args) {
[Link]([Link]("Gagan"));
[Link]("Square of 5 = " + [Link](5));
}
}
OUTPUT:
Program.15
Implement file handling in Java (read/write operations using
FileReader/FileWriter).
Code:
import [Link].*;
public class FileIOExample {
public static void main(String[] args) {
String fileName = "[Link]";
// Write data to file
try (FileWriter fw = new FileWriter(fileName)) {
[Link]("Hello, this is written to file.\n");
[Link]("Java File Handling Example.");
[Link]("Data written successfully.");
} catch (IOException e) {
[Link]("Write Error: " + [Link]());
}
// Read data from file
[Link]("\nReading from file:");
try (FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
} catch (IOException e) {
[Link]("❌Read Error: " + [Link]());
}
}
}
OUTPUT: