Memon MD Uvesh Abdul Gani Roll:-270
Q1. Write a Java program to print “Hello World”.
Ans:-
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World");
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-1
Memon MD Uvesh Abdul Gani Roll:-270
Q-2Write a Java program to calculate the simple interest for the given data.
Ans:-
class SimpleInterest {
public static void main(String[] args) {
double principal = 1000; // Principal amount
double rate = 5; // Rate of interest
double time = 2; // Time in years
double simpleInterest = (principal * rate * time) / 100;
[Link]("Simple Interest = " + simpleInterest);
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-2
Memon MD Uvesh Abdul Gani Roll:-270
Q-3Write a Java program to print the Fibonacci series up to a given number.
Ans:
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci series up to " + n + ": ");
while (a <= n) {
[Link](a + " ");
int next = a + b;
a = b;
b = next;
}
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-3
Memon MD Uvesh Abdul Gani Roll:-270
Q4-Write a Java program to accept three numbers and find the largest among them.
Ans:-
import [Link];
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Enter third number: ");
int c = [Link]();
int largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
[Link]("The largest number is: " + largest);
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-4
Memon MD Uvesh Abdul Gani Roll:-270
Q-5Write a Java program to accept a number and check whether it is a prime
number.
Ans:-
import [Link];
public class PrimeNumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
}
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-5
Memon MD Uvesh Abdul Gani Roll:-270
Q-6Write a Java program to demonstrate constructor-based initialization of objects
using the this keyword to clearly distinguish between instance variables and
constructor parameters having the same names.
Ans:-
class Student {
// Instance variables
int id;
String name;
// Constructor with parameters having same names as instance variables
Student(int id, String name) {
[Link] = id; // refers to instance variable
[Link] = name; // refers to instance variable
}
// Method to display values
void display() {
[Link]("Student ID: " + id);
[Link]("Student Name: " + name);
}
}
public class ThisKeywordDemo {
public static void main(String[] args) {
// Creating objects and initializing them using constructor
Student s1 = new Student(101, "Alice");
Student s2 = new Student(102, "Bob");
[Link]();
[Link]();
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-6
Memon MD Uvesh Abdul Gani Roll:-270
Q-7Write a Java program to demonstrate constructor overloading by defining
multiple constructors within the same class with different parameter lists and
invoking them to initialize objects in different ways.
Ans:-
class Employee {
int id;
String name;
double salary;
// Constructor with no parameters
Employee() {
id = 0;
name = "Not Assigned";
salary = 0.0;
}
// Constructor with two parameters
Employee(int id, String name) {
[Link] = id;
[Link] = name;
[Link] = 0.0;
}
// Constructor with three parameters
Employee(int id, String name, double salary) {
[Link] = id;
[Link] = name;
[Link] = salary;
}
// Method to display employee details
void display() {
[Link]("ID: " + id);
[Link]("Name: " + name);
[Link]("Salary: " + salary);
[Link]();
}
}
public class ConstructorOverloadingDemo {
public static void main(String[] args) {
// Using different constructors
Employee e1 = new Employee();
Employee e2 = new Employee(101, "Alice");
Employee e3 = new Employee(102, "Bob", 50000);
[Link]();
[Link]();
Vimal Tormal Poddar BCA College Page no:-7
Memon MD Uvesh Abdul Gani Roll:-270
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-8
Memon MD Uvesh Abdul Gani Roll:-270
Q8Write a Java program to invoke constructors using the super keyword.
Ans:-
// Parent class
class Animal {
String name;
// Parent class constructor
Animal(String name) {
[Link] = name;
[Link]("Animal constructor called");
}
}
// Child class
class Dog extends Animal {
String breed;
// Child class constructor
Dog(String name, String breed) {
super(name); // Invokes parent class constructor
[Link] = breed;
[Link]("Dog constructor called");
}
// Method to display details
void display() {
[Link]("Name: " + name);
[Link]("Breed: " + breed);
}
}
public class SuperKeywordConstructorDemo {
public static void main(String[] args) {
Dog d = new Dog("Buddy", "Golden Retriever");
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-9
Memon MD Uvesh Abdul Gani Roll:-270
Q9-Write a Java program to implement multilevel inheritance.
Ans:-
// Base class
class Animal {
void eat() {
[Link]("Animal is eating");
}
}
// Derived class (Level 1)
class Mammal extends Animal {
void walk() {
[Link]("Mammal is walking");
}
}
// Derived class (Level 2)
class Dog extends Mammal {
void bark() {
[Link]("Dog is barking");
}
}
// Main class
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // From Animal
[Link](); // From Mammal
[Link](); // From Dog
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-10
Memon MD Uvesh Abdul Gani Roll:-270
Q10:Write a Java program to demonstrate inheritance and runtime
polymorphism by overriding methods in a derived class and invoking them
using a base class reference.
Ans:-// Base class
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
// Derived class 1
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
}
}
// Derived class 2
class Cat extends Animal {
@Override
void sound() {
[Link]("Cat meows");
}
}
// Main class
public class RuntimePolymorphismDemo {
public static void main(String[] args) {
// Base class reference
Animal a;
// Refers to Dog object
a = new Dog();
[Link](); // Calls Dog's sound()
// Refers to Cat object
a = new Cat();
[Link](); // Calls Cat's sound()
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-11
Memon MD Uvesh Abdul Gani Roll:-270
Q:-11:-Write a Java program to demonstrate the implementation of multiple
inheritance using interfaces.
Ans:-// First interface
interface Printable {
void print();
}
// Second interface
interface Showable {
void show();
}
// Class implementing multiple interfaces
class Demo implements Printable, Showable {
@Override
public void print() {
[Link]("Printing...");
}
@Override
public void show() {
[Link]("Showing...");
}
}
// Main class
public class MultipleInheritanceDemo {
public static void main(String[] args) {
Demo d = new Demo();
[Link]();
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-12
Memon MD Uvesh Abdul Gani Roll:-270
Q12-Write a Java program to demonstrate the use of static members, static block,
and static nested class, and explain how static data is shared among objects.
Ans:-
class Student {
// Static data member (shared among all objects)
static String college;
int id;
String name;
// Static block (executes once when class is loaded)
static {
college = "ABC Engineering College";
[Link]("Static block executed");
}
// Constructor
Student(int id, String name) {
[Link] = id;
[Link] = name;
}
// Static method
static void changeCollege(String newCollege) {
college = newCollege;
}
// Method to display details
void display() {
[Link](id + " " + name + " " + college);
}
// Static nested class
static class Department {
void showDepartment() {
[Link]("Department: Computer Science");
}
}
}
public class StaticDemo {
public static void main(String[] args) {
// Creating objects
Student s1 = new Student(1, "Alice");
Student s2 = new Student(2, "Bob");
Vimal Tormal Poddar BCA College Page no:-13
Memon MD Uvesh Abdul Gani Roll:-270
// Display before changing static data
[Link]();
[Link]();
// Change static variable
[Link]("XYZ University");
// Display after changing static data
[Link]();
[Link]();
// Access static nested class
[Link] dept = new [Link]();
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-14
Memon MD Uvesh Abdul Gani Roll:-270
Q13-Write a Java program to read a string and rewrite it in alphabetical order (e.g., the
word COMPUTER should be written as CEMOPRTU).
Ans:-
import [Link];
import [Link];
public class AlphabeticalOrderString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
// Convert string to character array
char[] ch = [Link]();
// Sort the character array
[Link](ch);
// Convert back to string
String sortedString = new String(ch);
[Link]("String in alphabetical order: " + sortedString);
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-15
Memon MD Uvesh Abdul Gani Roll:-270
Q14-Design an abstract class called Shape with subclasses Triangle, Rectangle, and
Circle. Define an abstract method area() in the Shape class and override it in each
subclass to calculate the respective areas.
Ans:-
// Abstract base class
abstract class Shape {
// Abstract method
abstract double area();
}
// Triangle subclass
class Triangle extends Shape {
double base, height;
Triangle(double base, double height) {
[Link] = base;
[Link] = height;
}
@Override
double area() {
return 0.5 * base * height;
}
}
// Rectangle subclass
class Rectangle extends Shape {
double length, width;
Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
double area() {
return length * width;
}
}
// Circle subclass
class Circle extends Shape {
double radius;
Circle(double radius) {
[Link] = radius;
}
Vimal Tormal Poddar BCA College Page no:-16
Memon MD Uvesh Abdul Gani Roll:-270
@Override
double area() {
return [Link] * radius * radius;
}
}
// Main class
public class AbstractShapeDemo {
public static void main(String[] args) {
Shape s;
s = new Triangle(10, 5);
[Link]("Area of Triangle: " + [Link]());
s = new Rectangle(8, 4);
[Link]("Area of Rectangle: " + [Link]());
s = new Circle(7);
[Link]("Area of Circle: " + [Link]());
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-17
Memon MD Uvesh Abdul Gani Roll:-270
Q16-Write a Java program to check whether a given string is a palindrome.
Ans:-
import [Link];
public class PalindromeString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reverse = "";
// Reverse the string
for (int i = [Link]() - 1; i >= 0; i--) {
reverse = reverse + [Link](i);
}
// Check palindrome
if ([Link](reverse)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-18
Memon MD Uvesh Abdul Gani Roll:-270
Q16-Write a Java program to modify a string by inserting and deleting characters at
specific positions using the StringBuffer class.
Ans:-import [Link];
public class StringBufferModifyDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
// Create StringBuffer object
StringBuffer sb = new StringBuffer(input);
// Insert operation
[Link](2, 'X'); // Inserts 'X' at index 2
[Link]("After insertion: " + sb);
// Delete operation
[Link](3, 5); // Deletes characters from index 3 to 4
[Link]("After deletion: " + sb);
[Link]();
}
}
Output:-
Vimal Tormal Poddar BCA College Page no:-19