Submarine
Given two numbers
totalTorpedos, torpedosFired as inputs, write a super class Submarine with property and methods as below,PropertyDescription
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
import java.util.Scanner;
class Submarine{
private final int totalTorpedos;
private final int torpedosFired;
/**
* Constructor
* @param totalTorpedos
* @param torpedosFired
*/
public Submarine(int totalTorpedos,int torpedosFired){
this.totalTorpedos=totalTorpedos;
this.torpedosFired=torpedosFired;
}
public void displayInformation(){
int leftTorpedos=this.totalTorpedos-this.torpedosFired;
System.out.println("Submarine Submerged");
System.out.println(this.torpedosFired+" Torpedos Fired, "+leftTorpedos+" Left");
System.out.println("Submarine Surfaced");
}
}
public class Q182462 {
/***
* Main method
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int totalTorpedos;
int torpedosFired;
System.out.print("Enter the number of torpedos: ");
totalTorpedos=input.nextInt();
System.out.print("Enter the number of torpedos fired: ");
torpedosFired=input.nextInt();
Submarine submarine =new Submarine(totalTorpedos,torpedosFired);
submarine.displayInformation();
input.close();
}
}
Comments
Leave a comment