AIM: void display(){[Link].
println(acc_no+" "+name+"
"+amount);}
Write a Program in Java to implement the Classes and Objects.
}
ALGORITHM:
//Creating a test class to deposit and withdraw amount
STEP 1: Start the Program
class TestAccount{
STEP 2: Create Class
public static void main(String[] args){
STEP 3: Declare the Input and Output Variables
Account a1=new Account();
STEP 3: Create object and access the method
[Link](832345,"Ankit",1000);
STEP 4: Implement it with return type and without parameter
list [Link]();
STEP 5: Implement it with return type and with parameter list [Link]();
STEP 6: Implement the constructor by creating classes and [Link](40000);
object
[Link]();
class Account{
[Link](15000);
int acc_no;
[Link]();
String name;
}}
float amount;
RESULT:
//Method to initialize object
Thus the Java program to implement classes and objects was
void insert(int a,String n,float amt){ written, executed and the output was verified successfully
acc_no=a; AIM:
name=n; To write a program in java with Constructors and destructors
amount=amt; ALGORITHM:
}//deposit method STEP 1: Start the Program
void deposit(float amt){ STEP 2: Declare and Initialize the input variables
amount=amount+amt; STEP 3: Create the Constructors
[Link](amt+" deposited"); STEP 4: Create various methods for Subclass
} STEP 5: In derived class extend the previous class
//withdraw method STEP 6: In main class specify the values and create the object
void withdraw(float amt){ STEP 7: Stop
if(amount<amt){ // import [Link].*;
[Link]("Insufficient Balance"); class Geek
}else{ {
amount=amount-amt; int num;
[Link](amt+" withdrawn"); String name;
} // this would be invoked while an
} object
//method to check the balance of the account // of that class is created.
void checkBalance(){[Link]("Balance is: Geek()
"+amount);}
{
//method to display the values of an object
[Link]("Constructor
called"); result = number1 / number2;
[Link](number1 + " / "
} + number2 + " = " + result);
break;
}
default:
class GFG [Link]("Invalid
operator!");
{ break;
}
public static void main (String[] [Link]();
}
args) }
{
import [Link];
// this would invoke default
class BinarySearch {
constructor.
// Method to perform binary search
Geek geek1 = new Geek();
public static int binarySearch(int[] arr, int key) {
// Default constructor provides
int left = 0, right = [Link] - 1;
the default
while (left <= right) {
// values to the object like 0, null
int mid = left + (right - left) / 2;
[Link]([Link]);
// Check if key is at mid
[Link]([Link]);
if (arr[mid] == key) {
}
return mid; // Return index of the key
}
}
import [Link];
// If key is smaller, search in left half
class Main {
public static void main(String[] args) { else if (arr[mid] > key) {
char operator; right = mid - 1;
Double number1, number2, result;
Scanner input = new Scanner([Link]); }
[Link]("Choose an operator: // If key is larger, search in right half
+, -, *, or /");
operator = [Link]().charAt(0); else {
[Link]("Enter first
number"); left = mid + 1;
number1 = [Link]();
}
[Link]("Enter second
number"); }
number2 = [Link]();
return -1; // Key not found
switch (operator) {
}
case '+':
result = number1 + number2; public static void main(String[] args) {
[Link](number1 + " + "
+ number2 + " = " + result); Scanner sc = new Scanner([Link]);
break;
case '-': // Input size of array
result = number1 - number2;
[Link](number1 + " - "
[Link]("Enter the number of elements: ");
+ number2 + " = " + result);
break;
int n = [Link]();
case '*':
result = number1 * number2; int[] arr = new int[n];
[Link](number1 + " * "
+ number2 + " = " + result); // Input sorted array elements
break;
[Link]("Enter " + n + " sorted elements:");
case '/':
for (int i = 0; i < n; i++) { // Calling method to set details
arr[i] = [Link](); [Link]("Alice", 20);
} // Calling method to display details
// Input element to search [Link]("Student Information:");
[Link]("Enter the element to search: "); [Link]();
int key = [Link](); }
// Perform binary search }
int result = binarySearch(arr, key);
// Display result class Animal {
if (result != -1) void eat() {
{ [Link]("This animal eats food.");
[Link]("Element found at index: " + result); }
} else { }
[Link]("Element not found in the list."); // Child class inheriting from Animal
} class Dog extends Animal {
[Link](); void bark() {
} [Link]("The dog barks.");
} }
class Student { public class SingleInheritanceExample {
// Data members (instance variables) public static void main(String[] args) {
String name; // Creating an object of Dog class
int age; Dog myDog = new Dog();
// Method to set student details // Calling methods from both parent and child classes
void setDetails(String n, int a) { [Link](); // Inherited method
name = n; [Link](); // Child class method
age = a; }
} }
// Method to display student details class Animal {
void displayDetails() { void eat() {
[Link]("Student Name: " + name); [Link]("This animal eats food.");
[Link]("Student Age: " + age); }
} }
} // Derived class (Child of Animal)
public class ClassMechanismExample { class Mammal extends Animal {
public static void main(String[] args) { void walk() {
// Creating an object of Student class [Link]("Mammals can walk.");
Student s1 = new Student(); }
} // Creating objects of different shapes
// Derived class (Child of Mammal) Shape circle = new Circle(5);
class Dog extends Mammal { Shape rectangle = new Rectangle(4, 6);
void bark() { // Calculating areas
[Link]("The dog barks."); [Link]();
} [Link]();
} }
public class MultilevelInheritanceExample { }
public static void main(String[] args) {
// Creating an object of Dog class public class MultipleCatchExample {
Dog myDog = new Dog(); public static void main(String[] args) {
// Calling methods from all levels of inheritance try {
[Link](); // Inherited from Animal int arr[] = {10, 20, 30};
[Link](); // Inherited from Mammal int a = 10, b = 0;
[Link](); // Defined in Dog // This will cause ArithmeticException
} int result = a / b;
} [Link]("Result: " + result);
/ Interface definition // This will cause ArrayIndexOutOfBoundsException
interface Shape { [Link]("Array Element: " + arr[5]);
// Abstract method (no implementation) } catch (ArithmeticException e) {
void calculateArea(); [Link]("Exception caught: Division by zero is not
allowed.");
}
} catch (ArrayIndexOutOfBoundsException e) {
// Class implementing the interface
[Link]("Exception caught: Array index is out of
class Circle implements Shape { bounds.");
double radius; } catch (Exception e) {
// Constructor [Link]("Exception caught: General exception
occurred.");
Circle(double r) {
} finally {
radius = r;
[Link]("Execution completed.");
}
}
// Implementing interface method
}
public void calculateArea() {
}
double area = [Link] * radius * radius;
[Link]("Area of Circle: " + area);
public class InterfaceExample {
public static void main(String[] args) {