GURUNANAK INSTITUTE OF HIGHER EDUCATION,
MURTHAL
Practical file
Of
Object Technologies & Programming using Java
Submitted by: Submitted to:
Rohit Miss. Anjali Mam
GNI22/02/16
BCA 3rd Year
1
[Link] Topic Page Signature
no
1. Write a program to enter 5 values in an Array. 3-6
2. Write a program of an Array of size 3*4. 7
3. Write a program to print hello world. 8
4. Write a program to print year enter by the user. 9
5. Write a program of function overloading . 10-11
6. Write a program of constructor overloading. 12
7. Write a program of inheritance. 13
8. Write a program exception handling (Arithmetic). 14-15
9. Write a program to print Try and Catch block. 16-17
10. Write a program to implement Polymorphism. 18
11. Write a program to implement Abstraction.
12. Write a program to implement Fibonacci series.
13. Write a program to implement Factorials.
14. Write a program of Method overloading.
15. Write a program to implement Multi threading.
2
1. Write a program to enter 5 values in an Array.
Import [Link];
Public class ArrayInput {
Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Int[] numbers = new int[5]; // Array to store 5 values
// Input 5 values
[Link](“Enter 5 numbers:”);
For (int I = 0; I < 5; i++) {
[Link](“Enter number “ + (I + 1) + “: “);
Numbers[i] = [Link]();
}
// Display entered values
[Link](“\nYou entered:”);
For (int num : numbers) {
[Link](num + “ “);
}
}
[Link]();
}
3
2. Write a program of an Array of size 3*4.
Import [Link];
Public class TwoDArray {
Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Int[][] array = new int[3][4]; // 3x4 array
// Input values
[Link](“Enter 12 numbers for a 3x4 array:”);
For (int I = 0; I < 3; i++) {
For (int j = 0; j < 4; j++) {
[Link](“Enter value for [“ + I + “][“ + j + “]: “);
Array[i][j] = [Link]();
}
}
// Display the array
[Link](“\nThe 3x4 array is:”);
For (int I = 0; I < 3; i++) {
For (int j = 0; j < 4; j++) {
[Link](array[i][j] + “\t”);
}
[Link](); // New line for next row
}
[Link]();
}
}
4
3. Write a program to print hello world.
Import [Link];
Public class HelloWorld {
Public static void main(String[] args) {
[Link](“Hello, World!”);
}
}
5
[Link] a program to print year enter by user.
Import [Link];
Public class YearInput {
Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Asking the user to enter a year
[Link](“Enter a year: “);
Int year = [Link]();
// Printing the entered year
[Link](“You entered: “ + year);
[Link]();
}
}
6
5. Write a program of Function overloading.
Import [Link];
Public class FunctionOverloading {
// Method with one parameter
Public void display(int num) {
[Link](“Integer value: “ + num);
}
// Method with two parameters
Public void display(int num1, int num2) {
[Link](“Sum of two integers: “ + (num1 + num2));
}
// Method with a different type of parameter
Public void display(String text) {
[Link](“String value: “ + text);
}
Public static void main(String[] args) {
FunctionOverloading obj = new FunctionOverloading();
// Calling overloaded methods
[Link](10); // Calls method with one int parameter
[Link](10, 20); // Calls method with two int parameters
[Link](“Hello Java”); // Calls method with a String parameter
}
}
7
6. Write a program of Constructor overloading.
Import [Link];
Class Person {
String name;
Int age;
// Default constructor
Public Person() {
Name = “Unknown”;
Age = 0;
[Link](“Default Constructor: Name = “ + name + “, Age = “ + age);
}
// Constructor with one parameter
Public Person(String name) {
[Link] = name;
Age = 0;
[Link](“Constructor with one parameter: Name = “ + name + “, Age
= “ + age);
}
// Constructor with two parameters
Public Person(String name, int age) {
[Link] = name;
[Link] = age;
[Link](“Constructor with two parameters: Name = “ + name + “,
Age = “ + age);
}
}
Public class ConstructorOverloading {
Public static void main(String[] args) {
// Creating objects with different constructors
Person p1 = new Person(); // Calls default constructor
8
Person p2 = new Person(“Alice”); // Calls constructor with one parameter
Person p3 = new Person(“Bob”, 25); // Calls constructor with two parameters
}
}
9
[Link] a program of Inheritance.
Import [Link];
// Parent class (Superclass)
Class Animal {
String name;
Public void eat() {
[Link](name + “ is eating.”);
}
Public void sleep() {
[Link](name + “ is sleeping.”);
}
}
// Child class (Subclass) inheriting from Animal
Class Dog extends Animal {
Public void bark() {
[Link](name + “ is barking.”);
}
}
Public class InheritanceExample {
Public static void main(String[] args) {
Dog myDog = new Dog();
[Link] = “Buddy”; // Setting name inherited from Animal class
// Calling methods
[Link](); // Inherited method
[Link](); // Inherited method
[Link](); // Child class method
}
}
10
8. Write a program Exception Handling (Arithmetic).
Import [Link];
Public class ArithmeticExceptionExample {
Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Try {
// Taking input
[Link](“Enter numerator: “);
Int numerator = [Link]();
[Link](“Enter denominator: “);
Int denominator = [Link]();
// Performing division
Int result = numerator / denominator;
[Link](“Result: “ + result);
} catch (ArithmeticException e) {
// Handling division by zero exception
[Link](“Error: Cannot divide by zero.”);
} finally {
// Closing the scanner
[Link]();
[Link](“Execution completed.”);
}
}
}
11
9. Write a program of Try and Catch block.
Import [Link];
Public class TryCatchExample {
Public static void main(String[] args) {
Try {
// Code that may cause an exception
Int num = 10 / 0; // Division by zero error
[Link](“Result: “ + num);
} catch (ArithmeticException e) {
// Handling the exception
[Link](“Exception caught: Cannot divide by zero.”);
}
[Link](“Program continues after exception handling.”);
}
}
12
10. Write a program to implement Abstraction.
Import [Link];
// Abstract class
Abstract class Vehicle {
// Abstract method (must be implemented by subclasses)
Abstract void start();
// Concrete method (common functionality)
Void stop() {
[Link](“Vehicle stopped.”);
}
}
// Subclass implementing abstract method
Class Car extends Vehicle {
@Override
Void start() {
[Link](“Car starts with a key.”);
}
}
// Another subclass
13
Class Bike extends Vehicle {
@Override
Void start() {
[Link](“Bike starts with a self-start button.”);
}
}
Public class AbstractionExample {
Public static void main(String[] args) {
Vehicle myCar = new Car();
[Link](); // Calls Car’s start method
[Link](); // Calls Vehicle’s stop method
Vehicle myBike = new Bike();
[Link](); // Calls Bike’s start method
[Link](); // Calls Vehicle’s stop method
}
}
14
[Link] a program to implement Fibonacci series.
Import [Link];
Public class FibonacciIterative {
Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link](“Enter the number of terms: “);
Int n = [Link]();
Int first = 0, second = 1;
[Link](“Fibonacci Series: “ + first + “ “ + second + “ “);
For (int I = 2; I < n; i++) {
Int next = first + second;
[Link](next + “ “);
First = second;
Second = next;
}
[Link]();
}
}
15
12. Write a program to implement Factorials .
Import [Link];
Public class FactorialRecursive {
// Recursive method to calculate factorial
Public static long factorial(int n) {
If (n == 0 || n == 1) // Base case
Return 1;
Return n * factorial(n – 1); // Recursive call
}
Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link](“Enter a number: “);
Int n = [Link]();
[Link](“Factorial of “ + n + “ is: “ + factorial(n));
[Link]();
}
}
16
13. Write a program of Method overriding.
Import [Link];
// Parent class
Class Animal {
Public void makeSound() {
[Link](“Animal makes a sound”);
}
}
// Child class overriding the method
Class Dog extends Animal {
@Override
Public void makeSound() {
[Link](“Dog barks”);
}
}
// Another child class
Class Cat extends Animal {
@Override
Public void makeSound() {
[Link](“Cat meows”);
17
}
}
Public class OverridingExample {
Public static void main(String[] args) {
Animal myAnimal; // Reference of type Animal
myAnimal = new Dog(); // Dog object
[Link](); // Calls Dog’s overridden method
myAnimal = new Cat(); // Cat object
[Link](); // Calls Cat’s overridden method
}
}
18
14. Write a program to implement Multithreading.
Import [Link];
Class MyThread extends Thread {
Public void run() {
For (int I = 1; I <= 5; i++) {
[Link]([Link]().getName() + “ – Count: “ + i);
Try {
[Link](500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
[Link]€;
}
}
}
}
Public class MultithreadingExample1 {
Public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link](“Thread 1”);
[Link](“Thread 2”);
[Link](); // Starts first thread
[Link](); // Starts second thread
}
19
}
20