Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

Please help me design a Passsenger class in C++. THANK YOU!

Develop a high-quality, object-oriented C++ program that performs a simulation using a heap implementation of a priority queue.

simulation creates a model of a real-world situation, allowing us to introduce a variety of conditions and observe their effects. For instance, a flight simulator challenges a pilot to respond to varying conditions and measures how well the pilot responds. Simulation is frequently used to measure current business practices, such as the number of checkout lines in a grocery store or the number of tellers in a bank, so that management can determine the fewest number of employees required to meet customer needs.

Airlines have been experimenting with different boarding procedures to shorten the entire boarding time, keep the flights on-time, reduce aisle congestion, and make the experience more pleasant for passengers and crew. A late-departing flight can cause a domino effect:

  • the departure gate is tied up and cannot be used by other landing or departing flights,
  • passengers on board the late flight may miss connecting flights and require rebooking and possibly overnight arrangements (meals and lodging), etc., and
  • passengers complain about being late, and/or about having to rearrange their plans.

Thus, late flights have a huge operational impact. 

For this assignment, we will simulate boarding procedures for Airworthy Airlines.  The Airline's current procedure is as follows: 

  • pre-board in the following order:
    • families with young children or people who need help (e.g., wheelchair)
    • first class and/or business class passengers
    • elite passengers (frequent fliers) and those passengers seated in exit rows
  • conduct general boarding in reverse, from the back of the plane to the front in the following order:
    • rows 23-26
    • rows 17-22
    • rows 11-16
    • rows 5-10

Airworthy is considering revising their boarding procedure such that general boarding is done randomly, meaning the first passenger in line for general boarding is the first passenger to board (i.e., general boarding passengers all have the same priority). Airworthy suspects this random general boarding method will improve the flow of passengers, getting them on board and seated more quickly. It is also less labor-intensive for Airworthy's customer service agents because it significantly reduces the number of boarding announcements required and eliminates confrontations with customers trying to board "out of turn."  Note that the revision is to general boarding only.  The pre-boarding procedures will not be changed.

Develop a Passenger class that stores the following data for an Airworthy Airlines passenger:

  • key - the priority value for the PriorityQueue
  • passenger's last name
  • passenger type, a character, where 'H' is a child or passenger who needs help in boarding, 'E' is an elite passenger (frequent flyer), and 'G' is a general boarding passenger
  • row where passenger is seated; must be a number between 1 and 26, where rows 1-4 are first class and rows 10 and 11 are exit rows

Include a constructor and accessors and mutators for each attribute. In addition, you MUST include methods that overload the < and > operators. You may include other methods, if needed. 

HeapException.cpp:

#include "HeapException.h"

HeapException::HeapException(const string& msg)
    : logic_error("Heap Exception: " + msg) {
}

HeapException.h:

#ifndef HEAPEXCEPTION_H
#define HEAPEXCEPTION_H

#include <stdexcept>
#include <string>
using namespace std;

class HeapException : public logic_error {
public:
    HeapException(const string& msg = "");
};

#endif /* HEAPEXCEPTION_H */

HeapADT:

#ifndef HEAPADT_H
#define HEAPADT_H

template <class T>
class HeapADT {
    public:
        virtual bool isEmpty() const = 0;
        virtual int getItemCount() const = 0;
        virtual T peek() const = 0;
        virtual bool add(const T& newItem) = 0;
        virtual bool remove() = 0;
        virtual void clear() = 0;
};

#endif /* HEAPADT_H */

ArrayMaxHeap.h:

#ifndef ARRAYMAXHEAP_H
#define ARRAYMAXHEAP_H

#include "HeapADT.h"

template <class T>
class ArrayMaxHeap : public HeapADT<T> {
public:
    ArrayMaxHeap();
    virtual ~ArrayMaxHeap();
    
