Answer to Question #223927 in HTML/JavaScript Web Application for Sajid

Question #223927
Submarine
Given two numbers totalTorpedos, torpedosFired as inputs, write a super class Submarine with property and methods as below,
Property Description
isSubmerged It should contain a boolean value to indicate whether the submarine is submerged or not.
Method Description
dive When this method is called, it should set the value of isSubmerged to true and log "Submarine Submerged" text in the console.
surface When 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,
1
Expert's answer
2021-08-09T02:21:28-0400
class Submarine{
      isSubmerged = true;
      constructor(totalTorpedos, torpedosFired){
            this.totalTorpedos = totalTorpedos;
            this.torpedosFired = torpedosFired;
      }
      
      fireTorpedo(){
            console.log(`${this.torpedosFired} torpedos fired,${this.totalTorpedos - this.torpedosFired} torpedos left`);
            this.isSubmerged = false;
      }
      checkSubmerge(){
            if(this.isSubmerged === true){
                  console.log("Submarine Submerged");
            }else{
                  console.log("Submarine Resurface");
            }
      }
      submerge(){
            this.checkSubmerge();
            this.fireTorpedo();
            this.checkSubmerge();
      }
}
const submarine = new Submarine(10,2); 
submarine.submerge();

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS