Create a base class called WaterVehicle that has: -length of ship (in number of
ID: 3889600 • Letter: C
Question
Create a base class called WaterVehicle that has: -length of ship (in number of grid spaces) starting grid location -horizontal or vertical orientation on grid -sunk (boolean) Then create a class called Submarine that is derived from WaterVehicle and has the following additional properties: -dive depth -surfaced (Boolean) Be sure your classes have a reasonable complement of constructors, accessor, and mutator methods including a public function to determine if the Submarine was hit by a torpedo and whether a hit sunk the ship. Additionally, create an overloaded assignment operator. Write a program which will allow input of the ship length, grid location, orientation, and dive depth. Assume the grid where the submarine is to be placed is a 10 by 10 grid. Randomly generate 15 torpedo shots, output the shot grid locations to the screen, and then output whether the submarine was hit or missed or sunk. The submarine should be the object to understand whether it was hit or sunk. Then create a second submarine and use your overloaded assignment operator to assign the values from the first submarine to the second submarine. Print out the information about the second submarine.Explanation / Answer
You have not specified programming langauge so i have written in JAVA if you want in C/C++ please comment
import java.util.Scanner;
/**
*
* @author Veerendra Patel
*/
class WaterVehicle{
WaterVehicle()
{}
int Length;
int[][] StartingLocation=new int[10][10];;
String Orientation;//horizontal or Vertical;
boolean sunk;
}
class SubMarine extends WaterVehicle
{
int Depth;//in meters
boolean Surfaced;
int[] shoot = new int[2];
void printDetails()
{
System.out.println("Length:"+Length);
System.out.println("Orinetation:"+Orientation);
System.out.println("Depth:"+Depth);
System.out.println("Surfaced:"+Surfaced);
System.out.println("Sunk:"+sunk);
if(sunk==true)
{
System.out.println(" you a hit submerine");
}
System.out.println("Length:"+Length);
}
void readDetails()
{
Scanner input=new Scanner(System.in);
System.out.print("Enter Length:");
Length=input.nextInt();
System.out.print("Enter starting Location row number:");
int row=input.nextInt();
System.out.print("Enter starting Location column number:");
int col=input.nextInt();
StartingLocation[row][col]=1;
System.out.print("Enter Orientaion:");
Orientation=input.next();
System.out.print("Enter Depth:");
Depth=input.nextInt();
System.out.print("Enter Surfaced(true or false):");
Surfaced=input.hasNextBoolean();
sunk=false;
}
void shot(int[] shoot)
{
//Scanner input = new Scanner(System.in);
//System.out.print("Row: ");
int randomNum = 0 + (int)(Math.random() * 9);
shoot[0] = randomNum;
shoot[0]--;
shoot[1] = randomNum = 0 + (int)(Math.random() * 9);
shoot[1]--;
}
boolean hit(int[] shoot, int[][] ships){
for(int ship=0 ; ship<ships.length ; ship++)
{
if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1])
{
System.out.printf("You hit a Submerine located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
return true;
}
//System.out.printf("You missed a Submerine located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
}
return false;
}
}
public class Vehicle {
public static void main(String[] args)
{
int[] shoot = new int[2];
SubMarine s1=new SubMarine();
s1.readDetails();
boolean res;
for(int i=0;i<15;i++){
s1.shot(shoot);
res=s1.hit(shoot,s1.StartingLocation);
s1.sunk=res;
}
s1.printDetails();
}
}
OUTPUT:
Enter Length:2
Enter starting Location row number:1
Enter starting Location column number:1
Enter Orientaion:vertical
Enter Depth:3
Enter Surfaced(true or false):true
You hit a Submerine located in (1,2)
Length:2
Orinetation:vertical
Depth:3
Surfaced:true
Sunk:false
Length:2