Submarine
Given two numbers
totalTorpedos, torpedosFired as inputs, write a super class Submarine with property and methods as below,
PropertyDescriptionisSubmergedIt should contain a boolean value to indicate whether the submarine is submerged or not.
MethodDescriptiondiveWhen this method is called, it should set the value of isSubmerged to true and log "Submarine Submerged" text in the console.surfaceWhen this method is called, it should set the value of isSubmerged to false and log "Submarine Surfaced" text in the console.
Add a sub class weaponUnit which extends to Submarine with the below properties and methods,
PropertyDescriptiontorpedosIt should contain the totalTorpedos loaded.torpedosLaunchedIt should contain the torpedosFired.
MethodDescriptionfireTorpedosWhen this method is called, it should decrease the totalTorpedos by torpedosFired and log the number of torpedos fired and left, as shown in the sample outputs.
The sequence of operations is,
Sample Input 1
5
2
Sample Output 1
Submarine Submerged
2 Torpedos Fired, 3 Left
Submarine Surfaced
Sample Input 2
10
2
Sample Output 2
Submarine Submerged
2 Torpedos Fired, 8 Left
Submarine Surfaced
i want code in between write your code here
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
class Submarine {
constructor() {
this.isSubmerged = false;
}
/* Write your code here */
}
class WeaponUnit extends Submarine {
/* Write your code here */
}
/* Please do not modify anything below this line */
function main() {
const totalTorpedos = parseInt(readLine());
const torpedosFired = parseInt(readLine());
const weaponUnit1 = new WeaponUnit(totalTorpedos, torpedosFired);
weaponUnit1.dive();
weaponUnit1.fireTorpedos();
weaponUnit1.surface();
}
Comments
Leave a comment