Table of Contents
Sr No Programs Signature
1 Write a program to demonstrate variables, data
types and typecasting. Read the variable using
Scanner class and perform typecasting.
2 Write a program to implement switch case in
Java.
3 Write a program to perform linear search and
bubble sort for a given array.
4 Write a program to perform insertion and
deletion of elements in an array.
5 Write a program to perform method
overloading.
6 Create a class, assign values to the variables of
the class using constructor. Also perform
constructor overloading.
7 Write a program to perform passing object as a
parameter of a function and returning object as
a return type.
8 Write a program to demonstrate multi-level
inheritance in Java.
9 Write a program to implement method
overriding in Java.
10 Write a program to demonstrate the use of
super keyword.
11 Write a program to implement the concept of
abstract classes and abstract methods.
12 Develop a program to demonstrate the working
of interface.
13 Write a program to create and handle
Sr No Programs Signature
exceptions using try-catch block.
14 Write a program to define and use Java
packages.
15 Write a program to create strings using
StringBuffer.
Program 1: Variables, Data Types & Typecasting
Question:
Write a program to demonstrate variables, data types and typecasting. Read the variable using scanner
class and perform typecasting.
Code:
import [Link];
public class Program1 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
int num = [Link]();
[Link]("Enter a double value: ");
double dbl = [Link]();
// Typecasting
int castedValue = (int) dbl;
[Link]("Integer value: " + num);
[Link]("Double value: " + dbl);
[Link]("After typecasting double to int: " + castedValue);
[Link]();
}
}
Output:
Enter an integer: 10
Enter a double value: 15.6
Integer value: 10
Double value: 15.6
After typecasting double to int: 15
Page 2
Program 2: Switch Case
Question:
Write a program to implement switch case in java.
Code:
import [Link];
public class Program2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a day number (1-7): ");
int day = [Link]();
switch (day) {
case 1 -> [Link]("Monday");
case 2 -> [Link]("Tuesday");
case 3 -> [Link]("Wednesday");
case 4 -> [Link]("Thursday");
case 5 -> [Link]("Friday");
case 6 -> [Link]("Saturday");
case 7 -> [Link]("Sunday");
default -> [Link]("Invalid day!");
}
[Link]();
}
}
Output:
Enter a day number (1-7): 4
Thursday
Page 3
Program 3: Linear Search & Bubble Sort
Question:
Write a program to perform linear search and bubble sort for a given array.
Code:
import [Link];
public class Program3 {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 7};
Scanner sc = new Scanner([Link]);
// Linear Search
[Link]("Enter element to search: ");
int key = [Link]();
boolean found = false;
for (int i = 0; i < [Link]; i++) {
if (arr[i] == key) {
[Link]("Element found at position: " + (i+1));
found = true;
break;
}
}
if (!found) [Link]("Element not found.");
// Bubble Sort
for (int i = 0; i < [Link]-1; i++)
for (int j = 0; j < [Link]-i-1; j++)
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
[Link]("Sorted Array: ");
for (int n: arr) [Link](n + " ");
[Link]();
}
}
Output:
Enter element to search: 9
Element found at position: 3
Sorted Array: 1 2 5 7 9
Page 4
Program 4: Insertion and Deletion in Array
Question:
Write a program to perform insertion and deletion of elements in an array.
Code:
import [Link];
public class Program4 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] arr = new int[10];
int n = 5;
// Initialize array
for (int i = 0; i < n; i++) arr[i] = i + 1;
[Link]("Array: ");
for (int i = 0; i < n; i++) [Link](arr[i] + " ");
[Link]();
// Insertion
[Link]("Enter position to insert : “)
int pos = [Link]();
[Link]("Enter element: ");
int ele = [Link]();
for (int i = n; i >= pos; i--) arr[i] = arr[i-1];
arr[pos-1] = ele;
n++;
[Link](>"After Insertion: ");
for (int i = 0; i < n; i++) [Link](arr[i] + " ");
[Link]();
// Deletion
[Link]("Enter position to delete: ")
pos = [Link]();
for (int i = pos-1; i < n-1; i++) arr[i] = arr[i+1];
n--;
[Link]("After Deletion: ");
for (int i = 0; i < n; i++) [Link](arr[i] + " ");
[Link]();
}
}
Output:
Array: 1 2 3 4 5
Enter position to insert (1-6): 3
Enter element: 10
After Insertion: 1 2 10 3 4 5
Program 5: Method Overloading
Question:
Write a program to perform method overloading.
Code:
public class Program5 {
void show(int a) {
[Link]("Integer: " + a);
}
void show(String s) {
[Link]("String: " + s);
}
void show(double d) {
[Link](class="string">"Double: " + d);
}
public static void main(String[] args) {
Program5 obj = new Program5();
[Link](10);
[Link]("Java");
[Link](12.5);
}
}
Output:
Integer: 10
String: Java
Double: 12.5
Page 6
Program 6: Constructor and Constructor Overloading
Question:
Create a class, assign values to the variables of the class using constructor. Also perform constructor
overloading.
Code:
public class Program6 {
int id;
String name;
// Default constructor
Program6() {
id = 0;
name = "Unknown";
}
// Parameterized constructor
Program6(int i, String n) {
id = i;
name = n;
}
void display() {
[Link]("ID: " + id + ", Name: " + name);
}
public static void main(String[] args) {
Program6 s1 = new Program6();
Program6 s2 = new Program6(1, "Surya");
[Link]();
[Link]();
}
}
Output:
ID: 0, Name: Unknown
ID: 1, Name: Surya
Page 7
Program 7: Passing and Returning Objects
Question:
Write a program to perform passing object as a parameter of a function and returning object as a return
type.
Code:
class Data {
int value;
Data(int v) {
value = v;
}
Data add(Data d2) {
return new Data([Link] + [Link]);
}
}
public class Program7 {
public static void main(String[] args) {
Data d1 = new Data(10);
Data d2 = new Data(20);
Data result = [Link](d2);
[Link]("Sum of values: " + [Link]);
}
}
Output:
Sum of values: 30
Page 8
Program 8: Multilevel Inheritance
Question:
Write a program to demonstrate multi-level inheritance in java.
Code:
class A {
void showA() {
[Link]("Class A");
}
}
class B extends A {
void showB() {
[Link]("Class B");
}
}
class C extends B {
void showC() {
[Link]("Class C");
}
}
public class Program8 {
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}
Output:
Class A
Class B
Class C
Page 9
Program 9: Method Overriding
Question:
Write a program to implement method overriding in java.
Code:
class Animal {
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class Program9 {
public static void main(String[] args) {
Animal obj = new Dog();
[Link]();
}
}
Output:
Dog barks
Page 10
Program 10: Use of super Keyword
Question:
Write a program to demonstrate the use of super keyword.
Code:
class Parent {
String name = "Parent";
void show() {
[Link]("This is parent class");
}
}
class Child extends Parent {
String name = "Child";
void show() {
[Link]("This is child class");
[Link]("Parent name: " + [Link]);
[Link]();
}
}
public class Program10 {
public static void main(String[] args) {
Child c = new Child();
[Link]();
}
}
Output:
This is child class
Parent name: Parent
This is parent class
Page 11
Program 11: Abstract Class and Abstract Methods
Question:
Write a program to implement the concept of abstract classes and abstract methods.
Code:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing a Circle");
}
}
public class Program11 {
public static void main(String[] args) {
Shape s = new Circle();
[Link]();
}
}
Output:
Drawing a Circle
Page 12
Program 12: Working of Interface
Question:
Develop a program to demonstrate the working of interface.
Code:
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
[Link]("Car starts with key");
}
}
public class Program12 {
public static void main(String[] args) {
Vehicle v = new Car();
[Link]();
}
}
Output:
Car starts with key
Page 13
Program 13: Exception Handling using try-catch
Question:
Write a program to create and handle exceptions using try-catch block.
Code:
public class Program13 {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b;
[Link]("Result: " + c);
} catch(ArithmeticException e) {
[Link]("Error: Division by zero!");
} finally {
[Link]("Program ended.");
}
}
}
Output:
Error: Division by zero!
Program ended.
Page 14
Program 14: Java Packages
Question:
Write a program to define and use java packages.
Code:
[Link] (in package mypackage)
package mypackage;
public class Message {
public void display() {
[Link]("Hello from Package!");
}
}
[Link]
import [Link];
public class Program14 {
public static void main(String[] args) {
Message msg = new Message();
[Link]();
}
}
Output:
Hello from Package!
Page 15
Program 15: StringBuffer Operations
Question:
Write a program to create strings using string buffer and perform following operation on the string: count
the length of string, use insert and replace methods, delete and reverse method, append a string into
existing string, convert the string into upper and lower case.
Code:
public class Program {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java Programming");
[Link]("Length: " + [Link]());
[Link](5, "is ");
[Link]("After Insert: " + sb);
[Link](5, 7, "was ");
[Link]("After Replace: " + sb);
[Link](0, 4);
[Link]("After Delete: " + sb);
[Link]();
[Link]("Reversed: " + sb);
[Link]();
[Link](" Language");
[Link]("After Append: " + sb);
String str = [Link]();
[Link]("Uppercase: " + [Link]());
[Link]("Lowercase: " + [Link]());
}
}
Output:
Length: 16
After Insert: Javais Programming
After Replace: Javawas Programming
After Delete: was Programming
Reversed: gnimmargorP saw
After Append: was Programming Language
Uppercase: WAS PROGRAMMING LANGUAGE
Lowercase: was programming language
Page 16