Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS REQUIRES A CLASS THAT MAKES USE OF BOTH PUBLIC AND PRIVATE DATA MEMBER IN C

ID: 3704859 • Letter: T

Question

THIS REQUIRES A CLASS THAT MAKES USE OF BOTH PUBLIC AND PRIVATE DATA MEMBER IN C++

1) Create a class named Country with a default constructor, a public methods ReadFile, getMaxAreaState and a private data members-

stateName, which is an array with 50 string values

area, which is an array of 50 integer values.

Use -1.0 to initialize all the area values of the array and empty string for the stateName values initialization.

2) The public method ReadFile will read each line from the given filename and store each value into the arrays. Each line of the file contains a stateName and its area separated by a comma. This method does not return a value.

Please note, each line in the file contains a stateName and its area but not necessarily in order, you need to determine if the read value is a stateName or area.

Note: state names will be in lower case only.

Hint: stateName will consist of only alphabets and area only of digits.

e.g. Data in File can be in following order:

3) The public method getMaxAreaState returns the name of the state with maximum area.

You only need to write the class definition and any code that is required for that class. Place all code within the class definition. Our code will create and test your class.

Explanation / Answer

Hi Dear,

Please find my implementtion of required class.

I have not used the split function as the parameters are not clear as what each parameter does.

#include<iostream>

#include<string.h>

#include<fstream>

#include<sstream>

using namespace std;

class Country

{

public:

Country(){

for(int i =0;i<50;i++)

{

stateName[i] = "";

area[i] = -1.0;

}

}

void ReadFile()

{

// user input filename

string fileName;

cout<<"Enter the filename: ";

getline(cin,fileName);

ifstream myfile(fileName.c_str());

if(myfile.is_open())

{

string line;

int i=0,j=0;

while(getline(myfile,line))

{

int pos = line.find(",");

string first= line.substr(0,pos);

string second =line.substr(pos+1,strlen(line.c_str())-1);

if(first.find_first_not_of( "0123456789" ) == string::npos)

{

stateName[i++] = second;

stringstream ss1(first);

int x =0;

ss1>>x;

area[j++]= x;

}

else

{

stateName[i++] = first;

stringstream ss(second);

int x =0;

ss>>x;

area[j++]= x;   

}  

}

}

else

{

cout<<"Can't open file";

}

}

string getMaxAreaState()

{

int maxarea=0;

int position;

for(int i=0;i<((sizeof(area))/sizeof(area[0]));i++)

{

if(area[i]>maxarea)

{

maxarea = area[i];

position = i;

}

}

return stateName[position];

}

private:

string stateName[50];

int area[50];

};