Question

In: Computer Science

C++ Program : You are working as a programmer designing a space ship weapon system for...

C++ Program : You are working as a programmer designing a space ship weapon system for the newly

forced Space Force. The weapon system is a generic weapon housing that can t a number

of dierent weapons that are all controlled by a ship pilot in the same way. Most im-

portantly, is the emphasis on safety. During combat, the weapon systems cannot become

unreliable or fail lest the pilots be put in unnecessary danger. Therefore you will need to

provide mechanisms to combat this.

2.2.1 weaponMount

This is the mounting system for the weapons. It can store a number of weapons and

control them as well as handle problems. It is dened as follows:

weaponMount

-weapons:weapon **

-numWeapons: int

---------------------------

+weaponMount(numWeapons:int, weaponList: string *)

+~weaponMount()

+accessWeapon(i:int):weapon *

The class variables are as follows:

weapons: A 1D array of weapon pointers. It must be able to accept any type of

weapon dened in the hierarchy.

numWeapons: The number of weapons that are mounted into the system.

The class methods are as follows:

weaponMount: This is the constructor. It will receive a list of weapons as a string

array plus the number of weapons. It must allocate memory for the weapons variable

and create a weapon based on the information provided by the string array. Create

one weapon of the type indicated at each index of the array. For example ["Doom

Cannon","Ion Cannon"] means create a doomCannon weapon at index 0 and so on.

The default strength for an ion cannon is 5.

weaponMount: The class destructor. It must deallocate all of the memory assigned

to the class.

accessWeapon: This receives an int which provides an index in the weapons list. It

will return the weapon that is stored at that position. If no such weapon is found

there, then throw a weaponFailure exception.

2.2.2 weaponFailure

This is a custom exception class used in the context of this system. It will inherit publicly

from the exception class. You will need to override specically the what method to return

the statement "Weapon System Failure!" without the quotation marks. The name of this

class is weaponFailure. This exception will be used to indicate a failure of the weapon

system. You will implement this exception in the weaponMount class as a struct with

public access. You will need to research how to specify exceptions due to the potential

for a "loose throw specier error" and what clashes this might have with a compiler.

Remember that implementations of classes and structs must be done in .cpp les.

2.2.3 ammoOut

This is a custom exception class used in the context of this system. It will inherit publicly

from the exception class. You will need to override specically the what method to

return the statement "Ammo Depleted!" without the quotation marks. The name of this

class is ammoOut. This exception will be used to indicate a depletion of ammunition

for a weapon. You will implement this exception in the weapon class as a struct with

public access. You will need to research how to specify exceptions due to the potential

for a "loose throw specier error" and what clashes this might have with a compiler.

Remember that implementations of classes and structs must be done in .cpp les.

2.2.4 Weapon Parent Class

This is the parent class of ionCannon and doomCannon. Both of these classes inherit

publicly from it. It is dened according to the following UML diagram:

weapon

-ammo:int

-type:string

-------------------

+weapon()

+weapon(a:int, t:string)

+getAmmo():int

+getType():string

+setAmmo(s:int):void

+setType(s:string):void

+weapon()

+fire()=0:string

The class variables are as follows:

ammo: The amount of ammo stored in the weapon. As it is red, this will deplete.

type: The type of the weapon as a string. Examples include: "Rail Ri

Cannon","Doom Cannon", "Missile Launcher" and "Ion Cannon".

The class methods are as follows:

weapon: The default class constructor.

weapon(a:int, t:string): The constructor. It will take two arguments and instantiate

the class variables accordingly.

getAmmo,setAmmo: The getter and setter for the ammo.

getType,setType: The getter and setter for the ammo.

weapon: The destructor for the class. It is virtual.

fire: This is the method that will re each of the weapons and produce a string of

the outcome. It is pure virtual here.

2.2.5 ionCannon

The ionCannon is dened as follows:

ionCannon

-strength:int

------------------------------

+ionCannon(s:int)

+~ionCannon()

+setStrength(s:int):void

+getStrength() const:int

+fire():string

The class variables are as follows:

strength: The strength of the ion cannon. Ion cannons get stronger the longer they

are red.

The class methods are as follows:

ionCannon: The class constructor. This receives an initial strength for the ion

cannon.

ionCannon: This is the destructor for the ion cannon. It prints out "Ion Cannon

Uninstalled!" without the quotation marks and a new line at the end when the class

is deallocated.

fire: If the cannon still has ammo, it must decrease the ammo by 1. It will also

increase the strength by 1. It will return the following string: "Ion Cannon red at

strength: X" where X represents the strength before ring. Do not add quotation

marks. If ammo is not available, instead throw the ammoOut exception. No new

lines should be added.

getStrength/setStrength: The getter and setter for the strength variable.

2.2.6 doomCannon

The doomCannon is dened as follows:

doomCannon

-charge:int

------------------------------

+doomCannon()

+~doomCannon()

+getCharge():int

+setCharge(s:int):void

+fire():string

The class methods are as follows:

doomCannon: This is the constructor for the class. It will set the charge to 0.

doomCannon: This is the destructor for the ion cannon. It prints out "Doom

Cannon Uninstalled!" without the quotation marks and a new line at the end when

the class is deallocated.

setCharge/getCharge: The getter and setter for the charge variable.

re: The cannon can only re if it has ammo and only charge if it has ammo as

well. The charging process does not require ammo. Firing it will reduce the ammo

by 1. However the doom cannon requires a charging up period. Since the doom

cannon starts with 0 charge, it will not re until it reaches 5 charge, inclusive of the

5. Therefore every time it is red, it will increase in charge by 1. If the charge is not

at 5, then return the string "DOOM CANNON CHARGING" without quotation

marks. If the cannon starts at 5 charge when red reset the charge back to 0 and

return the following string: "DOOM CANNON FIRED!". Do not add quotation

marks. If ammo is not available, instead throw the ammoOut exception. No new

lines should be added.

You will be allowed to use the following libraries: sstream,exception, cstring,string,

iostream. Your submission must contain weaponMount.h, weaponMount.cpp, ionCannon.h, ionCannon.cpp, doom-

Cannon.h, doomCannon.cpp,weapon.h, weapon.cpp, main.cpp

Solutions

Expert Solution

weapon.h file:

#pragma once
#include <iostream>
using namespace std;

class weapon {
private:
   int ammo; // current amount of ammunition stored in the weapon
   string type; // type of weapon
public:
   weapon(); // default constructor
   weapon(int a, string t); // constructor
   virtual ~weapon(); // virtual destructor
   int getAmmo(); // returns amount of ammo stored in weapon
   string getType(); //returns type of weapon
   void setAmmo(int a); // set new amount of ammo in weapon
   void setType(string t); //set new weapon type
   virtual string fire() = 0; // pure virtual function need to be implimented into sub classes
   struct ammoOut : public exception {
       virtual char const* what() const;
   };
};

weapon.cpp file:

#include "weapon.h"

weapon::weapon() {
   ammo = 0;
   type = "N/A";
}

weapon::weapon(int a, string t) {
   ammo = a;
   type = t;
}

weapon::~weapon() {
   // uninstall weapon
   // virtual destructor calls derived class's destructor
}

int weapon::getAmmo() {
   return ammo;
}

string weapon::getType() {
   return type;
}

void weapon::setAmmo(int a) {
   ammo = a;
}

void weapon::setType(string t) {
   type = t;
}

char const* weapon::ammoOut::what() const {
   return "Ammo Depleted!";
}

ionCannon.h file:

#pragma once
#include "weapon.h"

class ionCannon : public weapon{
private:
   int strength; // strength of ion cannon
public:
   ionCannon(int s); // constructor
   ~ionCannon(); // destructor
   int getStrength(); // returns strength of ion cannon
   void setStrength(int s); // set new strength of ion cannon
   string fire(); // fires the weapon if it hase ammo left
};

ionCannon.cpp file:

#include "ionCannon.h"
#include <string>

ionCannon::ionCannon(int s) {
   // set initial strength of ion cannon
   strength = s;
   // set type of weapon
   setType("Ion Cannon");
}
  
ionCannon::~ionCannon() {
   // prints message
   cout << "Ion Cannon Uninstalled!" << endl;
}
  
int ionCannon::getStrength() {
   return strength;
}
  
void ionCannon::setStrength(int s) {
   strength = s;
}
  
string ionCannon::fire() {
   // check for ammo
   if (getAmmo() > 0) {
       // fire the weapon
       setAmmo(getAmmo() - 1); // reduce ammo by 1
       //increase strength by 1
       setStrength(getStrength() + 1);
       // return message
       string str = "Ion Cannon red at strength: " + to_string(getStrength() - 1); // -1 as strength represent before ring
       return str;
   }
   else {
       // throw ammoOut exception
       throw ammoOut();
   }
}

doomCannon.h file:

#pragma once
#include "weapon.h"

class doomCannon : public weapon {
private:
   int charge; //
public:
   doomCannon(); // constructor
   ~doomCannon(); // destructor
   int getCharge(); // returns amount of charge
   void setCharge(int s); // set new charge
   string fire(); // fires doomCannon at charge 5 if it hase ammo
};


doomCannon.cpp file:

#include "doomCannon.h"

doomCannon::doomCannon() {
   //set initial charge to 0
   charge = 0;
   //set type of weapon
   setType("Doom Cannon");
}

doomCannon::~doomCannon() {
   // print message
   cout << "Doom Cannon Uninstalled!" << endl;
}

int doomCannon::getCharge() {
   return charge;
}

void doomCannon::setCharge(int s) {
   charge = s;
}

string doomCannon::fire(){
   // check for ammo
   if (getAmmo() > 0) {
       // red the doom cannon
       //check for charge
       if (charge == 5) {
           // fire the cannon
           setCharge(0); // set charge to 0 after cannon is fired
           // reduce ammo by 1
           setAmmo(getAmmo() - 1);
           return "DOOM CANNON FIRED!";
       }
       else {
           // charge the cannon
           // increase charge by 1;
           charge++;
           return "DOOM CANNON CHARGING";
       }
   }
   else {
       // throw ammoOut exception
       throw ammoOut();
   }
}

weaponMount.h file:

#pragma once
#include "ionCannon.h"
#include "doomCannon.h"

class weaponMount {
private:
   weapon** weapons; // array of weapon pointers
   int numWeapons; // number of weapon mountedd into the system
public:
   weaponMount(int _numWeapon, string* weaponList); // constructor
   ~weaponMount(); // deconstructor
   weapon* accessWeapon(int i); // returns weapon at ith position
   struct weaponFailure : public exception {
       virtual char const* what() const;
   };
};

weaponMount.cpp file:

#include "weaponMount.h"

weaponMount::weaponMount(int _numWeapon, string* weaponList) {
   // takes weapon list string and creates numWeapon weapon objects and stores in weapons array variable
   numWeapons = _numWeapon; // initialize variables
   // allocate memory for weapons
   weapons = new weapon * [numWeapons];
   // get individual weapon type from weaponList
   for (int i = 0; i < numWeapons; i++) {
       // get ith weapon from weaponList
       // create weapon according to type and store it in weapons
       if (weaponList[i].compare("Ion Cannon") == 0) {
           // create ion cannon weapon
           weapons[i] = new ionCannon(5);
       }
       else if (weaponList[i].compare("Doom Cannon") == 0) {
           // create doom cannon weapon
           weapons[i] = new doomCannon();
       }
   }
}

weaponMount::~weaponMount() {
   //destroy all weapons and free the memomry
   for (int i = 0; i < numWeapons; i++) {
       delete weapons[i];
   }
   delete[] weapons;
}

weapon* weaponMount::accessWeapon(int i) {
   // if weapon is not found throw weaponFailure exception
   if(i>=numWeapons){
       throw weaponFailure();
   }
   else {
       return weapons[i]; // return the weapon
   }
}

char const* weaponMount::weaponFailure::what() const {
   return "Weapon System Failure!";
}

main.cpp file:

#include "weaponMount.h"

int main() {
   cout << "create weaponMount system with 2 weapons" << endl;
   string weaponlist[] = { "Doom Cannon","Ion Cannon" };
   weaponMount *wms = new weaponMount(2, weaponlist);
   cout << "Add 2 ammo to first weapon and 3 to second weapon" << endl;
   wms->accessWeapon(0)->setAmmo(2);
   wms->accessWeapon(1)->setAmmo(3);
   cout << "fire first weapon" << endl;
   try {
       cout << wms->accessWeapon(0)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire first weapon" << endl;
   try {
       cout << wms->accessWeapon(0)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire first weapon" << endl;
   try {
       cout << wms->accessWeapon(0)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire first weapon" << endl;
   try {
       cout << wms->accessWeapon(0)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire first weapon" << endl;
   try {
       cout << wms->accessWeapon(0)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire first weapon" << endl;
   try {
       cout << wms->accessWeapon(0)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "Get second weapon type: " << wms->accessWeapon(1)->getType() << endl;
   cout << "fire second weapon" << endl;
   try {
       cout << wms->accessWeapon(1)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire second weapon" << endl;
   try {
       cout << wms->accessWeapon(1)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire second weapon" << endl;
   try {
       cout << wms->accessWeapon(1)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << endl;
   cout << "fire second weapon" << endl;
   try {
       cout << wms->accessWeapon(1)->fire();
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << "fire third weapon" << endl;
   try {
       cout << wms->accessWeapon(2);
   }
   catch (const exception& e) {
       cout << e.what() << endl;
   }
   cout << "Uninstall WeaponMount system" << endl;
   delete wms;
   return 0;
}


Related Solutions

*I was given the following problem to make a C program* You are a programmer at...
*I was given the following problem to make a C program* You are a programmer at Disney World. You need to create a program that will allow people to choose fast passes for three different attractions. 1. Space mountain 2. Haunted Mansion 3. Pirates of the Caribbean The fast passes are given out in 15 minute increments from 9am - 5pm Sunday-Saturday. The user can only obtain three fast passes per day - you must be able to tell the...
1. The space force developed a new weapon system that makes an object experience no air...
1. The space force developed a new weapon system that makes an object experience no air drag. The objects have a mass of 100 kg. If one of the objects is dropped from a space weapons platform from a height of 136.3 miles, how much Kinetic Energy would it be able to release upon impact? ( Only work in the y dimension) A stick of dynamite has 1.7 MJ of energy. How many sticks of dynamite equivalent would this be?...
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However this time the statement is designed in the style of a Logical Circuit. You are asked to create three truth tables for the three problems. To achieve this, you should create SEVEN gate functions for your checkpoint. For each of the problem: First, convert the diagram to...
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However the statement is designed in the style of a Logical Circuit.You are asked to create three truth tables for the three problems.First, convert the diagram to boolean algebra equations.Then evaluate their values step by step based on the values of A, B and C. Finally, print the...
who designs computer system? a. system analyst b. operator c. manager d. programmer
who designs computer system? a. system analyst b. operator c. manager d. programmer
You recently started working in at a University in the Math department as a software programmer....
You recently started working in at a University in the Math department as a software programmer. You need to build an app that can calculate the mod of two numbers. Users need to enter num1 and num2. The app should perform the calculation and display the output as num1 mod nub2 operation. For example, 25 mod 5=0 24 mod 5 = 4 Once you implement the mod operations, then you need to add following buttons. Addition Subtraction Division Power Follow...
C++ program Dairy Farm decided to ship milk in containers in the form of cubes rather...
C++ program Dairy Farm decided to ship milk in containers in the form of cubes rather than cylinders. Write a program that prompts the user to input: The radius of the base of a cylindrical container The height of the cylindrical container The program then outputs: The side of the cube with the same volume as the cylindrical container with a precision of 2 decimal places. You may assume that the value of π = 3.141593.
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Suppose that you are working as part of a team designing a network for XYZ high...
Suppose that you are working as part of a team designing a network for XYZ high school. Consider that the school has six departments, Admin, Academic, Human Resource (HR), Finance, IT support and Sports. 1. Analyse the project hardware requirements along with the number of devices and cost. 2. Justify the approach you would be taking to design the network for the school. 3. Using the network simulator (such as packet tracer) design the network. 4. Illustrate the network configuration...
Suppose that you are working as part of a team designing a network for XYZ high...
Suppose that you are working as part of a team designing a network for XYZ high school. Consider that the school has six departments, Admin, Academic, Human Resource (HR), Finance, IT support and Sports. 1. Analyse the project hardware requirements along with the number of devices and cost. [3 Marks] 2. Justify the approach you would be taking to design the network for the school. [3 Marks] 3. Using the network simulator (such as packet tracer) design the network. [4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT