Question

In: Computer Science

(C++ with main to test) Task 1 Imagine that you are an engineer on a distant...

(C++ with main to test)


Task 1
Imagine that you are an engineer on a distant planet. You were tasked with mining the
resources of the planet and have done so through largely primitive means. However you
have managed to set up an automated train system to gather and collect the resources
from your distant operations. Your train system collects boxes full of resources. A box
might contain ores or wood or even liquid fuel. These boxes are placed into the train
using drones who stack them on each other to save space. You now need to implement
a management system using the newly researched data structures that will prove useful.
In particular, the rapid pace of your development means that resources are accumulated
quickly and fixed sized structures would be unwise. Therefore you will be implementing
the stack through the use of a linked list.
2.2.1
resrcStack
The class is defined according to the simple UML diagram below:
resrcStack<T>
-top: stackNode<T>*
2----------------------------
+resrcStack()
+~resrcStack()
+push(t: stackNode<T>*):void
+pop():void
+peek():stackNode <T>*
+print():void
+tallyWeights():int
+calculateStorageRequirements():string
The class variables are defined below:
• top: The current top of the stack. It will start as null but should refer to the top of
the stack.
The class methods are defined below:
• resrcStack: The class constructor. It will start by initialising the variables to null.
• ∼resrcStack: The class destructor. It will deallocate all of the memory assigned by
the class.
• push: This will receive a stackNode to add onto the current stack. The node is
added from the top.
• pop: This will remove the top resrcNode from the stack. If it is empty, print out
”EMPTY” with a newline at the end and no quotation marks. When removing the
node, it should be deleted.
• peek: This will return the top node of the stack but without removing it from the
stack.
• print: This will print the entire contents of the stack. For each resrc node in the
stack, print the following information out sequentially, line by line. The format of
this is as follows:
Resource:
Quantity:
Resource:
Quantity:
Unrefined Ores
12
Refined Alloys
1000
Remember that when printing, the first nodes added will be printed last. The first
node to be printed should be the last node pushed into the stack.
• tallyWeights: This function will tally up the weights for all nodes in the stack and
return their total weight. If empty, return -1.
• calculateStorageRequirements: This function must determine what the storage re-
quirement for the materials contained in the stack. The resulting type of storage
unit is returned. The storage requirements are determined as follows:
31. wooden crate: If the weights of the items together are less than 100, a wooden
crate should prove sufficient.
2. steel crate: A steel crate will be required if the weights of the items total 200
or more and the number of nodes, is greater than 5 but less than 10.
3. silo: A silo is required when the number of node is greater than 10 under all
circumstances.
This should return wooden crate, steel crate or silo. If none of the conditions are
met, return ”LOGISTICS ERROR” without the quotation marks. The wooden
crate takes precedence over the steel crate which takes precedence over the silo for
what to return.
2.2.2
stackNode
The class is defined according to the simple UML diagram below:
stackNode<T>
-resrc:T
+next:stackNode *
-weight: int
------------------------
+stackNode(i:T,w:int)
+~stackNode()
+getResrc():T
+getWeight():int
The class variables are defined below:
• resrc: This is the basic resource unit. It will describe the resource contained within
the box. It might be a code that describes the contents or just a description and so
must be a template type to accommodate for a variety of manifests.
• next: A pointer to the next node of the stack.
• weight: A variable which describes the total weight of the resources contained within
the class.
The class methods are defined below:
• stackNode: A class constructor. It receives the template type and the weight for
that item.
• ∼stackNode: The class destructor. It should print out ”Resource Unit Destroyed”
with no quotation marks and a new line at the end.
• getResrc: This returns the template variable stored within the class.
You will be allowed to use the following libraries: string, iostream.

Solutions

Expert Solution

Source code:

#include <iostream>

using namespace std;

//stack node class
template <class T>
class stackNode{
T resrc;
int weight;
  
public:
stackNode* next;
stackNode(T i, int w){
resrc = i;
weight = w;
next = NULL;
}
  
T getResrc(){
return resrc;
}
  
int getWeight(){
return weight;
}
  
~stackNode(){
cout << "Resource Unit Destroyed" << endl;
}
};

//resrcStack class
template <class T>
class resrcStack{
stackNode<T>* top;
  
public:
//constructor
resrcStack(){
top = NULL;
}
  
//destructor
~resrcStack(){
delete top;
}
  
//push a node
void push(stackNode<T>* t){
if(top == NULL){
top = t;
}
else{
t->next = top;
top = t;
}
}
  
//pop a node
stackNode<T>* pop(){
stackNode<T>* st = NULL;
if(top == NULL){
cout << "EMPTY" << endl;
}
else{
st = top;
top = top->next;
}
return st;
}
  
//peek a node
stackNode<T>* peek(){
return top;
}
  
//print all nodes
void print(){
stackNode<T>* start = top;
  
while(start != NULL){
cout << "Resource: " << start->getResrc() << endl;
cout << "Quantity: " << start->getWeight() << endl;
start = start->next;
}
}
  
//calculate total weight
int tallyWeights(){
if(top == NULL){
return -1;
}
  
int total = 0;
stackNode<T>* start = top;
  
while(start != NULL){
total += start->getWeight();
start = start->next;
}
  
return total;
}
  
//calculate storage requirements
string calculateStorageRequirements(){
if(top == NULL){
return "LOGISTICS ERROR";
}
  
int sumWeight = 0;
int sumItems = 0;
  
stackNode<T>* start = top;
  
while(start != NULL){
sumWeight += start->getWeight();
sumItems++;
start = start->next;
}
  
if(sumWeight < 100)
return "wooden crate";
else if(sumWeight >= 200 && (sumItems > 5 && sumItems < 10))
return "steel crate";
else if(sumItems > 10)
return "silo";
else
return "LOGISTICS ERROR";
}
};

int main(){
   resrcStack<string>* rstk = new resrcStack<string>();
stackNode<string>* node;
  
//creating and pushing a node
node = new stackNode<string>("Unrefined Ores", 12);
rstk->push(node);
cout << "\nInserted a node: " << "Unrefined Ores, 12" << endl;
  
//creating and pushing a node
node = new stackNode<string>("Refined Alloys", 80);
rstk->push(node);
cout << "\nInserted a node: " << "Refined Alloys, 80" << endl;
  
//printing all nodes
cout << endl << "Details of all stack items:" << endl;
rstk->print();
  
//printing total weight of items
cout << "\nTotal weight of items: "
<< rstk->tallyWeights() << endl << endl;

//printing storage requirement of items
cout << "Storage requirement: "
<< rstk->calculateStorageRequirements() << endl << endl;
  
   return 0;
}


Code screenshot:

Output:


Related Solutions

(C++ with main to test) 2.2 Task 1 You are going to implement a variant of...
(C++ with main to test) 2.2 Task 1 You are going to implement a variant of the standard linked list, the circular list. The circular linked list is a variant of the standard linked list that wraps around so that traversals through the list will wrap around to the beginning instead of simply reaching the end. This will consist of implementing two classes: cLL and item. 2.2.1 cLL The class is described according to the simple UML diagram below: 2cLL<T>...
C++ (With main to test) 2.2 Task 1 You are going to implement a variant of...
C++ (With main to test) 2.2 Task 1 You are going to implement a variant of the standard linked list, the doubly linked list. Doubly linked lists are because they enable a backwards and forwards traversal of the list through the addition of more pointers. By increasing the memory cost of the list, it enables a better access since the list does not need to be traversed in only one direction. This will consist of implementing two classes: dLL and...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible. Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When...
Imagine you are an automotive engineer for Toyoto Motor Corporation. You are test driving a new...
Imagine you are an automotive engineer for Toyoto Motor Corporation. You are test driving a new cruise control system, and you are driving around the test track at 55 miles per hour. You set the cruise control to 57miles per hour. The cruise controller takes over and speeds up to 59 miles per hour, then slows to 56 miles per hour, and finally settles at 58 miles per hour. Given this result, what adjustments would you make to the car's...
Galactic Dynamics. Imagine that you are an occupant of a strange, distant galaxy, living on a...
Galactic Dynamics. Imagine that you are an occupant of a strange, distant galaxy, living on a planet in a solar system 10 kpc away from the galaxy's center. Looking along a galactic longitude of I 25°, you determine the orbital velocity of a cloud to be v = all orbits to be circular. 250 km/s. Take (a) You determine that this cloud is at the subcentral point-what is its orbital radius? (b) What is the mass interior to the cloud's...
Task (1) You work as a material engineer at an aircraft development, manufacturing and maintenance company...
Task (1) You work as a material engineer at an aircraft development, manufacturing and maintenance company in the MENA region. Recently, the manufactured airplanes have been experiencing crashes after a couple of years in service. You have been assigned as a member in the investigation team to identify the main cause of the crashes.                             The shape of the designed windows of the manufactured planes is square. The investigation team suggested that this design might be the main cause of...
Imagine you are working for Drexel Morgan Bank, and you are assigned with the task of...
Imagine you are working for Drexel Morgan Bank, and you are assigned with the task of evaluating Earl Grey, Inc. Consider the following financial information of this company. Consider the following information about Earl Grey, Inc. Total assets $250 million Total debt $110 million Preferred stock $ 35 million Common stockholders' equity $105 million Net profits after taxes $25.5 million Number of preferred stock outstanding 1.5 million shares Number of common stock outstanding 9 million shares Preferred dividends paid $2.5...
Imagine you are the orbital engineer for the first NASA spaceshot to Ceres, the largest...
Imagine you are the orbital engineer for the first NASA space shot to Ceres, the largest known asteroid. Ceres’s nearly circular orbit around the sun has a radius of R = 2.77 AU. After being launched from the earth, the probe will initially be in a circular orbit around the sun with the same radius (r_{c} = 1.0 AU) and the same orbital speed (|\vec{v}_{c}| = 6.28 AU/y) as the earth’s orbit. The probe’s rocket engines will then fire briefly...
imagine you are a network engineer who has been hired to design a network for a...
imagine you are a network engineer who has been hired to design a network for a small company consisting of a headquarters office in Indianapolis, Indiana, and a branch office in Minneapolis, Minnesota. The company has hired you to design and build the network infrastructure from the ground up. Following is a brief summary of the business structure: Indianapolis, IN This office is the headquarters for the company. It has 61 employees divided across the following three departments: • Administration:...
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with...
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with arrays. You will have to create your own structure. However, make sure to meet these guidelines: - Give the structure whichever name you want. - It must have at least 3 members. - Two of the members must be arrays. - Your members should be of at least two different data-types. In other words, your members cannot be integers only (or floats, or doubles…)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT