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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\n 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)\n",shoot[0]+1,shoot[1]+1);
return true;
}
//System.out.printf("You missed a Submerine located in (%d,%d)\n",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

Add a comment
Know the answer?
Add Answer to:
Create a base class called WaterVehicle that has: -length of ship (in number of grid spaces)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

  • C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember...

    C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember class should have a constructor with no parameters, a constructor with a parameter, a mutator function and an accessor function. Also, include a function to display the name. Define a class called Athlete which is derived from FitnessMember. An Athlete record has the Athlete's name (defined in the FitnessMember class), ID number of type String and integer number of training days. Define a class...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property...

    PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property 1 constructor that takes 3 arguments pass each argument to the appropriate property Task 2 In a new file import the Window class Instantiate the Window object Output the two public properties Task 3 Alter the Window class in Task 1 Add an accessor and mutator (the ability to access and change) to the private property Task 4 Create a class named Instrument Give...

  • Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's...

    Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given in Listing 8.1 in the textbook and in LMS). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double, since it may contain a fractional part) and towing capacity in tons (type double). Give your classes...

  • Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to...

    Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the annualInterestRate to a new value....

  • Application should be in C# programming language Create a class called SavingsAccount.  ...

    Application should be in C# programming language Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the...

  • Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...

    Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template (20 pts) – TempDriver.cpp Template Exercise Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values. Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object). You will need: The FeetInches class (which was provided in Week...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT