Question

In: Computer Science

Programming Assignment 1: Representing, Managing and Manipulating Travel Options You may NOT do any of the...

Programming Assignment 1: Representing, Managing and Manipulating Travel Options

You may NOT do any of the following:

  • change any of the function names or signatures (parameter lists and types and return type)

  • introduce any global or static variables

  • use any arrays or vectors inside the TravelOptions class! Not for this assignment!! (You may use them as you see fit in any driver/tester programs you write)

You MAY do any of the following as you see fit:

  • Introduce helper functions. But you should make them private.

Reminders:
Some functions eliminate list entries (e.g., prune_sorted). Don't forget to deallocate the associated nodes (using the delete operator).

Please read the below code and complete the missing parts.

#ifndef _TRVL_OPTNS_H

#define _TRVL_OPTNS_H

#include <iostream>

#include <vector>

#include <utility>

// using namespace std;

class TravelOptions{

public:

enum Relationship { better, worse, equal, incomparable};

private:

struct Node {

double price;

double time;

Node *next;

Node(double _price=0, double _time=0, Node* _next=nullptr){

price = _price; time = _time; next = _next;

}

};

/* TravelOptions private data members */

Node *front; // pointer for first node in linked list (or null if list is empty)

int _size;

public:

// constructors

TravelOptions() {

front = nullptr;

_size=0;

}

~TravelOptions( ) {

clear();

}

   /**

   * func: clear

   * desc: Deletes all Nodes currently in the list

   * status: DONE

   */

void clear(){

Node *p, *pnxt;

   p = front;

   while(p != nullptr) {

   pnxt = p->next;

   delete p;

   p = pnxt;

   }

   _size = 0;

   front = nullptr;

}

   /**

   * func: size

   * desc: returns the number of elements in the list

   * status: DONE

   */

int size( ) const {

   return _size;

}

/**

* func: compare

* desc: compares option A (priceA, timeA) with option B (priceB, timeA) and

* returns result (see enum Relationship above):

*

* There are four possible scenarios:

* - A and B are identical: option A and option B have identical price and time:

* ACTION: return equal

* - A is better than B: option A and B are NOT equal/identical AND

* option A is no more expensive than option B AND

* option A is no slower than option B

* ACTION: return better

* - A is worse than B: option A and B are NOT equal/identical AND

* option A is at least as expensive as option B AND

* option A is no faster than option B

* ACTION: return worse

* NOTE: this means B is better than A

* - A and B are incomparable: everything else: one option is cheaper and

* the other is faster.

* ACTION: return incomparable

*

* COMMENTS: since this is a static function, there is no calling object.

* To call it from a client program, you would do something like this:

*

   TravelOptions::Relationship r;

   double pa, ta, pb, tb;

// some code to set the four price/time variables

r = TravelOptions::compare(pa, ta, pb, tb);

if(r == TravelOptions::better)

std::cout << "looks like option b is useless!" << std::endl;

// etcetera

*

* status: TODO

*/

static Relationship compare(double priceA, double timeA,

double priceB, double timeB) {

return incomparable; // placeholder

}

Solutions

Expert Solution

#ifndef TRVL_OPTNS_H_

#define TRVL_OPTNS_H_

#include <iostream>

#include <vector>

#include <utility>

#include <math.h>

// using namespace std;

class TravelOptions{

public:

enum Relationship { better, worse, equal, incomparable};

private:

struct Node {

double price;

double time;

Node *next;

Node(double _price=0, double _time=0, Node* _next=nullptr){

price = _price; time = _time; next = _next;

}

};

/* TravelOptions private data members */

Node *front; // pointer for first node in linked list (or null if list is empty)

int _size;

public:

// constructors

TravelOptions() {

front = nullptr;

_size=0;

}

~TravelOptions( ) {

clear();

}

/**

* func: clear

* desc: Deletes all Nodes currently in the list

* status: DONE

*/

void clear(){

Node *p, *pnxt;

p = front;

while(p != nullptr) {

pnxt = p->next;

delete p;

p = pnxt;

}

_size = 0;

front = nullptr;

}

/**

* func: size

* desc: returns the number of elements in the list

* status: DONE

*/

int size( ) const {

return _size;

}

/**

* func: compare

* desc: compares option A (priceA, timeA) with option B (priceB, timeA) and

* returns result (see enum Relationship above):

*

* There are four possible scenarios:

* - A and B are identical: option A and option B have identical price and time:

* ACTION: return equal

* - A is better than B: option A and B are NOT equal/identical AND

* option A is no more expensive than option B AND

* option A is no slower than option B

* ACTION: return better

* - A is worse than B: option A and B are NOT equal/identical AND

* option A is at least as expensive as option B AND

* option A is no faster than option B

* ACTION: return worse

* NOTE: this means B is better than A

* - A and B are incomparable: everything else: one option is cheaper and

* the other is faster.

* ACTION: return incomparable

*

* COMMENTS: since this is a static function, there is no calling object.

* To call it from a client program, you would do something like this:

*

   TravelOptions::Relationship r;

   double pa, ta, pb, tb;

// some code to set the four price/time variables

r = TravelOptions::compare(pa, ta, pb, tb);

if(r == TravelOptions::better)

std::cout << "looks like option b is useless!" << std::endl;

// etcetera

*

* status: TODO

*/

static Relationship compare(double priceA, double timeA,

double priceB, double timeB) {

               double priceDiff = priceA - priceB;

               double timeDiff = timeA - timeB ;

               if(priceDiff < 0) // priceA < priceB

               {

                              if(fabs(timeDiff) <= 0.001) //timeA <= timeB

                                             return better;

                              else // timeA > timeB

                                             return incomparable;

               }else if(fabs(priceDiff) <= 0.001 ) //priceA = priceB

               {

                              if(timeDiff < 0) // timeA < timeB

                                             return better;

                              else if(fabs(timeDiff) <= 0.001) // timeA = timeB

                                             return equal;

                              else //timeA > timeB

                                             return worse;

               }else // priceA > priceB

               {

                              if(fabs(timeDiff) <= 0.001) // timeA < timeB

                                             return incomparable;

                              else

                                             return worse;

               }

}

};

#endif /* TRVL_OPTNS_H_ */


Related Solutions

Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
Assignment questions: 1. VALUES A. Which values (if any) do you think have intrinsic or foundational...
Assignment questions: 1. VALUES A. Which values (if any) do you think have intrinsic or foundational worth? (If you think that there are no values with intrinsic or foundational worth, please briefly explain why.) B. Do you think that moral values are objective, subjective, or relative to particular cultures? Or perhaps a combination of the three? 2. LOGIC and CRITICAL THINKING A. In your own words, how would you define “critical thinking”? B. How do you know when a conclusion...
1. Discuss possible ways for manipulating profits under the absorption costing system. 2. Do you find...
1. Discuss possible ways for manipulating profits under the absorption costing system. 2. Do you find it ethically acceptable if management resorts to such techniques?
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value parameters, using Java objects, and graphics. This assignment has 2 parts; therefore you should turn in two Java files.You will be using a special class called DrawingPanel written by the instructor, and classes called Graphics and Color that are part of the Java class libraries.Part 1 of 2 (4 points)Simple Figure, or your own drawingFor the first part of this assignment, turn in a file named...
1. Identify and describe four identity statuses that adolescents may experience. Do you recall experiencing any...
1. Identify and describe four identity statuses that adolescents may experience. Do you recall experiencing any of these identity status during your adolescent years?Which identity status is most closely related to your experience and why? The Four identity foreclosure -is the status of one is commitments are made with out alternatives. diffusion-is the status of one who have not experienced a crisis or made by a commitments. moratorium- is the status of one who are in a crisis but commitiments...
Programming. Write only code for the following. Do not write any comments. You can submit your...
Programming. Write only code for the following. Do not write any comments. You can submit your answer as .java file(s). #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the...
Programming assignment (75 pts): The Lab 1 development assignment was largely an exercise in completing an...
Programming assignment (75 pts): The Lab 1 development assignment was largely an exercise in completing an already started implementation. The Lab 2 development assignment will call on you to implement a program from scratch. It’s an exercise in learning more about Java basics, core Java Classes and Class/ method-level modularity. Implement a ‘runnable’ Class called “NumericAnalyzer”. Here’s the functional behavior that must be implemented. NumericAnalyzer will accept a list of 1 or more numbers as command line arguments. These numeric...
Assignment 1. Linear Programming Case Study Your instructor will assign a linear programming project for this...
Assignment 1. Linear Programming Case Study Your instructor will assign a linear programming project for this assignment according to the following specifications. It will be a problem with at least three (3) constraints and at least two (2) decision variables. The problem will be bounded and feasible. It will also have a single optimum solution (in other words, it won’t have alternate optimal solutions). The problem will also include a component that involves sensitivity analysis and the use of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT