Question

In: Computer Science

C++ Zybook Lab 15.4 Zip code and population (class templates) Complete the TODOs in StatePair.h and...

C++ Zybook Lab

15.4 Zip code and population (class templates)

Complete the TODOs in StatePair.h and main.cpp.

StatePair.h

Define a class StatePair with two template types (T1 and T2), and constructor, mutators, accessors, and PrintInfo() member functions which will work correctly with main.cpp. Note, the three vectors (shown below) have been pre-filled with StatePair data in main() and do not require modification.

  • vector<StatePair <int, string>> zipCodeState: ZIP code - state abbreviation pairs
  • vector<StatePair<string, string>> abbrevState: state abbreviation - state name pairs
  • vector<StatePair<string, int>> statePopulation: state name - population pairs

main.cpp

Complete main() to:

  • Use an input ZIP code to retrieve the correct state abbreviation from the vector zipCodeState.
  • Then, use the state abbreviation to retrieve the state name from the vector abbrevState.
  • Then, use the state name to retrieve the correct state name/population pair from the vector statePopulation and,
  • Finally, output the pair.

For FULL CREDIT on this assignment, the member functions for StatePair MUST be written outside the class StatePair { ... }; context as indicated by the TODO comments.

Ex: If the input is: 21044 the output is: Maryland: 6079602

Here's the code:

//StatePair.h

#ifndef STATEPAIR
#define STATEPAIR

template<typename T1, typename T2>
class StatePair {

// TODO: Define signatures ONLY for a constructor, mutators,
// and accessors for StatePair which work with main.cpp
// as written
  
// TODO: Define a signature for the PrintInfo() method

// TODO: Define data member(s) for StatePair
};

// TODO: Complete the implementations of the StatePair methods

#endif

//main.cpp

#include<iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;

int main() {
   ifstream inFS; // File input stream
   int zip;
   int population;
   string abbrev;
   string state;
   unsigned int i;

   // ZIP code - state abbrev. pairs
   vector<StatePair <int, string>> zipCodeState;

   // state abbrev. - state name pairs
   vector<StatePair<string, string>> abbrevState;

   // state name - population pairs
   vector<StatePair<string, int>> statePopulation;

   // Fill the three ArrayLists

   // Try to open zip_code_state.txt file
   inFS.open("zip_code_state.txt");
   if (!inFS.is_open()) {
       cout << "Could not open file zip_code_state.txt." << endl;
       return 1; // 1 indicates error
   }
   while (!inFS.eof()) {
       StatePair <int, string> temp;
       inFS >> zip;
       if (!inFS.fail()) {
           temp.SetKey(zip);
       }
       inFS >> abbrev;
       if (!inFS.fail()) {
           temp.SetValue(abbrev);
       }
       zipCodeState.push_back(temp);
   }
   inFS.close();
  
// Try to open abbreviation_state.txt file
   inFS.open("abbreviation_state.txt");
   if (!inFS.is_open()) {
       cout << "Could not open file abbreviation_state.txt." << endl;
       return 1; // 1 indicates error
   }
   while (!inFS.eof()) {
       StatePair <string, string> temp;
       inFS >> abbrev;
       if (!inFS.fail()) {
           temp.SetKey(abbrev);
       }
       getline(inFS, state); //flushes endline
       getline(inFS, state);
       state = state.substr(0, state.size()-1);
       if (!inFS.fail()) {
           temp.SetValue(state);
       }
      
       abbrevState.push_back(temp);
   }
   inFS.close();
  
   // Try to open state_population.txt file
   inFS.open("state_population.txt");
   if (!inFS.is_open()) {
       cout << "Could not open file state_population.txt." << endl;
       return 1; // 1 indicates error
   }
   while (!inFS.eof()) {
       StatePair <string, int> temp;
       getline(inFS, state);
       state = state.substr(0, state.size()-1);
       if (!inFS.fail()) {
           temp.SetKey(state);
       }
       inFS >> population;
       if (!inFS.fail()) {
           temp.SetValue(population);
       }
       getline(inFS, state); //flushes endline
       statePopulation.push_back(temp);
   }
   inFS.close();
  
   cin >> zip;

for (i = 0; i < zipCodeState.size(); ++i) {
       // TODO: Using ZIP code, find state abbreviation
       if(zipCodeState.at(i).SetValue() == zip)
{
abbrev = zipCodeState.at(i).SetValue();
break;
}
   }


   for (i = 0; i < abbrevState.size(); ++i) {
       // TODO: Using state abbreviation, find state name
       if(abbrevState.at(i).SetValue() == abbrev){
state = abbrevState.at(i).SetValue();
break;
}
   }


   for (i = 0; i < statePopulation.size(); ++i) {
       // TODO: Using state name, find population. Print pair info.
       if(statePopulation.at(i).SetValue() == state){
statePopulation.at(i).PrintInfo();
break;
}
   }

}

Solutions

Expert Solution

// StatePair.h

#ifndef STATEPAIR
#define STATEPAIR

#include <iostream>
using namespace std;

