0% found this document useful (0 votes)
15 views14 pages

Java Practical With Question

The document is a practical file for a Java programming course at Bimla Devi Educational Society's Group of Institutions for the session 2025-2026. It includes a list of programming tasks, such as displaying 'Hello, World!', checking even or odd numbers, implementing recursion, and creating GUI applications. Each task is accompanied by example code demonstrating various Java concepts like data abstraction, encapsulation, inheritance, and exception handling.

Uploaded by

fdashlap
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Java Practical With Question

The document is a practical file for a Java programming course at Bimla Devi Educational Society's Group of Institutions for the session 2025-2026. It includes a list of programming tasks, such as displaying 'Hello, World!', checking even or odd numbers, implementing recursion, and creating GUI applications. Each task is accompanied by example code demonstrating various Java concepts like data abstraction, encapsulation, inheritance, and exception handling.

Uploaded by

fdashlap
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BIMLA DEVI EDUCATIONAL SOCIETY’S GROUPS OF

INSTITUTIONS,
JB KNOWLEDGE PARK, FARIDABAD

PRACTICAL FILE

JAVA PROGRAMMING
(25CSC403SE01)

Session : 2025-2026

SUBMITTED TO SUBMITTED BY
Rashmi Deepanshu
Professor 241122
CSE Department BCA 3rd Sem
No. Program Description Date Signature

Write a Java program to


1 display "Hello, World!" on the
screen.

Write a Java program to check


2 whether a given number is
even or odd.

Write a Java program to find


3 the factorial of a number using
recursion.

Write a Java program to print


4 the Fibonacci series up to N
terms.

Write a Java program to


5
demonstrate data abstraction.

Write a Java program to check


6 if a given string is a
palindrome.

Write a Java program to


7 demonstrate data
encapsulation.

Write a Java program to


demonstrate constructor and
8
deconstructor its type I.e.
default, parameterize.

Write a Java program to


9 implement a simple calculator
using switch case.

10 Write a Java program to count


the number of vowels,
No. Program Description Date Signature

consonants, digits, and white


spaces in a string.

Write a Java program to find


11 the largest and smallest
elements in an array.

Write a Java program to


12 demonstrate single and
multilevel inheritance.

Write a Java program to


demonstrate method
13
overloading and method
overriding.

Write a Java program to


14 implement an interface and
demonstrate polymorphism.

Write a Java program to create


15 an abstract class and
implement it in a subclass.

Write a Java program to


16 handle exceptions using try-
catch blocks.

Write a Java program to create


17 a custom exception and
handle it.

Write a Java program using


18
this and super keywords.

Write a Java program to create


a simple GUI application using
19
Swing (e.g., a basic calculator
or login form).
No. Program Description Date Signature

Write a Java program to


implement multithreading
20
using the Thread class and
Runnable interface.

1. Write a Java program to display "Hello, World!" on the screen.


public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
2. Write a Java program to check whether a given number is even or odd.

import [Link];

public class EvenOddCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");


int number = [Link]();

if (number % 2 == 0) {
[Link](number + " is even.");
} else {
[Link](number + " is odd.");
}

[Link]();
}
}
3. Write a Java program to find the factorial of a number using recursion.

import [Link];

public class FactorialRecursive {


public static void main(String[] args) {
int n = new Scanner([Link]).nextInt();
[Link](fact(n));
}

static long fact(int n) {


return (n <= 1) ? 1 : n * fact(n - 1);
}
}
4. Write a Java program to print the Fibonacci series up to N terms
import [Link];

public class FibonacciSeries {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of terms: ");
int n = [Link]();

int a = 0, b = 1;
for (int i = 1; i <= n; i++) {
[Link](a + " ");
int next = a + b;
a = b;
b = next;
}

[Link]();
}
}
5. Write a Java program to demonstrate data abstraction.
abstract class Shape {
abstract void area();
}

class Circle extends Shape {


double r;
Circle(double r){ this.r = r; }
void area(){ [Link](3.14 * r * r); }
}

public class AbstractionDemo {


public static void main(String[] args) {
Shape s = new Circle(5);
[Link]();
}

}
6. Write a Java program to check if a given string is a palindrome.
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
String s = new Scanner([Link]).nextLine();
String rev = new StringBuilder(s).reverse().toString();
[Link]([Link](rev) ? "Palindrome" : "Not Palindrome");
}
}
7. Write a Java program to demonstrate data encapsulation.
class Student {
private String name;
void setName(String n){ name = n; }
String getName(){ return name; }
}

public class EncapsulationDemo {


public static void main(String[] args) {
Student s = new Student();
[Link]("Devansh");
[Link]([Link]());
}
}
8. Write a Java program to demonstrate constructor and deconstructor its type
I.e. default, parameterize.

class Demo {
Demo() { [Link]("Default Constructor"); }
Demo(String msg) { [Link]("Parameterized Constructor: " + msg); }
protected void finalize() { [Link]("Object destroyed"); }
}

public class ConstructorDemo {


public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo("Hello");
d1 = null;
d2 = null;
[Link](); // Suggest garbage collection
}
}
9. Write a Java program to implement a simple calculator using switch case .
import [Link];

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter num1, num2: ");
double a = [Link](), b = [Link]();
[Link]("Enter operator (+,-,*,/): ");
char op = [Link]().charAt(0);

