Learn Computer Programming Free from our source codes in my website.
Sponsored Link Please Support
https://www.techseries.dev/a/27966/qWm8FwLb
https://www.techseries.dev/a/19181/qWm8FwLb
My Personal Website is http://www.jakerpomperada.com
Email me at jakerpomperada@gmail.com and jakerpomperada@yahoo.com
Friday, August 30, 2024
Tuesday, August 27, 2024
Monday, August 26, 2024
Saturday, August 24, 2024
Thursday, August 22, 2024
Wednesday, August 21, 2024
Tuesday, August 20, 2024
Monday, August 19, 2024
Sunday, August 18, 2024
Saturday, August 17, 2024
Friday, August 16, 2024
Thursday, August 15, 2024
Wednesday, August 14, 2024
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");
});
});
});