    //interface methods
    bool isEmpty() const;
    int getItemCount() const;
    T peek() const;
    bool add(const T& newItem);
    bool remove();
    void clear();
    
private:
    static const int ROOT_INDEX = 0;
    static const int CAPACITY = 5000;
    int itemCount;
    T* items;
    
    int getLeftChild(int currNode) const;
    int getRightChild(int currNode) const;
    int getParent(int currNode) const;   
    bool isLeaf(int currNode) const;
    void percolateDown(int subtree);
};
#include "ArrayMaxHeap.cpp"
#endif /* ARRAYMAXHEAP_H */

 

 

 

```cpp
template <class T>
bool ArrayMaxHeap<T>::remove() {
    if (isEmpty()) {
        throw HeapException("Trying to remove from an empty heap.");
    }
    items[ROOT_INDEX] = items[itemCount - 1];
    itemCount--;
    percolateDown(ROOT_INDEX);
}

// Test if heap is empty
// @return true if empty
template <class T>
bool ArrayMaxHeap<T>::isEmpty() const {
    return (itemCount == 0);
}

// recursive method
template <class T>
void ArrayMaxHeap<T>::percolateDown(int subtree) {
    if (isLeaf(subtree)) { // base case
        return;
    }
    int leftChild = getLeftChild(subtree);
    int rightChild = leftChild + 1;

    int maxChild = rightChild;
    if (rightChild >= itemCount) {
        maxChild = leftChild;
    } else {
        // single child exists
        if (items[leftChild] > items[rightChild]) {
            maxChild = leftChild;
        }
    }

    // maxChild has the index of the largest value of 2 children
    if (items[subtree] < items[maxChild]) {
        swap(items[subtree], items[maxChild]);
        percolateDown(maxChild);
    }
}

// private methods
template <class T>
int ArrayMaxHeap<T>::getLeftChild(int curNode) const {
    return 2 * curNode + 1;
}

template <class T>
int ArrayMaxHeap<T>::getRightChild(int curNode) const {
    return 2 * curNode + 2;
}

template <class T>
int ArrayMaxHeap<T>::getParent(int curNode) const {
    return (curNode - 1) / 2;
}

template <class T>
bool ArrayMaxHeap<T>::isLeaf(int curNode) const {
    return (getLeftChild(curNode) >= itemCount);
}
```

### Explanation

This code defines an `ArrayMaxHeap` class with several methods to manage a heap data structure, often used in implementing priority queues. The following are key components:

1. **Remove Method**: 
   - Checks if the heap is empty before attempting to remove the root element.
   - Throws an exception if the heap is empty.
   - Replaces the root with the last element and decreases the item count.
   -
expand button
Transcribed Image Text:```cpp template <class T> bool ArrayMaxHeap<T>::remove() { if (isEmpty()) { throw HeapException("Trying to remove from an empty heap."); } items[ROOT_INDEX] = items[itemCount - 1]; itemCount--; percolateDown(ROOT_INDEX); } // Test if heap is empty // @return true if empty template <class T> bool ArrayMaxHeap<T>::isEmpty() const { return (itemCount == 0); } // recursive method template <class T> void ArrayMaxHeap<T>::percolateDown(int subtree) { if (isLeaf(subtree)) { // base case return; } int leftChild = getLeftChild(subtree); int rightChild = leftChild + 1; int maxChild = rightChild; if (rightChild >= itemCount) { maxChild = leftChild; } else { // single child exists if (items[leftChild] > items[rightChild]) { maxChild = leftChild; } } // maxChild has the index of the largest value of 2 children if (items[subtree] < items[maxChild]) { swap(items[subtree], items[maxChild]); percolateDown(maxChild); } } // private methods template <class T> int ArrayMaxHeap<T>::getLeftChild(int curNode) const { return 2 * curNode + 1; } template <class T> int ArrayMaxHeap<T>::getRightChild(int curNode) const { return 2 * curNode + 2; } template <class T> int ArrayMaxHeap<T>::getParent(int curNode) const { return (curNode - 1) / 2; } template <class T> bool ArrayMaxHeap<T>::isLeaf(int curNode) const { return (getLeftChild(curNode) >= itemCount); } ``` ### Explanation This code defines an `ArrayMaxHeap` class with several methods to manage a heap data structure, often used in implementing priority queues. The following are key components: 1. **Remove Method**: - Checks if the heap is empty before attempting to remove the root element. - Throws an exception if the heap is empty. - Replaces the root with the last element and decreases the item count. -
```cpp
#include "ArrayMaxHeap.h"
#include "HeapException.h"

template <class T>
ArrayMaxHeap<T>::ArrayMaxHeap() : itemCount(0) {
    items = new T[CAPACITY];
}

template <class T>
ArrayMaxHeap<T>::~ArrayMaxHeap() {
    delete[] items;
}

// Interface methods

/**
 * Add an item to the max heap
 * @param newItem Item to be added
 * @return true if successful
 */
template <class T>
bool ArrayMaxHeap<T>::add(T newItem) {
    if (itemCount == CAPACITY) {
        throw HeapException("Trying to add to a full heap.");
    }

    // Add to bottom; "upheap" if needed
    int newNode = itemCount;
    int parent = (newNode - 1) / 2;
    bool inPlace = false;

    // Percolate new node up to proper place
    while (newNode > 0 && !inPlace) {
        if (items[newNode] > items[parent]) {
            // Swap elements
            std::swap(items[newNode], items[parent]);
            newNode = parent;
            parent = (newNode - 1) / 2;
        } else {
            inPlace = true;
        }
    }

    items[newNode] = newItem;
    itemCount++;
    return true;
}

/**
 * Removes all items from the heap
 */
template <class T>
void ArrayMaxHeap<T>::clear() {
    itemCount = 0;
}

/**
 * Returns the number of items in the heap
 * @return the number of items in the heap
 */
template <class T>
int ArrayMaxHeap<T>::getItemCount() const {
    return itemCount;
}
```

This code defines an `ArrayMaxHeap` class in C++ with methods and comments explaining each part of the implementation. It includes functions to add an item to the heap, clear the heap, and get the number of items in the heap. The code uses generic programming with templates, allowing it to handle different data types. The `add` function employs a technique called "upheap" to maintain the heap property by percolating a new item to its correct position.
expand button
Transcribed Image Text:```cpp #include "ArrayMaxHeap.h" #include "HeapException.h" template <class T> ArrayMaxHeap<T>::ArrayMaxHeap() : itemCount(0) { items = new T[CAPACITY]; } template <class T> ArrayMaxHeap<T>::~ArrayMaxHeap() { delete[] items; } // Interface methods /** * Add an item to the max heap * @param newItem Item to be added * @return true if successful */ template <class T> bool ArrayMaxHeap<T>::add(T newItem) { if (itemCount == CAPACITY) { throw HeapException("Trying to add to a full heap."); } // Add to bottom; "upheap" if needed int newNode = itemCount; int parent = (newNode - 1) / 2; bool inPlace = false; // Percolate new node up to proper place while (newNode > 0 && !inPlace) { if (items[newNode] > items[parent]) { // Swap elements std::swap(items[newNode], items[parent]); newNode = parent; parent = (newNode - 1) / 2; } else { inPlace = true; } } items[newNode] = newItem; itemCount++; return true; } /** * Removes all items from the heap */ template <class T> void ArrayMaxHeap<T>::clear() { itemCount = 0; } /** * Returns the number of items in the heap * @return the number of items in the heap */ template <class T> int ArrayMaxHeap<T>::getItemCount() const { return itemCount; } ``` This code defines an `ArrayMaxHeap` class in C++ with methods and comments explaining each part of the implementation. It includes functions to add an item to the heap, clear the heap, and get the number of items in the heap. The code uses generic programming with templates, allowing it to handle different data types. The `add` function employs a technique called "upheap" to maintain the heap property by percolating a new item to its correct position.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY