13.5 Focus on Software Engineering: Separating Class Specification from Implementation: 10896

Question

Write the interface (.h file) of a class  GasTank containing:
A data member named  amount of type double.
A data member named  capacity of type double.
A constructor that accepts a parameter of type double.
A function named  addGas that accepts a parameter of type double and returns no value.
A function named  useGas that accepts a parameter of type double and returns no value.
A function named  isEmpty that accepts no parameters and returns a boolean value.
A function named  isFull that accepts no parameters and returns a boolean value.
A function named  getGasLevel that accepts no parameters and returns a double.
A function named  fillUp that accepts no parameters and returns a double.

Solution
class GasTank
{
    private:
        double amount;
        double capacity;

    public:
        GasTank(double);
        void addGas(double);
        void useGas(double);
        bool isEmpty();
        bool isFull();
        double getGasLevel();
        double fillUp();
};

13.5 Focus on Software Engineering: Separating Class Specification from Implementation: 10889

Question

Write the implementation (.cpp file) of the  Averager class of the previous exercise. The full specification of the class is:
An data member named  sum of type integer.
An data member named  count of type integer.
A constructor with no parameters. The constructor initializes the data members  sum and the data member  count to 0.
A function named  getSum that accepts no parameters and returns an integer.  getSum returns the value of  sum .
A function named  add that accepts an integer parameter and returns no value.  add increases the value of  sum by the value of the parameter, and increments the value of  count by one.
A function named  getCount that accepts no parameters and returns an integer.  getCount returns the value of the  count data member, that is, the number of values added to  sum .
A function named  getAverage that accepts no parameters and returns a double.  getAverage returns the average of the values added to  sum . The value returned should be a value of type double (and therefore you must cast the data members to double prior to performing the division).

Solution
Averager::Averager()
{
    sum=0;
    count=0;
}

int Averager::getSum()
{
    return sum;
}

void Averager::add(int theNum)
{
    sum+=theNum;
    count++;
}

int Averager::getCount()
{
    return count;
}

double Averager::getAverage()
{
    return (double) sum / (double) count;
}

13.5 Focus on Software Engineering: Separating Class Specification from Implementation: 10885

Question

Write the implementation (.cpp file) of the  Accumulator class of the previous exercise. The full specification of the class is:
An data member named  sum of type integer.
A constructor that accepts no parameters. The constructor initializes the data member  sum to 0.
A function named  getSum that accepts no parameters and returns an integer.  getSum returns the value of  sum .
A function named  add that accepts an integer parameter and returns no value.  add increases the value of  sum by the value of the parameter.

Solution
Accumulator::Accumulator()
{
    sum=0;
}

int Accumulator::getSum()
{
    return sum;
}

void Accumulator::add(int theNum)
{
    sum+=theNum;
}

13.5 Focus on Software Engineering: Separating Class Specification from Implementation: 10877

Question

Write the implementation (.cpp file) of the  Counter class of the previous exercise. The full specification of the class is:
A data member  counter of type  int .
An data member named  limit of type  int .
A static  int data member named  nCounters which is initialized to  0 .
A constructor that takes two  int arguments and assigns the first one to  counter and the second one to  limit . It also adds one to the static variable  nCounters
A function called  increment that accepts no parameters and returns no value. If the data member  counter is less than  limit ,  increment just adds one to the instance variable  counter .
A function called  decrement that accepts no parameters and returns no value. If  counter is greater than zero,  decrement subtracts one from the  counter .
A function called  getValue that accepts no parameters. It returns the value of the instance variable  counter .
A static function named  getNCounters that accepts no parameters and return an  int .  getNCounters returns the value of the static variable  nCounters .

Solution
int Counter::nCounters = 0;

Counter::Counter(int theCounter, int theLimit)
{
        nCounters++;
        counter = theCounter;
        limit=theLimit;
}

void Counter::increment()
{
    if (counter<limit)
        counter++;
}

void Counter::decrement()
{
    if (counter>0)
        counter--;
}

int Counter::getValue()
{
    return counter;
}


int Counter::getNCounters()
{
    return nCounters;
}

13.5 Focus on Software Engineering: Separating Class Specification from Implementation: 10854

Question

Write the interface (.h file) of a class  Counter containing:
An instance variable  counter of type  int , initialized to 0.
A function called  increment that adds one to the instance variable  counter . It does not accept parameters or return a value.
A function called  getValue that doesn't accept any parameters. It returns the value of the instance variable  counter .
A default constructor.

Solution
class Counter {

private:
int counter;

public:
void increment();
int getValue();
Counter();
};

13.5 Focus on Software Engineering: Separating Class Specification from Implementation: 10853

Question

Write the implementation (.cpp file) of the  ContestResult class from the previous exercise. Again, the class contains:
An instance variable  winner of type  string , initialized to the empty string.
An instance variable  secondPlace of type  string , initialized to the empty string.
An instance variable  thirdPlace of type  String , initialized to the empty string.
A function called  setWinner that has one parameter, whose value it assigns to the instance variable  winner .
A function called  setSecondPlace that has one parameter, whose value it assigns to the instance variable  secondPlace .
A function called  setThirdPlace that has one parameter, whose value it assigns to the instance variable  thirdPlace .
A function called  getWinner that has no parameters and that returns the value of the instance variable  winner .
A function called  getSecondPlace that has no parameters and that returns the value of the instance variable  secondPlace .
A function called  getThirdPlace that has no parameters and that returns the value of the instance variable  thirdPlace .

Solution
void ContestResult::setWinner (string theWinner)
{
    winner = theWinner;
}

void ContestResult::setSecondPlace (string theSecondPlace)
{
    secondPlace = theSecondPlace;
}

void ContestResult::setThirdPlace (string theThirdPlace)
{
    thirdPlace = theThirdPlace;
}

string ContestResult::getWinner ()
{
    return winner;
}

string ContestResult::getSecondPlace()
{
    return secondPlace;
}

string ContestResult::getThirdPlace()
{
    return thirdPlace;
}

13.3: Defining an Instance of a Class in C++: 11204

Question

Creating objects of the Currency class require a name (string), a currency symbol (string) and the number of decimal places (integer) usually associated with the currency (in that order). Creating objects of the Money class require a Currency object, and the actual amount (integer) (in that order). Define an object of type Currency named canadianDollars and corresponding to the name "Canadian Dollar", the symbol "C$", and 2 decimal places. Define an object of type Money named valueInCanadians corresponding to 230 units of canadianDollars.

Solution
Currency canadianDollars ("Canadian Dollar", "C$", 2);
Money valueInCanadians (canadianDollars,230);

13.3: Defining an Instance of a Class in C++: 11200

Question

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Declare two integers corresponding to a width and a length and read values into them from standard input (in that order). Use these value to define an object of type Window named newWindow.

Solution
int width, length;
cin >> width >> length;
Window newWindow (width, length);

13.3: Defining an Instance of a Class in C++: 11198

Question

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Define an object named window, of type Window, corresponding to a 80 x 20 window.

Solution
Window window (80,20);

13.3: Defining an Instance of a Class in C++: 11196

Question

Objects of the BankAccount class require a name (string) and a social security number (string) be specified (in that order)upon creation. Define an object named  account , of type  BankAccount , using the values "John Smith" and "123-45-6789" as the name and social security number respectively.

Solution
BankAccount account ("John Smith", "123-45-6789");

CH12: Programming Challenge #15

Question
I only have a partial...anyone have more info send via comment.

SC-CUSTOM EXERCISE

12.9: Random-Access Files with C++

I don't have the questions, if you have them send via comment.

12.3: Passing File Stream Objects to Functions in C++ 11319

Question

Write the definition of a function named  openLog . This function can be safely passed two arguments, an  fstream object and a  string object. The function associates the  fstream with a file whose name is formed by concatenating the second argument with the suffix ".log". The  fstream is made ready for output that is appended to the end of the file. If the file did not exist previously, false is returned; otherwise its content is deleted (truncated) and replaced by a single line:
    PREVIOUS CONTENT DELETED
and true is returned.

Solution
If you have a solution, please send via comment

12.2: File Output Formatting: 11308

Question

Given three variables, k, m, n, of type int that have already been declared and initialized, write some code that prints each of them left-justified in a 9-position field on the same line. For example, if their values were 27, 987654321, -4321, the output would be:

|27xxxxxxx987654321-4321xxxx

NOTE: The vertical bar,  | , on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

Solution
cout.setf(ios::left, ios::adjustfield);
cout << setw(9) << k << setw(9) << m << setw(9) << n;

12.2: File Output Formatting: 11304

Question

Given three variables, a, b, c, of type double that have already been declared and initialized, write a statement that prints each of them on the same line, separated by one space, in such away that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 4.014268319, 14309, 0.00937608, the output would be:

|4.01427x14309.00000x0.00938

NOTE: The vertical bar,  | , on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

Solution
cout << setprecision(5);
cout << fixed << a << " " << b << " " << c << " ";

12.1: File Operations: 11314

Question

Given an  ifstream object named  input , associate it with a file named  hostdata by opening the file for input.

Solution
input.open("hostdata",ios::in);

12.1: File Operations: 11313

Question

Given an  fstream object named  menu , associate it with a file named  todaysmenu.txt for output in a way that truncates (erases) any existing data in the file.

Solution
menu.open("todaysmenu.txt",ios::out);

12.1: File Operations: 11312

Question

Given an  fstream object named  todo , associate it with a file named  todolist by opening the file for appending.

Solution
todo.open("todolist");

12.1: File Operations: 11311

Question

Given an  ofstream object named  output , associate it with a file named  yearsummary.txt for output in a way that truncates (erases) any existing data in the file.

Solution
output.open("yearsummary.txt",ios::trunc);