BCAIII JAVAASSIGNMENT
class Employee1
{ private String name;
private int age;
public String getName()
{ return name;
}
public void setName(String name)
{ [Link] = name;
}
public int getAge()
{ return age;
}
public void setAge(int age)
{ [Link] = age;
}
}
public class Employee {
public static void main(String[] args)
{ Employee1 emp = new Employee1();
[Link]("vaishali Sahu");
[Link](21);
[Link]("Employee Name: " + [Link]());
[Link]("Employee Age: " + [Link]());
}
}
Output:
D:\Vaishali\[Link] Page1
BCAIII JAVAASSIGNMENT
Program2:WriteaprogramtodemonstrateconceptofPolymorphism(Over
riding).
Source Code:
class Animal {
public void sound()
{ [Link]("Animal makes a
sound");
}
}
class Dog extends Animal {
public void sound()
{ [Link]("Dog
barks");
}
}
class Cat extends Animal {
public void sound()
{ [Link]("Cat
meows");
}
}
public class Program2 {
public static void main(String[] args)
{ Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
[Link]();
[Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page2
BCAIII JAVAASSIGNMENT
Program2:WriteaprogramtodemonstrateconceptofPolymorphism(Over
riding).
D:\Vaishali\[Link] Page3
BCAIII JAVAASSIGNMENT
Program3:Writeaprogramthatprintprimenumberupto1to100. Source
Code:
public class Program3 {
public static void main(String[] args) {
for (int number = 1; number <= 100; number++)
{ if (isPrime(number)) {
[Link](number+", ");
}
}
}
public static boolean isPrime(int num)
{ if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++)
{ if (num % i == 0) {
return false;
}
}
return true;
}
}
Output:
D:\Vaishali\[Link] Page4
BCAIII JAVAASSIGNMENT
Program4:Writeaprogramtoprintfirst10numbersofthefollowingseriesusing Do-
While Loops 0,1,1,2,3,5,8,13…….
Source Code:
public class Program4 {
public static void main(String[] args) {
int a = 0, b = 1, count = 0;
[Link](a + ", " + b);
count = 2;
do {
int next = a + b;
[Link](", " + next);
a = b;
b = next;
count++;
} while (count < 10);
}
}
Output:
D:\Vaishali\[Link] Page5
BCAIII JAVAASSIGNMENT
Program 5: Write a program to sort the element of the One Dimensional Array in
ascending order.
Source Code:
public class Program5 {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};
int n = [Link];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++)
{ [Link](arr[i] + " ");
}
}
}
Output:
D:\Vaishali\[Link] Page6
BCAIII JAVAASSIGNMENT
Program 6: Write a program for multiplication of matrix using I/O stream.
Source Code:
import [Link];
public class Program6 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows for the first matrix: ");
int rows1 = [Link]();
[Link]("Enter number of columns for the first matrix: ");
int cols1 = [Link]();
[Link]("Enter number of rows for the second matrix: ");
int rows2 = [Link]();
[Link]("Enter number of columns for the second matrix: ");
int cols2 = [Link]();
if (cols1 != rows2) {
[Link]("Matrix multiplication is not possible.");
return;
}
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows2][cols2];
int[][] result = new int[rows1][cols2];
[Link]("Enter elements of the first matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++)
{ matrix1[i][j] = [Link]();
}
}
[Link]("Enter elements of the second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++)
{ matrix2[i][j] = [Link]();
}
}
D:\Vaishali\[Link] Page7
BCAIII JAVAASSIGNMENT
for (int i = 0; i < rows1; i++)
{ for (int j = 0; j < cols2; j++)
{
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
[Link]("Resultant matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++)
{ [Link](result[i][j] + " ");
}
[Link]();
}
}
}
Output:
D:\Vaishali\[Link] Page8
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page9
BCAIII JAVAASSIGNMENT
Program 7: Write a program for addition of matrix using I/O stream.
Source Code:
import [Link];
public class Program7 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows for the matrices: ");
int rows = [Link]();
[Link]("Enter number of columns for the matrices:
"); int cols = [Link]();
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
int[][] result = new int[rows][cols];
[Link]("Enter elements of the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{ matrix1[i][j] = [Link]();
}
}
[Link]("Enter elements of the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{ matrix2[i][j] = [Link]();
}
}
for (int i = 0; i < rows; i++)
{ for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
[Link]("Resultant matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{ [Link](result[i][j] + " ");
D:\Vaishali\[Link] Page10
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page11
BCAIII JAVAASSIGNMENT
}
[Link]();
}
}
}
Output:
D:\Vaishali\[Link] Page12
BCAIII JAVAASSIGNMENT
Program 8: Write a program for transpose of matrix using I/O stream.
Source Code:
import [Link];
public class Program8 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows for the matrix: ");
int rows = [Link]();
[Link]("Enter number of columns for the matrix: ");
int cols = [Link]();
int[][] matrix = new int[rows][cols];
int[][] transpose = new int[cols][rows];
[Link]("Enter elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{ matrix[i][j] = [Link]();
}
}
for (int i = 0; i < rows; i++)
{ for (int j = 0; j < cols; j++)
{
transpose[j][i] = matrix[i][j];
}
}
[Link]("Transpose of the matrix:");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++)
{ [Link](transpose[i][j] + " ");
}
[Link]();
}
}
}
D:\Vaishali\[Link] Page13
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page14
BCAIII JAVAASSIGNMENT
Output:
D:\Vaishali\[Link] Page15
BCAIII JAVAASSIGNMENT
Program 9: Write a program to add the elements of Vector as arguments of main
method (Run time) and rearrange them, and copy it into an Array.
Source Code:
import [Link].*;
class Program9 {
public static void main(String arg[])
{ Vector<String> list = new
Vector<>(); int len = [Link];
for (int i = 0; i < len; i++) {
[Link](arg[i]);
}
[Link]("BCA", 0);
int size = [Link]();
String str[] = new String[10];
[Link](str);
[Link]("List of Class:");
for (int i = 0; i < size; i++) {
[Link](str[i]);
}
}
}
Output:
D:\Vaishali\[Link] Page16
BCAIII JAVAASSIGNMENT
Program 10 : Write a program to check that the given String is palindrome or not.
Source Code:
public class Palindrome
{
public static void main(String[] args)
{
String str = "Radar", reverseStr
=""; int strLength = [Link]();
for (int i = (strLength - 1); i >=0; --i)
{
reverseStr = reverseStr + [Link](i);
}
if ([Link]().equals([Link]()))
{
[Link](str + " is a Palindrome String.");
}
else
{
[Link](str + " is not a Palindrome String.");
}
}
}
Output:
D:\Vaishali\[Link] Page17
BCAIII JAVAASSIGNMENT
Program11:WriteaprogramtoarrangetheStringinalphabeticalorder. Source
Code:
import [Link].*;
class Program11 {
public static void main(String arg[]) {
String S[] = { "Raigarh", "Raipur", "Abhanpur", "Bhilai", "Korba" };
int len = [Link];
String temp;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (S[j].compareTo(S[i]) < 0)
{ temp = S[i];
S[i] = S[j];
S[j] = temp;
}
}
}
[Link]("Sorted Strings are:");
for (int i = 0; i < len; i++) {
[Link](S[i]);
}
}
}
Output:
D:\Vaishali\[Link] Page18
BCAIII JAVAASSIGNMENT
Program12:WriteaprogramforStringBufferclasswhichperformsallthe
methods of that class.
Source Code:
class Program12 {
public static void main(String[] args)
{ StringBuffer sb = new StringBuffer("Vaishali");
[Link]("Sahu ");
[Link]("After append: " + sb);
[Link](8, ",");
[Link]("After insert: " + sb);
[Link](5, 6, "!");
[Link]("After replace: " + sb);
[Link](5, 7);
[Link]("After delete: " + sb);
[Link]();
[Link]("After reverse: " + sb);
int capacity = [Link]();
[Link]("Current capacity: " +
capacity); int length = [Link]();
[Link]("Current length: " + length);
[Link](5);
[Link]("After setLength(5): " + sb);
}
}
Output:
D:\Vaishali\[Link] Page19
BCAIII JAVAASSIGNMENT
Program 13: Write a program to calculate Simple Interest using the Wrapper Class.
Source Code:
import [Link];
class Program13 {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);
[Link]("Enter Principal (P): ");
String principalInput = [Link]();
[Link]("Enter Rate of Interest (R):
"); String rateInput = [Link]();
[Link]("Enter Time (T) in years: ");
String timeInput = [Link]();
Double principal = [Link](principalInput);
Double rate = [Link](rateInput);
Double time = [Link](timeInput);
Double simpleInterest = (principal * rate * time) / 100;
[Link]("Simple Interest = " + simpleInterest);
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page20
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page21
BCAIII JAVAASSIGNMENT
Program 14: Write a program to calculate area of various geometrical figures using
the abstract class.
Source Code:
import [Link];
abstract class Shape {
abstract void calculateArea();
}
class Circle extends Shape
{ double radius;
Circle(double radius) {
[Link] = radius;
}
void calculateArea() {
double area = 3.14 * radius * radius;
[Link]("Area of Circle: " + area);
}
}
class Rectangle extends Shape
{ double length, width;
Rectangle(double length, double width)
{ [Link] = length;
[Link] = width;
}
void calculateArea() {
double area = length * width;
[Link]("Area of Rectangle: " + area);
}
}
class Triangle extends Shape {
double base, height;
Triangle(double base, double height)
{ [Link] = base;
[Link] = height;
}
void calculateArea() {
double area = 0.5 * base * height;
[Link]("Area of Triangle: " + area);
D:\Vaishali\[Link] Page22
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page23
BCAIII JAVAASSIGNMENT
}
}
public class Program14 {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);
[Link]("Enter radius of circle: ");
double radius = [Link]();
Circle circle = new Circle(radius);
[Link]();
[Link]("Enter length and width of rectangle: ");
double length = [Link]();
double width = [Link]();
Rectangle rectangle = new Rectangle(length, width);
[Link]();
[Link]("Enter base and height of triangle: ");
double base = [Link]();
double height = [Link]();
Triangle triangle = new Triangle(base, height);
[Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page24
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page25
BCAIII JAVAASSIGNMENT
Program 15: Write a program where Single class implements more than one class
interfaces and with the help of interface reference variable user call the methods.
Source Code:
interface Printable {
void print();
}
interface Showable
{ void show();
}
class MultiInterfaceClass implements Printable, Showable {
public void print() {
[Link]("Printing from Printable interface.");
}
public void show() {
[Link]("Showing from Showable interface.");
}
}
public class Program15 {
public static void main(String[] args) {
Printable printable = new MultiInterfaceClass();
Showable showable = new MultiInterfaceClass();
[Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page26
BCAIII JAVAASSIGNMENT
Program 16: Write a program that uses the multiple catch statements within the
try-catch mechanism.
Source Code:
import [Link];
public class Program16 {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]); try {
[Link]("Enter numerator: ");
int numerator = [Link]();
[Link]("Enter denominator: ");
int denominator = [Link]();
int result = numerator / denominator;
[Link]("Result: " + result);
[Link]("Enter the size of the array: ");
int size = [Link]();
int[] array = new int[size];
[Link]("Enter the index to access: ");
int index = [Link]();
[Link]("Value at index: " + array[index]);
} catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
} catch (ArrayIndexOutOfBoundsException e)
{ [Link]("Error: Array index is out of bounds.");
} catch (NegativeArraySizeException e)
{ [Link]("Error: Array size cannot be
negative.");
} catch (Exception e) {
[Link]("Error: An unexpected error occurred.");
} finally {
[Link]("Execution completed.");
[Link]();
}
}
}
Output:
D:\Vaishali\[Link] Page27
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page28
BCAIII JAVAASSIGNMENT
Program 17: Write a program where user will create a Self- Exception using the
“throw” keyword.
Source Code:
import [Link];
class InvalidAgeException extends Exception {
public InvalidAgeException(String message)
{ super(message);
}
}
public class Program17 {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
try {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or older to vote.");
} else {
[Link]("You are eligible to vote.");
}
} catch (InvalidAgeException e) {
[Link]("Caught Exception: " + [Link]());
} finally {
[Link]("Program execution completed.");
[Link]();
}
}
}
Output:
D:\Vaishali\[Link] Page29
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page30
BCAIII JAVAASSIGNMENT
Program 18: Write a program to set the priority of threading.
Source Code:
class PriorityThread extends Thread {
public PriorityThread(String name)
{ super(name);
}
public void run() {
for (int i = 1; i <= 3; i++) {
[Link](getName() + " with priority " + getPriority() + " is running.");
}
}
}
public class Program18 {
public static void main(String[] args)
{ PriorityThread t1 = new PriorityThread("Thread-
1"); PriorityThread t2 = new
PriorityThread("Thread-2"); PriorityThread t3 = new
PriorityThread("Thread-3");
[Link](Thread.MIN_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page31
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page32
BCAIII JAVAASSIGNMENT
Program 19: Write a program for multithread using the isAlive(), join(), and
synchronized() methods of Thread class.
Source Code:
class SharedResource {
synchronized void printNumbers(String threadName) {
for (int i = 1; i <= 5; i++) {
[Link](threadName + " prints: " +
i); try {
[Link](500);
} catch (InterruptedException e)
{ [Link]([Link]());
}
}
}
}
class WorkerThread extends Thread
{ SharedResource resource;
WorkerThread(String name, SharedResource resource)
{ super(name);
[Link] = resource;
}
public void run()
{ [Link](getName());
}
}
public class Program19 {
public static void main(String[] args)
{ SharedResource resource = new SharedResource();
WorkerThread t1 = new WorkerThread("Thread-1", resource);
WorkerThread t2 = new WorkerThread("Thread-2", resource);
[Link]();
[Link]();
try {
[Link]("Is " + [Link]() + " alive? " + [Link]());
[Link]();
[Link]("Is " + [Link]() + " alive after join? " + [Link]());
[Link]();
} catch (InterruptedException e)
{ [Link]([Link]()
);
D:\Vaishali\[Link] Page33
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page34
BCAIII JAVAASSIGNMENT
}
[Link]("All threads have completed execution.");
}
}
Output:
D:\Vaishali\[Link] Page35
BCAIII JAVAASSIGNMENT
Program 20: Write a program to create a package using command and one package
will import another package.
Source Code:
Package mypack;
public class A
{
public void show()
{
[Link]("First class");
}
}
package mypack.mapack2;
public class B
{
public void show()
{
[Link]("second class");
}
}
Import mypack.*;
import mypack.mapack2.*;
class packt
{
public static void main(String are[])
{
A a=new A();
[Link]();
B b=new B();
[Link]();
}
}
Output:-
D:\Vaishali\[Link] Page36
BCAIII JAVAASSIGNMENT
Program 21: Write a program for JDBC to display the records from the existing
table.
Source Code:
import [Link].*;
public class JDData {
public static void main(String[] args)
{ String databasePath = "E:\\BCA III
YEAR\\[Link]"; String url =
"jdbc:ucanaccess://" + databasePath; Connection
connection = null;
try {
connection =
[Link](url); Statement
statement = [Link]();
String query = "SELECT * FROM
STUDENT"; ResultSet resultSet =
[Link](query);
while ([Link]()) {
[Link]("ROLLNO: " + [Link]("ROLLNO"));
[Link]("NAME: " + [Link]("NAME"));
}
[Link]();
[Link]();
} catch (Exception e)
{ [Link]()
;
} finally
{ try {
if (connection != null)
{ [Link]();
}
} catch (Exception e)
{ [Link]()
;
}
}
}
D:\Vaishali\[Link] Page37
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page38
BCAIII JAVAASSIGNMENT
}
Output:
D:\Vaishali\[Link] Page39
BCAIII JAVAASSIGNMENT
Program 22: Write a program to demonstrate the Nested Class.
Source Code:
class OuterClass {
private String message = "Hello from OuterClass!";
class InnerClass {
public void displayMessage()
{ [Link](message);
}
}
static class StaticNestedClass {
public void displayStaticMessage()
{ [Link]("Hello from StaticNestedClass!");
}
}
}
public class Program22 {
public static void main(String[] args) {
OuterClass outer = new OuterClass();
[Link] inner = [Link] InnerClass();
[Link]();
[Link] staticNested = new [Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page40
BCAIII JAVAASSIGNMENT
Program 23: Write a program to demonstrate the anonymous inner class.
Source Code:
class Program23
{ interface Greet {
void sayHello();
}
public static void main(String[] args) {
Greet greeting = new Greet() {
public void sayHello() {
[Link]("Hello! This is an example of an anonymous inner class.");
}
};
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page41
BCAIII JAVAASSIGNMENT
Program24:Writeaprogramtodemonstratethestaticinnerclass. Source
Code:
classProgram24{
static class Inner
{ void display() {
[Link]("Hello! This is a static inner class.");
}
}
public static void main(String[] args) {
[Link] innerObj = new [Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page42
BCAIII JAVAASSIGNMENT
Program24:Writeaprogramtodemonstratethestaticinnerclass. Source
Code:
classProgram24{
D:\Vaishali\[Link] Page43
BCAIII JAVAASSIGNMENT
Program 25: Write a program for Switch Statement.
Source Code:
import [Link];
class Program25 {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);
[Link]("Enter a number (1-3):");
int choice = [Link]();
switch (choice)
{ case 1:
[Link]("You chose 1: Hello!");
break;
case 2:
[Link]("You chose 2: Welcome!");
break;
case 3:
[Link]("You chose 3: Goodbye!");
break;
default:
[Link]("Invalid choice. Please enter a number between 1 and 3.");
}
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page44
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page45
BCAIII JAVAASSIGNMENT
Program 26: Write a program for Final keyword.
(A) Final Variable.
Source Code:
class Program26a {
final int CONSTANT = 100;
void showConstant() {
[Link]("The value of CONSTANT is: " + CONSTANT);
}
public static void main(String[] args)
{ FinalVariableDemo obj = new FinalVariableDemo();
[Link]();
// Uncommenting the below line will give an error because final variables cannot
// be modified
// [Link] = 200;
[Link]("Final variable.");
}
}
Output:
D:\Vaishali\[Link] Page46
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page47
BCAIII JAVAASSIGNMENT
(B) Final Method.
Source Code:
class FinalMethodDemo {
final void display() {
[Link]("This is a final method. It cannot be overridden.");
}
}
class SubClass extends FinalMethodDemo {
// Uncommenting the below method will give an error because final methods cannot
// be overridden
// void display() {
// [Link]("Trying to override a final method.");
// }
}
public class Program26b {
public static void main(String[] args)
{ FinalMethodDemo obj = new
FinalMethodDemo(); [Link]();
[Link]("Final method .");
}
}
Output:
D:\Vaishali\[Link] Page48
BCAIII JAVAASSIGNMENT
(c). Final class
Source Code:-
// create a final class
final class FinalClass
{
public void display()
{
[Link]("This is a final method.");
}
}
// try to extend the final
class class Main extends
FinalClass
{
public void display()
{
[Link]("The final method is overridden.");
}
public static void main(String[] args)
{
Main obj = new Main();
[Link]();
}
Output:-
D:\Vaishali\[Link] Page49
BCAIII JAVAASSIGNMENT
Program 27: Write a program for creating a file and to store data into that file.
(Using the FileWriterIOStream).
Source Code:
import [Link].*; public
class Charwrite
{
public static void main(String arg[])
{
FileWriter fout = null;
String msg = "Hello";
try
{
fout = new
FileWriter("[Link]");
[Link](msg);
[Link]("data
written"); [Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}
Output:-
D:\Vaishali\[Link] Page50
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page51
BCAIII JAVAASSIGNMENT
Program 28: Write a program to display your file in DOS console use the
Input/Output Stream.
Source Code:
import [Link].*;
class charread
{
public static void main(String arg[])
{
FileReader fin = null;
int b;
try
{
fin=new
FileReader("[Link]");
while((b = [Link]())!=-1)
{
[Link]((char)b);
}
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}
Output:-
D:\Vaishali\[Link] Page52
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page53
BCAIII JAVAASSIGNMENT
Program 29: Write a program Abstract keyword.
Source Code:
abstract class Animal
{ abstract void
sound();
}
class Dog extends Animal
{ void sound() {
[Link](" Dogs Bark");
}
}
class Program29 {
public static void main(String[] args) {
Animal myDog = new Dog();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page54
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page55
BCAIII JAVAASSIGNMENT
Program 30: Write a program demonstrate Constructor.
Source Code:
class Car
{ String
model; int
year;
Car(String model, int year)
{ [Link] = model;
[Link] = year;
}
void display() {
[Link]("Car Model: " + model);
[Link]("Car Year: " + year);
}
}
public class Program30 {
public static void main(String[] args)
{ Car myCar = new Car("Toyota",
2020); [Link]();
}
}
Output:
D:\Vaishali\[Link] Page56
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page57
BCAIII JAVAASSIGNMENT
Program 31: Write a program to demonstrate parameterized Constructor.
Source Code:
class Book
{ String title;
String author;
Book(String title, String author)
{ [Link] = title;
[Link] = author;
}
void display() {
[Link]("Book Title: " + title);
[Link]("Book Author: " + author);
}
}
public class Program31 {
public static void main(String[] args) {
Book myBook = new Book("The Alchemist", "Paulo Coelho");
[Link]();
Book anotherBook = new Book("1984", "George Orwell");
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page58
BCAIII JAVAASSIGNMENT
Program 32: Write a program demonstrate copy Constructor.
Source Code:
class Book
{ String title;
String author;
Book(String title, String author)
{ [Link] = title;
[Link] = author;
}
// Copy Constructor
Book(Book other) {
[Link] = [Link];
[Link] = [Link];
}
void display() {
[Link]("Book Title: " + title);
[Link]("Book Author: " + author);
}
}
public class Program32 {
public static void main(String[] args) {
Book originalBook = new Book("The Alchemist", "Paulo Coelho");
Book copiedBook = new Book(originalBook);
[Link]();
[Link]();
}
}
Output:
D:\Vaishali\[Link] Page59
BCAIII JAVAASSIGNMENT
Program 33: Write a program demonstrate Constructor overloading.
Source Code:
class Rectangle
{ int length;
int width;
// Default constructor
Rectangle() {
length = 0;
width = 0;
}
// Parameterized constructor
Rectangle(int length, int width) {
[Link] = length;
[Link] = width;
}
void display()
{ [Link]("Length: " +
length); [Link]("Width: " +
width);
}
int area() {
return length * width;
}
}
public class Program33 {
public static void main(String[] args)
{ Rectangle defaultRectangle = new
Rectangle(); [Link]();
[Link]("Area: " + [Link]());
Rectangle customRectangle = new Rectangle(10, 5);
[Link]();
[Link]("Area: " + [Link]());
}
}
Output:
D:\Vaishali\[Link] Page60
BCAIII JAVAASSIGNMENT
D:\Vaishali\[Link] Page61
BCAIII JAVAASSIGNMENT
Program 34: Write a program demonstrate anonyms array.
Source Code:
class Example {
void display(int[] arr)
{ for (int i : arr) {
[Link](i);
}
}
}
public class Program34 {
public static void main(String[] args) {
Example obj = new Example();
// Anonymous array
[Link](new int[] { 1, 2, 3, 4, 5 });
}
}
Output:
D:\Vaishali\[Link] Page62
BCAIII JAVAASSIGNMENT
Program 35: Write a program demonstrate garbage collection with finalize().
Source Code:
public class GcCollection
{ protected void finalize() {
[Link]("Garbage collector called");
[Link]("Object garbage collected");
}
public static void main(String[] args)
{ GcCollection g1 = new GcCollection();
GcCollection g2 = new GcCollection();
g1 = null;
[Link]();
g2 = null;
[Link]().gc(); }
}
Output:
D:\Vaishali\[Link] Page63