Write a Ship class that could be used in a game of Battleships and has the
following private member variables:
• name : string
• length : int
• hits : int
The Ship class will have the following member functions:
• Default Constructor – A default constructor that sets name to an empty
string, and sets both length and hits to zero (0).
• Constructor – A constructor that accepts values for name and length
as arguments and sets hits to zero (0).
• setName – A mutator function for the name member variable.
• setLength – A mutator function for the length member variable.
• setHits – A mutator function that accepts a new value for the hits
member variable. When the value of the hits member variable is
equal to the value of the length member variable, the function should
display the message: ” You sunk my <name>.” where <name> is the
name of the Ship object.
• getName – A function that returns the value of the name member
• getLength – A function that returns the value of the length member
• getHits – A function that returns the value of the hits member
• getDamage – A function that returns the damage percent as a double,
which is calculated as:
damage = 100.0 * hits / length
Write a program that demonstrates the Ship class by creating the following
ships and storing them in an array of Ship objects:
The program you write will use the no-argument default constructor to
create an instance of at least one of the ships. The program can then use
the mutator functions (setName and setLength) to define the initial member
variables for that ship.
Next, your program will prompt the user to enter any hits for each of the
ships. Your program will use the name of each ship when they prompt for
hits. Provide input validation that prevents the user from entering a number
of hits that is less than zero (0) or greater than the length of that ship.
Finally, your program will display the names, lengths, hits, and damage
percentages for each of the five ships.
Expected output looks like this:
Enter the number of hits on the Battleship (0 to 5): 3
Enter the number of hits on the Cruiser (0 to 4): 4
You sunk my Cruiser.
Enter the number of hits on the Destroyer (0 to 3): 2
Enter the number of hits on the Submarine 1 (0 to 2): 0
Enter the number of hits on the Submarine 2 (0 to 2): 1
Ship Name Length Hits Damage
———– —— —- ——-
Battleship 5 3 60.00%
Cruiser 4 4 100.00%
Destroyer 3 2 66.67%
Submarine 1 2 0 0.00%
Submarine 2 2 1 50.00%
Submit the following files so that your instructor can duplicate your success:
• Ship.h The specification file for the Ship class.
• Ship.cpp The implementation file for the Ship class.
• XYLab12.cpp The main program. “X” and “Y” are your initials
Define a C++ struct named Element with the following members:
• symbol : string, The symbol for the chemical element
• name : string, The name of the chemical element
• atomicNumber : int, The atomic number of the element
• atomicWeight : double, The atomic weight of the element
• active : bool, Flag variable controlling its visibility
Define an array of Element structures named elements that can hold up to
50 instances of the Element structure.
Write a menu-driven program that prompts your user with the following six
Enter your choice:
Option 1 will prompt the user for a chemical symbol, the corresponding
element name, atomic number, and atomic weight. It will then use that
information to define the next available entry in the array of elements.
Option 2 will prompt the user for a chemical symbol such as “C” for Carbon.
If that chemical symbol exists in the array of elements, the program will
display the symbol, name, atomic number, and atomic weight of that
element. If the chemical symbol is not in the array of elements, display an
informative message instead.
Option 3 will prompt the user for a chemical symbol such as “B” for Boron. If
that chemical symbol exists in the array of elements, the program will
assign the value false to the active member for that array entry. If the
chemical symbol does not exist in the array of elements, an informative
message will be displayed instead.
Copyright 2021, Dallas College. All Rights reserved. Page 2 of 3
Note: The structure member named active is initialized to true
when an entry is created. Once the member named active is
set to the value false in Option 3, it is invisible to all other
program options. Your program does not have to delete such an
entry. It just has to ignore it by not displaying it.
Option 4 prompts the user to identify a file containing the descriptions of
several elements This text file contains one record per line containing the
chemical symbol, element name, atomic number, and atomic weight of
several elements. The fields are separated from each other by a blank space.
A sample file named “elements.txt” is available in Blackboard to assist you
in testing this portion of your program. Your program will read and process
all the records in the file, adding an entry to the array of elements for each
record in the file.
Al Aluminum 13 26.982
Ar Argon 18 39.880
N Nitrogen 7 14.007
B Boron 5 10.810
Li Lithium 3 6.94
Be Beryllium 4 9.012
C Carbon 6 12.011
Ca Calcium 20 40.078
Cl Chlorine 17 35.450
Pb Lead 82 207.2
Na Sodium 11 22.990
K Potassiom 19 39.098
Kr Krypton 36 83.798
H Hydrogen 1 1.008
O Oxygen 8 15.999
Pd Palladium 46 106.42
Option 5 will display a formatted list of the contents of the elements array
sorted in ascending order by chemical symbol. This is an example of the
desired output from selecting this option:
Option 6 will terminate the program.
Your program will repeatedly prompt the user with the menu choices until
the user enters option 6 to quit. Use input validation to guard against your
user from entering menu choices outside the range from 1 to 6.
Use formatting to display the information contained in the array of Element
structures. Display atomic weights with three digits after the decimal point.
To receive full credit for this programming assignment, your submission
• Use a correctly formed file name.
• Submit a program that executes correctly.
• Interact effectively with the user in your prompts and messages.
Copyright 2021, Dallas College. All Rights reserved. Page 3 of 3
• Create a comment containing the student’s full name. (5 points)
• Document the program with other meaningful comments. (5 points)
• Repeatedly display the required choices. (10 points)
• Use input validation to verify the user’s option choice. (5 points)
• Process Option 1 correctly, adding an element. (15 points)
• Process Option 2 correctly, identifying and displaying an entry. (15
• Process Option 3 correctly, making an entry “invisible”. (10 points)
• Process Option 4 correctly, adding elements from a file. (15 points)
• Process Option 5 correctly, displaying the contents sorted in ascending
order by chemical symbol. (15 points)
• Use prompts and messages that are easy to understand. (5 points)
Assignment status: Solved by our experts