template<typename T1, typename T2>
class StatePair {

private:
T1 key;
T2 value;

public:

// Define a constructor, mutators, and accessors
// for StatePair
StatePair() //default constructor
{}

// set the key
void SetKey(T1 key)
{
this->key = key;
}

// set the value
void SetValue(T2 value)
{
this->value = value;
}

// return key
T1 GetKey() { return key;}
  
   // return value
T2 GetValue() { return value;}

// Define PrintInfo() method
// display key and value in the format key : value
void PrintInfo()
{
cout<<key<<" : "<<value<<endl;
}
};

#endif

//end of StatePair.h

// main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;

int main()
{
ifstream inFS; // File input stream
int zip;
int population;
string abbrev;
string state;
unsigned int i;

// ZIP code - state abbrev. pairs
vector<StatePair <int, string>> zipCodeState;

// state abbrev. - state name pairs
vector<StatePair<string, string>> abbrevState;

// state name - population pairs
vector<StatePair<string, int>> statePopulation;

// Fill the three vectors

// Try to open zip_code_state.txt file
inFS.open("zip_code_state.txt");

if (!inFS.is_open()) {
       cout << "Could not open file zip_code_state.txt." << endl;
       return 1; // 1 indicates error
   }
  
   while (!inFS.eof()) {
StatePair <int, string> temp;
inFS >> zip;

       if (!inFS.fail()) {
temp.SetKey(zip);
       }
      
       inFS >> abbrev;
       if (!inFS.fail()) {
           temp.SetValue(abbrev);
       }
       zipCodeState.push_back(temp);
   }
   inFS.close();

   // Try to open abbreviation_state.txt file
   inFS.open("abbreviation_state.txt");
   if (!inFS.is_open()) {
       cout << "Could not open file abbreviation_state.txt." << endl;
       return 1; // 1 indicates error
   }

   while (!inFS.eof()) {
       StatePair <string, string> temp;
       inFS >> abbrev;
       if (!inFS.fail()) {
           temp.SetKey(abbrev);
       }
      
       getline(inFS, state); //flushes endline
       getline(inFS, state);
       state = state.substr(0, state.size()-1);
       if (!inFS.fail()) {
           temp.SetValue(state);
       }
  
       abbrevState.push_back(temp);
   }
   inFS.close();

// Try to open state_population.txt file
   inFS.open("state_population.txt");
   if (!inFS.is_open()) {
       cout << "Could not open file state_population.txt." << endl;
       return 1; // 1 indicates error
   }
  
   while (!inFS.eof()) {
       StatePair <string, int> temp;
       getline(inFS, state);
       state = state.substr(0, state.size()-1);
       if (!inFS.fail()) {
           temp.SetKey(state);
       }
       inFS >> population;
       if (!inFS.fail()) {
           temp.SetValue(population);
       }
       getline(inFS, state); //flushes endline
       statePopulation.push_back(temp);
   }

   inFS.close();

cin >> zip;

for (i = 0; i < zipCodeState.size(); ++i) {
// Using ZIP code, find state abbreviation
       if(zipCodeState.at(i).GetKey() == zip)
       {
abbrev = zipCodeState.at(i).GetValue();
break;
}
}


for (i = 0; i < abbrevState.size(); ++i) {
// Using state abbreviation, find state name
if(abbrevState.at(i).GetKey() == abbrev)
{
state = abbrevState.at(i).GetValue();
break;
}
}


for (i = 0; i < statePopulation.size(); ++i) {
// Using state name, find population. Print pair info.
if(statePopulation.at(i).GetKey() == state)
{
statePopulation.at(i).PrintInfo();
break;
}
}

return 0;
}

//end of main.cpp


Related Solutions

CSI 1440 Lab 6 “Class Templates” Templates Templates in C++ is not a difficult idea. This...
CSI 1440 Lab 6 “Class Templates” Templates Templates in C++ is not a difficult idea. This lab is not intended to help you understand the details of how templates work. It is only intented to give students an opportunity to start developing code using templated classes. Everyone in the class has had to come to grips with the usage of variables in general. With templates, you can think of the type of the variable being a variable itself. The programmer...
In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates....
In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates. Give an example of a class template and a function template.
A zip code is a string of five digits (0,1,2,3,4,5,6,7,8,9). How many zip codes are there...
A zip code is a string of five digits (0,1,2,3,4,5,6,7,8,9). How many zip codes are there subject to the following restrictions? (1) Only even digits are allowed. (2) The first digital is even and the last digit is odd (3) Digits cannot be repeated (4) 0 appears exactly four times (5) 0 appears at at least once Solve the following discrete mathematics problem above. Show all work/explanations
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class...
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument . • setRadius. A mutator function for the radius variable. • getRadius. An acccssor function...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the formula is: team_wins / (team_wins + team_losses) Note: Use floating-point division. Ex: If the input is: Ravens 13 3 where Ravens is the team's name, 13 is the number of team wins, and 3 is the number of team losses, the output is: Congratulations, Team Ravens has a winning average! If the input is Angels 80 82, the output is: Team Angels has a...
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive....
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator−−():void +determineRouteStatistics()=0:void The class variables are as follows: • map: A 2D array of chars, it will represent the...
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive....
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reect that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will represent the map...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT