Experiment 3,4
Experiment 3,4
Q1(i)
class Adder
class TestOverloading1{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output:-
Q1(ii)
//Constructor overloading
int id;
String name;
Student(){
id = i;
name = n;
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{
int n = sc.nextInt();
emp[i].id = sc.nextInt();
emp[i].name = sc.next();
emp[i].desig = sc.next();
emp[i].salary = sc.nextFloat();
+ emp[0].id + " " + emp[i].name + " " + emp[i].desig + " " +emp[i].salary);
}
Output:-
Q3
import java.util.Scanner;
class Account {
balance = initialBalance;
if (amount > 0) {
balance += amount;
} else {
}
}
balance -= amount;
} else {
while (true) {
System.out.println("\nSelect an option:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("4. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
account.deposit(depositAmount);
break;
case 2:
account.withdraw(withdrawalAmount);
break;
case 3:
account.displayBalance();
break;
case 4:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
}
Q4
//Q4
class Person {
// Default Constructor
public Person() {
name = "Rahul";
age = 20;
// Parameterized Constructor
this.age = age;
// Copy Constructor
this.name = otherPerson.name;
this.age = otherPerson.age;
// Getter methods
return name;
return age;
Output:-
Q5
//Q5
class Rectangle {
this.length = length;
this.width = width;
return rectangle.calculateArea();
Output:-
Experiment-4
Q1(i)
//Q1
//Single Inheritance
class Animal {
void eat() {
void bark() {
class Animal {
void eat() {
}
class Dog extends Animal {
void bark() {
Output:-
Q1(ii)
//Q1(ii)
//multilevel
class Animal {
void eat() {
}
class Dog extends Animal {
void bark() {
void guard() {
myDog.eat();
myDog.bark();
myDog.guard();
Output:-
Q(iii)
//Q(iii)
//hierarchical inheritance
class Animal {
void eat() {
void bark() {
void meow() {
myDog.eat();
myDog.bark();
System.out.println();
myCat.eat();
myCat.meow();
Output:-
Q2
class Animal {
void makeSound() {
void makeSound() {
System.out.println("Dog barks");
void makeSound() {
System.out.println("Cat meows");
}
public class overriding {
animal1.makeSound();
animal2.makeSound();
Output:-
Q3.
class Animal {
String name;
Animal(String name) {
this.name = name;
void eat() {
String breed;
Dog(String name, String breed) {
super(name); // Call the constructor of the superclass with the 'name' parameter
this.breed = breed;
void bark() {
void displayDetails() {
myDog.displayDetails();
Output:-