switch(op){
case '+': [Link](a+b); break;
case '-': [Link](a-b); break;
case '*': [Link](a*b); break;
case '/': [Link](b!=0 ? a/b : "Cannot divide by zero"); break;
default: [Link]("Invalid operator");
}
[Link]();
}
}
[Link] a Java program to count the number of vowels, consonants, digits,
and white spaces in a string.

import [Link];

public class CharCount {


public static void main(String[] args) {
String s = new Scanner([Link]).nextLine();
int v=0, c=0, d=0, ws=0;
for(char ch : [Link]().toCharArray()){
if("aeiou".indexOf(ch)>=0) v++;
else if(ch>='a' && ch<='z') c++;
else if(ch>='0' && ch<='9') d++;
else if(ch==' ') ws++;
}
[Link]("Vowels:"+v+" Consonants:"+c+" Digits:"+d+"
Spaces:"+ws);
}
}
11. Write a Java program to find the largest and smallest elements in an array.
import [Link];

public class MinMaxArray {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[n];
for(int i=0;i<n;i++) arr[i]=[Link]();

int min=arr[0], max=arr[0];


for(int i=1;i<n;i++){
if(arr[i]<min) min=arr[i];
if(arr[i]>max) max=arr[i];
}

[Link]("Min: "+min+" Max: "+max);


[Link]();
}
}

[Link] a Java program to demonstrate single and multilevel inheritance.


class A {
void msgA(){ [Link]("Class A"); }
}

class B extends A {
void msgB(){ [Link]("Class B"); }
}

class C extends B {
void msgC(){ [Link]("Class C"); }
}

public class InheritanceDemo {


public static void main(String[] args) {
C obj = new C();
[Link](); // from A
[Link](); // from B
[Link](); // from C
}
}
[Link] a Java program to demonstrate method overloading and method
overriding.
class Parent {
void show(){ [Link]("Parent show"); }
}

class Child extends Parent {


void show(){ [Link]("Child show"); } // overriding
void show(String msg){ [Link]("Overloaded: "+msg); } //
overloading
}

public class MethodDemo {


public static void main(String[] args) {
Child c = new Child();
[Link](); // overriding
[Link]("Hello"); // overloading
}
}
[Link] a Java program to implement an interface and demonstrate
polymorphism.
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound(){ [Link]("Dog barks"); }
}

class Cat implements Animal {


public void sound(){ [Link]("Cat meows"); }
}

public class InterfaceDemo {


public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
[Link]();
[Link]();
}
}
[Link] a Java program to create an abstract class and implement it in a
subclass.
abstract class Shape {
abstract void area();
}

class Circle extends Shape {


double r;
Circle(double r){ this.r=r; }
void area(){ [Link](3.14*r*r); }
}

public class AbstractDemo {


public static void main(String[] args) {
Shape s = new Circle(5);
[Link]();
}
}
[Link] a Java program to handle exceptions using try-catch blocks
import [Link];

public class ExceptionDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter num1 and num2: ");
int a = [Link](), b = [Link]();
try {
[Link]("Result: " + (a / b));
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
[Link]();
}
}
[Link] a Java program to create a custom exception and handle it.
class MyException extends Exception {
MyException(String msg){ super(msg); }
}
public class CustomExceptionDemo {
static void check(int n) throws MyException {
if(n<0) throw new MyException("Negative number not allowed");
else [Link]("Number is "+n);
}

public static void main(String[] args) {


try {
check(-5);
} catch(MyException e) {
[Link]([Link]());
}
}
}
[Link] a Java program using this and super keywords.
class Parent {
int x = 10;
void show(){ [Link]("Parent x="+x); }
}

class Child extends Parent {


int x = 20;
void show(){
[Link]("Child x="+x);
[Link]("Parent x via super="+super.x);
}
void display(){ [Link]("this.x="+this.x); }
}

public class ThisSuperDemo {


public static void main(String[] args) {
Child c = new Child();
[Link]();
[Link]();
}
}
[Link] a Java program to create a simple GUI application using Swing (e.g., a
basic calculator or login form).
import [Link].*;
import [Link].*;
public class LoginForm {
public static void main(String[] args) {
JFrame f = new JFrame("Login");
JLabel l1 = new JLabel("User:"), l2 = new JLabel("Pass:");
JTextField tf = new JTextField(); JPasswordField pf = new JPasswordField();
JButton b = new JButton("Login");

[Link](20,20,80,25); [Link](100,20,150,25);
[Link](20,60,80,25); [Link](100,60,150,25);
[Link](100,100,80,25);

[Link](e -> [Link](f,


"Username: "+[Link]()+"\nPassword: "+new String([Link]())
));

[Link](l1); [Link](tf); [Link](l2); [Link](pf); [Link](b);


[Link](300,200); [Link](null); [Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
[Link] a Java program to implement multithreading using the Thread class
and Runnable interface
class MyThread extends Thread {
public void run(){
for(int i=0;i<5;i++) [Link]("Thread: "+i);
}
}

class MyRunnable implements Runnable {


public void run(){
for(int i=0;i<5;i++) [Link]("Runnable: "+i);
}
}

public class MultithreadDemo {


public static void main(String[] args) {
new MyThread().start();
new Thread(new MyRunnable()).start();
}
}

You might also like