Day 10 - 24 August, 2024

Program 28: WAP to understand the concept of constructor

class Student {
    int rollno; // instance variable
    String name;
    double fee;

    Student(int rollno, String name, double fee) {
        this.rollno = rollno;
        this.name = name;
        this.fee = fee;
    }

    public void display() {
        System.out.println("Roll No.: " + rollno);
        System.out.println("Student Name: " + name);
        System.out.println("Fee: " + fee);
    }
}

class P28 {
    public static void main(String[] args) {
        Student s1 = new Student(1, "Hardik Srivastava", 4000);
        s1.display();
        Student s2 = new Student(2, "Sanskar Dubey", 6000);
        s2.display();
    }
}
                
Expected Output:

Roll No.: 1
Student Name: Hardik Srivastava
Fee: 4000.0

Roll No.: 2
Student Name: Sanskar Dubey
Fee: 6000.0

Program 29: WAP to create a program to understand the concept of Single Inheritance

class Rundog {
    public void bark() {
        System.out.println("Sheru.............");
        System.out.println("Bhau....Bhau......");
    }
}

class Bulldog extends Rundog {
    public void growl() {
        System.out.println("Tuffy.............");
        System.out.println("gruu....gurr......");
    }
}

class P29 {
    public static void main(String[] args) {
        Bulldog d1 = new Bulldog();
        d1.growl();
        d1.bark();

        Rundog d2 = new Rundog();
        d2.bark();
    }
}
                
Expected Output:

Tuffy.............
gruu....gurr......

Sheru.............
Bhau....Bhau......

Sheru.............
Bhau....Bhau......

Program 30: WAP to create a program to understand the concept of Hierarchical Inheritance

import java.util.*;

class Shape {
    int s;
    public void setValue(int side) {
        s = side;
    }
}

class Square extends Shape {
    public int area() {
        return s * s;
    }
}

class Cube extends Shape {
    public int volume() {
        return s * s * s;
    }
}

class P30 {
    public static void main(String[] args) {
        int s, a, v;
        Scanner sc = new Scanner(System.in);

        Square sq = new Square();
        System.out.print("Enter Side of Square: ");
        s = sc.nextInt();
        sq.setValue(s);
        a = sq.area();
        System.out.println("Area of Square: " + a);

        Cube cu = new Cube();
        System.out.print("Enter Side of Cube: ");
        s = sc.nextInt();
        cu.setValue(s);
        v = cu.volume();
        System.out.print("Volume of Cube: " + v);
    }
}
                
Expected Output:

Input:
Enter Side of Square: 4
Enter Side of Cube: 4

Output:
Area of Square: 16
Volume of Cube: 64