Answer to Question #182462 in Java | JSP | JSF for adhi chinna

Question #182462

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,

  1. Submerge the Submarine
  2. Fire torpedos
  3. Surface the Submarine

Input

  • The first line of input contains a number totalTorpedos
  • The second line of input contains a number torpedosFired

Output

  • The first line of output is a string with Submarine Submerged text
  • The second line of output is a string with the number of torpedos fired and left, as shown in sample outputs
  • The third line of output is a string with Submarine Surfaced text

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




1
Expert's answer
2021-04-18T17:29:02-0400
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();  
    }
}

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