Thursday, October 3, 2024

Animals Sound Using Polymorphism in C++

Animals Sound Using Polymorphism in C++

 




Program Listing


#include <iostream>


class Animal {

public:

    virtual void makeSound() {

        std::cout << "The animal makes a sound." << std::endl;

    }

};


class Dog : public Animal {

public:

    void makeSound() override {

        std::cout << "\tThe dog barks." << std::endl;

    }

};



class Snake : public Animal {

public:

    void makeSound() override {

        std::cout << "\tThe snake ssshh." << std::endl;

    }

};


class Cat : public Animal {

public:

    void makeSound() override {

        std::cout << "\tThe cat meows." << std::endl;

    }

};


int main() {

    Animal* animals[3];

    animals[0] = new Dog();

    animals[1] = new Cat();

    animals[2] = new Snake();



 std::cout <<"\n\n\tAnimals Sound Using Polymorphism in C++\n\n";

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

        std::cout << "\tAnimal " << i + 1 << ": ";

        animals[i]->makeSound();

    }


    // Don't forget to delete the dynamically allocated objects to free memory.

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

        delete animals[i];

    }

std::cout << "\n\tEnd of Program. Thank you for using this program." << std::endl;

    return 0;

}


Sunday, September 8, 2024

What is an Exploit?

Student Records Using Encapsulation in Python

Student Records Using Encapsulation in Python

 Student Records Using Encapsulation in Python

                             Screenshot


Program Listing

class Student:

def __init__(self, student_name, student_age, student_grade):
self.name = student_name
self.age = student_age
self.grade = student_grade

def get_name(self):
return self.name

def get_age(self):
return self.age

def get_grade(self):
return self.grade

def set_name(self, student_name):
self.name = student_name

def set_age(self, student_age):
self.age = student_age

def set_grade(self, student_grade):
self.grade = student_grade


if __name__ == "__main__":
student = Student("Julianna Rae Pomperada", 16, 92)

print("\n\tStudent Records Using Encapsulation in Python")
print("\n\tDisplay Student Record\n")
print("\tStudent Name: " + student.get_name())
print("\tStudent Age: " + str(student.get_age()) + " years")
print("\tStudent Grade: " + str(student.get_grade()))

student.set_name("Jacob Samuel Pomperada")
student.set_age(18)
student.set_grade(88)

print("\n\tUpdated Student Record\n")
print("\tStudent Name: " + student.get_name())
print("\tStudent Age: " + str(student.get_age()) + " years old.")
print("\tStudent Grade: " + str(student.get_grade()))
print("\n\tEnd of Program. Thank you for using this program.")

Wednesday, August 14, 2024

What is Cyber Threats?

Addition and Subtraction of Two Numbers Using Polymorphism in JavaScript

 


Program Listing

const readline = require('readline');

class MathOperation {
    performOperation(a, b) {
        return 0; // Base class default implementation
    }
}

class Addition extends MathOperation {
    performOperation(a, b) {
        return a + b;
    }
}

class Subtraction extends MathOperation {
    performOperation(a, b) {
        return a - b;
    }
}

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("\n\tEnter the first number: ", (num1) => {
    rl.question("\tEnter the second number: ", (num2) => {
        rl.question("\tEnter the operation (+ for addition, - for subtraction): ", (op) => {
            let operation;

            if (op === '+') {
                operation = new Addition();
            } else if (op === '-') {
                operation = new Subtraction();
            } else {
                console.log("\tInvalid operation.");
                rl.close();
                process.exit();
            }

            const result = operation.performOperation(parseInt(num1), parseInt(num2));
            console.log("\n\tThe Result:", result);

            rl.close();
            console.log("\n\tEnd of Program. Thank you for using this program.\n");
        });
    });
});

Tuesday, July 23, 2024

What is Generative AI?

Addition and Subtraction Using Polymorphism in Java

Addition and Subtraction Using Polymorphism in Java

 


Program Listing

import java.util.Scanner;


class MathOperation {

    public int performOperation(int a, int b) {

        return 0; // Base class default implementation

    }

}


class Addition extends MathOperation {

    @Override

    public int performOperation(int a, int b) {

        return a + b;

    }

}


class Subtraction extends MathOperation {

    @Override

    public int performOperation(int a, int b) {

        return a - b;

    }

}


public class Main {

    public static void main(String[] args) {

        MathOperation operation = null;

        int num1, num2;

        char op;


        Scanner scanner = new Scanner(System.in);


        System.out.println("\n\n\tAddition and Subtraction Using Polymorphism in Java\n");

        System.out.print("\tEnter two numbers: ");

        num1 = scanner.nextInt();

        num2 = scanner.nextInt();

        System.out.print("\tEnter the operation (+ for addition, - for subtraction): ");

        op = scanner.next().charAt(0);


        if (op == '+') {

            operation = new Addition();

        } else if (op == '-') {

            operation = new Subtraction();

        } else {

            System.out.println("\tInvalid operation.");

            return;

        }


        int result = operation.performOperation(num1, num2);

        System.out.println("\n\tThe Result: " + result);


        scanner.close();


        System.out.println("\n\tEnd of Program. Thank you for using this program.");

    }

}