Question

In: Computer Science

in C++ Using your previous homework code: 1) Add a default constructor for the runners. (...

in C++

Using your previous homework code:

1) Add a default constructor for the runners. ( one that has no parameters)

2) Add a constructor that takes a name and a bib number

3) Add a constructor that takes a name , a bib number and a sex.

Add setter functions that sets the time:

1) one that takes a string of the format : hh:mm:ss

2) one that takes an int that is the number of seconds the runner took to complete the race.

Please Note: You decide what format inside the class to store the time amount.  

Add getter functions that return the time:

1) one that returns a string in the format : hh:mm:ss

2) one that returns an int that this the time in seconds.

Add a comment section to your class and explain why you chose some things.

1) What data type did you decide on in the class for the time, why

2) What was an alternative? Is your decision a big decision or a small decision? why

Please note : Again pick one C++ data type inside the class to store the time.

Do not use two variables to store the time.

Please note: if you write conversion functions that if expected. Do not make them public.  

Please note: on the time returned as a string please test and verify that times like these contain the correct zeros not blanks. the format will always be either: ( b = blank, hh = hours, mm = minutes, ss = seconds )

bh:mm:ss

hh:mm:ss

so a time like 1:03:02 will NOT be displayed as : 1:3:2. I strongly suggest testing in these areas.

You may want to convert from a string to an integer for you students here is some

sample code

String conversion using stoi() or atoi()
stoi() : The stoi() function takes a string as an argument and returns its value. Following is a simple implementation:

filter_none

edit

play_arrow

brightness_4

// C++ program to demonstrate working of stoi()

// Work only if compiler supports C++11 or above.

#include <iostream>

#include <string>

using namespace std;

  

int main()

{

    string str1 = "45";

    string str2 = "3.14159";

    string str3 = "31337 geek";

  

    int myint1 = stoi(str1);

    int myint2 = stoi(str2);

    int myint3 = stoi(str3);

  

    cout << "stoi(\"" << str1 << "\") is "

         << myint1 << '\n';

    cout << "stoi(\"" << str2 << "\") is "

         << myint2 << '\n';

    cout << "stoi(\"" << str3 << "\") is "

         << myint3 << '\n';

  

    return 0;

}

Output:

stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 geek") is 31337 



Please Note:  
You may decide that you need a function that returns a 2 character long string 
given an integer.   This is starter code .   
It does not fully help you in this project you will need to modify or add 
a bit of stuff to get it to be truly helpful. the statement 
temp+=number%10+48;
could be coded as :
temp+=number%10+'0';

Here is some starter code:

string convertInt(int number)
{
    if (number == 0)
        return "0";
    string temp="";
    string returnvalue="";
    while (number>0)
    {
        temp+=number%10+48;
        number/=10;
    }
    for (int i=0;i<temp.length();i++)
        returnvalue+=temp[temp.length()-i-1];
    return returnvalue;
}

this is my code from my previous homework:

#include<iostream>

#include<string>

using namespace std;

class Runner

{

private:

string bibnumber;

char gender;

int age;

string name;

string time;

public:

Runner()

{

bibnumber=" ";

gender=' ';

age=0;

name=" ";

time=" ";

}

int getAge()

{

return age;

}

string getName()

{

return name;

}

string getTime()

{

return time;

}

string getBin()

{

return bibnumber;

}

char getGender()

{

return gender;

}

void setCombinedGenderBib ( string bib)

{

gender=bib[0];

bibnumber=bib.substr(1,bib.length());

}

void setName(string name)

{

this->name=name;

}

void setTime(string time)

{

this->time= time;

}

void setAge(int age)

{

this->age= age;

}

};

int main()

{

Runner runner;

runner.setName("john");

runner.setAge(23);

runner.setTime("01:04:37");

runner.setCombinedGenderBib("M4321");

cout<<"Name : "<<runner.getName()<<endl;

cout<<"Bin Number : "<<runner.getBin()<<endl;

cout<<"Age : "<<runner.getAge()<<endl;

cout<<"Gender : "<<runner.getGender()<<endl;

cout<<"Time : "<<runner.getTime()<<endl;

return0;

Solutions

Expert Solution

Hi , following implementation meets all the above requirements. Hope you find it intresting...

#include<iostream>
#include<string>
#include <string.h>

using namespace std;

class Runner{
private:
string bibnumber;
char gender;
int age;
string name;
string time;
public:
//1) Add a default constructor for the runners. ( one that has no parameters)
Runner(){
bibnumber=" ";
gender=' ';
age=0;
name=" ";
time=" ";
}
//2) Add a constructor that takes a name and a bib number
Runner(string name,string bibnumber){
this->name=name;
this->bibnumber=bibnumber;
gender=' ';
age=0;
time=" ";
}
//3) Add a constructor that takes a name , a bib number and a sex.
Runner(string name,string bibnumber,char gender){
this->name=name;
this->bibnumber=bibnumber;
this->gender=gender;
age=0;
time=" ";
}


//1) one that takes a string of the format : hh:mm:ss
void setTime(string time){
this->time=time;
int tempTime=getSecondsTim();
setTime(tempTime);
}


#define HOUR 3600
#define MIN 60
//2) one that takes an int that is the number of seconds the runner took to complete the race.
// right......................
void setTime(int numOfSeconds){
int hour=numOfSeconds/HOUR;
int second=numOfSeconds % HOUR;
int minute=second/MIN;
second %= MIN;
char s[50];
//( b = blank, hh = hours, mm = minutes, ss = seconds )
sprintf(s,"%.1d:%.2d:%.2d",hour,minute,second);
this->time=s;
}

int getAge(){
return age;
}

string getName(){
return name;
}

//1) one that returns a string in the format : hh:mm:ss
//Note:time is always stored in proper format by setters...
string getTime(){
return time;
}

//2) one that returns an int that this the time in seconds.
// right................
int getSecondsTim(){

int n = time.length();

// declaring character array
char char_array[n + 1];

// copying the contents of the
// string to char array
strcpy(char_array, time.c_str());
char *token=strtok(char_array,":");
int hh,mm,ss;
int i=0;
while(token!=NULL){
if(i==0){
hh=stoi(token);
// printf("---------");
}
if(i==1){
mm=stoi(token);
// printf("---------");
}
if(i==2){
ss=stoi(token);
// printf("---------");
}
i++;
token=strtok(NULL,":");
}
return hh*3600 + mm*60 + ss;
}


string getBin(){
return bibnumber;
}

char getGender(){
return gender;
}

void setCombinedGenderBib( string bib){
gender=bib[0];
bibnumber=bib.substr(1,bib.length());
}

void setName(string name){
this->name=name;
}

void setAge(int age){
this->age= age;
}
};

int main(){

Runner runner;
runner.setName("john");
runner.setAge(23);
runner.setTime(590);
//runner.setTime("01:04:37");
runner.setCombinedGenderBib("M4321");

cout<<"Name : "<<runner.getName()<<endl;
cout<<"Bin Number : "<<runner.getBin()<<endl;
cout<<"Age : "<<runner.getAge()<<endl;
cout<<"Gender : "<<runner.getGender()<<endl;
cout<<"Time : "<<runner.getTime()<<endl;

return 0;
}


Related Solutions

The code must be under c++ program. For your Double and Integer classes add a default...
The code must be under c++ program. For your Double and Integer classes add a default constructor that sets the value to 0.0 and 0 respectively. Then add the following overloaded constructors Double class A Double argument A primitive double An Integer class Integer class An Integer class A primitive int Each of these constructors should set the data section of the class to the value being passed to it. In addition to the overloaded constructors add the following overloaded...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template <typename ItemType> ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list } // end default constructor template <typename ItemType> int ArrayBag<ItemType>::getCurrentSize() const { return item_count_; } template <typename ItemType> bool ArrayBag<ItemType>::isEmpty() const { return item_count_ == 0; } template <typename ItemType> bool ArrayBag<ItemType>::add(const ItemType &new_entry) {    bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY); if (has_room_to_add) { items_[item_count_] = new_entry; item_count_++; } // end if return has_room_to_add;...
code in c++ using the code given add a hexadecimal to binary converter and add a...
code in c++ using the code given add a hexadecimal to binary converter and add a binary to hexadecimal converter #include <iostream> #include <string> #include<cmath> #include<string> using namespace std; int main() { string again; do { int userChoice; cout << "Press 2 for Decimal to Binary"<< endl; cout << "Press 1 for Binary to Decimal: "; cin >> userChoice; if (userChoice == 1) { long n; cout << "enter binary number" << endl; cin>>n; int decnum=0, i=0, remainder; while(n!=0) {...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2....
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2. Add code to the generateRandomArray method which will fill testArray with random integers between 1 and 10 3. Add code to the printArray method which will print each value in testArray. 4. Write an accessor method, getArray which will return the testArray Here's the code starter code: import java.util.ArrayList; public class TestArrays { private int[] testArray; public TestArrays() { } public void generateRandomArray() {...
in c++ a Circle should be created with the default constructor, then its area, diameter, and...
in c++ a Circle should be created with the default constructor, then its area, diameter, and circumference printed to the screen for verification that it has the correct state and those respective methods work correctly. Then, output of the state should be repeated after calling each mutator to ensure that the object’s new state reflects the change Create a Circle class with the class implementation in a file named circle.cpp and the class declaration in a file named circle.h (or...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c public void setA(double newA) public void setB(double newB) public void setC(double newC) (6) public static QuadraticExpression scale( double r, QuadraticExpression q) returns...
1. You will need your ticker code (show your ticker code on your homework and include...
1. You will need your ticker code (show your ticker code on your homework and include a printout of the data!) for stock prices for this question. Use your ticker code to obtain the closing prices for the following three time periods to obtain three data sets: March 2, 2019 to March 16, 2019 Data set A March 17, 2019 to March 31, 2019 Data set B April 1, 2019 to April 17, 2019 Data set C To ensure equal...
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT