0% found this document useful (0 votes)
15 views

Experiment 3,4

b.tech

Uploaded by

23bcp471
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 views

Experiment 3,4

b.tech

Uploaded by

23bcp471
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
You are on page 1/ 17

Experiment-3

Q1(i)

//Write a java program for Method overloading

class Adder

static int add(int a,int b){return a+b;}

static int add(int a,int b,int c){return a+b+c;}

class TestOverloading1{

public static void main(String[] args){

System.out.println(Adder.add(11,11));

System.out.println(Adder.add(11,11,11));

}}

Output:-

Q1(ii)

//Constructor overloading

public class Student {

int id;

String name;

Student(){

System.out.println("this a default constructor");

Student(int i, String n){

id = i;
name = n;

public static void main(String[] args) {

Student s = new Student();

System.out.println("\nDefault Constructor values: \n");

System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);

System.out.println("\nParameterized Constructor values: \n");

Student student = new Student(10, "David");

System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);

Output:-

Q2

//Write a java program to display the employee details using Scanner class

import java.util.Scanner;

class Employee

{
int id;

String name;

String desig;

float salary;

class Main{

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("How many employees? ");

int n = sc.nextInt();

Employee emp[] = new Employee[n];

for (int i = 0; i < n; i++) {

emp[i] = new Employee();

System.out.println("Enter " + (i + 1) + " Employee data :");

System.out.print("Enter employee id :");

emp[i].id = sc.nextInt();

System.out.print("Enter employee name :");

emp[i].name = sc.next();

System.out.print("Enter employee designation :");

emp[i].desig = sc.next();

System.out.print("Enter employee salary :");

emp[i].salary = sc.nextFloat();

System.out.println("\n\n**** All Employee Details are :****\n");

for (int i = 0; i < n; i++) {

System.out.println("Employee id, Name, Designation and Salary :"

+ emp[0].id + " " + emp[i].name + " " + emp[i].desig + " " +emp[i].salary);

}
Output:-

Q3

import java.util.Scanner;

class Account {

private double balance;

public Account(double initialBalance) {

balance = initialBalance;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

System.out.println("Deposited: " + amount);

} else {

System.out.println("Invalid deposit amount.");

}
}

public void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

System.out.println("Withdrawn: " + amount);

} else {

System.out.println("Invalid withdrawal amount or insufficient balance.");

public void displayBalance() {

System.out.println("Current Balance: " + balance);

public class bank {

public static void main(String args[]) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter initial balance: ");

double initialBalance = scanner.nextDouble();

Account account = new Account(initialBalance);

while (true) {

System.out.println("\nSelect an option:");

System.out.println("1. Deposit");

System.out.println("2. Withdraw");

System.out.println("3. Display Balance");

System.out.println("4. Exit");
int choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.print("Enter deposit amount: ");

double depositAmount = scanner.nextDouble();

account.deposit(depositAmount);

break;

case 2:

System.out.print("Enter withdrawal amount: ");

double withdrawalAmount = scanner.nextDouble();

account.withdraw(withdrawalAmount);

break;

case 3:

account.displayBalance();

break;

case 4:

System.out.println("Exiting...");

scanner.close();

System.exit(0);

default:

System.out.println("Invalid choice. Please select a valid option.");

}
Q4

//Q4

//Write a java program for different types of constructors

class Person {

private String name;

private int age;

// Default Constructor

public Person() {

name = "Rahul";

age = 20;

// Parameterized Constructor

public Person(String name, int age) {


this.name = name;

this.age = age;

// Copy Constructor

public Person(Person otherPerson) {

this.name = otherPerson.name;

this.age = otherPerson.age;

// Getter methods

public String getName() {

return name;

public int getAge() {

return age;

public class Constructor {

public static void main(String[] args) {

// Using the default constructor

Person person1 = new Person();

System.out.println("Person 1: Name = " + person1.getName() + ", Age = " + person1.getAge());

// Using the parameterized constructor

Person person2 = new Person("Alice", 30);

System.out.println("Person 2: Name = " + person2.getName() + ", Age = " + person2.getAge());

// Using the copy constructor


Person person3 = new Person(person2);

System.out.println("Person 3: Name = " + person3.getName() + ", Age = " + person3.getAge());

Output:-

Q5

//Q5

//Write a java program for object as an argument

class Rectangle {

private double length;

private double width;

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

public double calculateArea() {

return length * width;

public class ObjectAsArgument {

public static void main(String[] args) {

Rectangle rectangle1 = new Rectangle(5.0, 3.0);

Rectangle rectangle2 = new Rectangle(7.0, 4.0);


double area1 = calculateRectangleArea(rectangle1);

System.out.println("Area of Rectangle 1: " + area1);

double area2 = calculateRectangleArea(rectangle2);

System.out.println("Area of Rectangle 2: " + area2);

public static double calculateRectangleArea(Rectangle rectangle) {

return rectangle.calculateArea();

Output:-
Experiment-4

Q1(i)

//Q1

//Single Inheritance

class Animal {

void eat() {

System.out.println("The dog eats.");

class Dog extends Animal {

void bark() {

System.out.println("The dog barks.");

public class SingleInheritance {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.eat(); // Inherited from the Animal class

myDog.bark(); // Specific to the Dog class

class Animal {

void eat() {

System.out.println("The dog eats.");

}
class Dog extends Animal {

void bark() {

System.out.println("The dog barks.");

public class SingleInheritance {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.eat(); // Inherited from the Animal class

myDog.bark(); // Specific to the Dog class

Output:-

Q1(ii)

//Q1(ii)

//multilevel

class Animal {

void eat() {

System.out.println("The dog eats.");

}
class Dog extends Animal {

void bark() {

System.out.println("The dog barks.");

class GermanShepherd extends Dog {

void guard() {

System.out.println("The dog guards.");

public class MultilevelInheritance {

public static void main(String[] args) {

GermanShepherd myDog = new GermanShepherd();

// Call methods from all three classes

myDog.eat();

myDog.bark();

myDog.guard();

Output:-
Q(iii)

//Q(iii)

//hierarchical inheritance

class Animal {

void eat() {

System.out.println("The dog eats.");

class Dog extends Animal {

void bark() {

System.out.println("The dog barks.");

class Cat extends Animal {

void meow() {

System.out.println("The dog plays.");

public class HierarchicalInheritance {

public static void main(String[] args) {

Dog myDog = new Dog();

Cat myCat = new Cat();

// Call methods from both Dog and Cat classes

myDog.eat();

myDog.bark();

System.out.println();
myCat.eat();

myCat.meow();

Output:-

Q2

//Write a java program to method overriding.

class Animal {

void makeSound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

void makeSound() {

System.out.println("Dog barks");

class Cat extends Animal {

void makeSound() {

System.out.println("Cat meows");

}
public class overriding {

public static void main(String[] args) {

Animal animal1 = new Dog();

Animal animal2 = new Cat();

animal1.makeSound();

animal2.makeSound();

Output:-

Q3.

class Animal {

String name;

Animal(String name) {

this.name = name;

void eat() {

System.out.println(name + " is eating.");

class Dog extends Animal {

String breed;
Dog(String name, String breed) {

super(name); // Call the constructor of the superclass with the 'name' parameter

this.breed = breed;

void bark() {

System.out.println(name + " is barking.");

void displayDetails() {

System.out.println("Name: " + this.name);

System.out.println("Breed: " + this.breed);

this.eat(); // Call the 'eat' method of the current instance

super.eat(); // Call the 'eat' method of the superclass using 'super'

public class keyword {

public static void main(String[] args) {

Dog myDog = new Dog("Buddy", "Golden Retriever");

myDog.displayDetails();

Output:-

You might also like