By the end of this century, we will likely have colonies on the moon and on Mars
ID: 3581633 • Letter: B
Question
By the end of this century, we will likely have colonies on the moon and on Mars. In an effort to prepare for this reality, NASA wants to hire you to write a program that will help them manage rocket launches to send supplies to the colonies. This program will use linked lists as queues. There will be one queue for each rocket. The rockets will fly to the International Space Station, the moon, or Mars. The rockets only have 5 cargo bays and the total weight of the cargo at the destination location must not exceed 1000 pounds. The weight at the destinations can be calculated by multiplying the weight times the gravity factor. Destination Gravity Factor ISS 1.0 (due to artificial gravity) Moon 0.1655 Mars 0.3895 The rockets will launch as soon as all 5 cargo bays have cargo. However, only one rocket can be launched per day. If multiple rockets need to be launched, priority will be given to the rocket that has the farthest to travel. You will need a counter to keep track of the days. More discussion of the counter can be found in the Input section. When the rocket launches, print out the necessary data from the first 5 nodes of the queue and delete them one at a time (print node1, delete node1, print node2, delete node2, etc.). There may be situations where the cargo that fills the rocket, puts the rocket over the weight limit. If this happens, the excess cargo will be moved to a new node to begin filling in the next rocket. At the end of the program, if rockets have cargo, launch them (this should happen in the destructor of the linked list). The Classes This program will implement a linked list by using classes o There will be a node class which contains Member variables Arrival Day (integer) Cargo (C-string pointer) Destination (character) Weight (double) Accessors Mutators Overloaded output stream operator Filestream << node – will print multiple elements of the node Overloaded + operator Node + node – will return sum of weights o There will be a linked list class which contains Member variables Head (node pointer) Tail (node pointer) Accessors Mutators Enqueue function – add node to end of linked list Dequeue function – delete node from beginning of linked list Input: All input will be from a file named cargo.txt. The format of each line of the file should be: - day the cargo arrived - detination o S or s – ISS o L or l – Moon (Lunar base) o M or m – Mars - cargo – 1 word description - earth weight If the line does not follow the proper format, it is to be ignored. If the line follows the format, the data should be added to a node and the node added to the appropriate queue. The day counter is critical here. Data should be read from the file when the arrival day matches the counter. After reading in all of the cargo that arrived that day, load it into the rockets and then determine if any of the rockets should be launched. After that, proceed to the next day. The input file will be ordered by day. Don’t assume the day starts at 1. Use the arrival day in the first valid line as the starting point for your counter. Output: All output will be written to a file named launch.txt. The format should be as follows: Day Destination Node 1 o Node format - Node 2 Node 3 Node 4 Node 5 Rocket Weight:<sum of the destination weights>
Explanation / Answer
#include<iostream.h>
#include<string.h>
//node contains the information about the cargo bay
class Node{
double weight ;
char destination;
String cargoName;
int arrivalDay;
Node *prev;
Node *next;
public Node(int arrivalDay, String cargoName,char destination, double weight ) {
super();
this.weight = weight;
this.destination = destination;
this.cargoName = cargoName;
this.arrivalDay = arrivalDay;
this.prev=null;
this.next=null;
}
//extraction operator overloading
istream& operator>>(istream& is ,node& n){
is>>n.arrivalDay>>n.cargoName>>n.destination>>n.weight;
return is;
}
//insertion operator overloading
ostream& operator<<(ostream& os ,node& n){
os<<n.arrivalDay<<n.cargoName<<n.destination<<n.weight;
return os;
}
//operator + overloading
double operator+(node& n){
return (this.weight+n.weight);
}
//getting the previous node
public Node getPrev() {
return prev;
}
//setting the previous node
public void setPrev(Node prev) {
this.prev = prev;
}
//getting the next node
public Node getNext() {
return next;
}
//setting the previous node
public void setNext(Node next) {
this.next = next;
}
public void computWeight(){
if(destination=='s') weight=1.0*weight;
else if(destination=='l') weight=0.1655 *weight;
else if(destination=='m') weight=0.3895 *weight;
}
class LinkedList {
node *head,*tail;
public int counter;
linkedList(){
head=null;
tail=null;
counter=0;
}
//inserting cargo at end
void insert(node n)
{
node *temp;
if (tail==null)
{
cout<<"Dequeue is Overflow"<<endl;
return;
}
if(counter==0)count=n.arrivalDay;
//checking the cargoweight
if(!checkWeight(n.getWeight()){
if (tail==null) {
tail= n;
tail->next = NULL;
tail->prev = NULL;
head=tail;
cout<<"node inserted into empty linked list"<<endl;
count++;
}
else {
temp = n;
temp->next = head;
temp->prev = tail;
tail->next = temp;
tail= temp;
count++;
}
}
//deleting cargo at start
public Node remove()
{
node temp=head;
if (head==null){
cout<<"Deque Underflow"<<endl;
return temp;
}
display();
head = head->next;
head->prev = NULL;
counter--;
return temp;
}
//displaying linked list in farward direction
void display()
{
node *temp;
if (head== null)
{
cout<<"linked list is Underflow"<<endl;
return;
}
temp = head;
cout<<"Linked List from Beginning:"<<endl;
while (temp != NULL)
{
cout<<temp.toString()<<" ";
temp = temp->next;
}
}
}
public void main(String args[])
{
int i=0;
LinkedList l;
Node n[5];
double tWeight=0;
l=new LinkedList();
//reading the cargo values from input file
ifstream ifs;
ifs.open("c:\input.txt");
while(!ifs ){
i=0;
while(i<5 && ifs>>n[i].arrivalDay==counter){
ifs>>n[i].cargoName>>n[i].destination>>n[i].weight;
n[i].computeweight();
l.insert(n[i]);
i++;
}
}
ifs.close();
//writing the rocket information in launch file.
ifs.open("c:\launch.txt");
cout<<"Rocket Launching with the following cargos";
for(i=0;i<5;i++){
n[i]=l.remove();
//checking the weights of the cargo.
if(n[i]<1000)
tweight=tweight+n[i].weight;
else
l.insert(n[i]);
}
ifs<<"Arrival Day: "<<n[0].arrivalDay;
for(i=0;i<5;i++){
ifs<<n[i]<<endl;
}
ifs<<"Rocket Weight:"<<tweight;
ifs.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.