Question

In: Computer Science

Instructions Write the definitions of the member functions of the class integerManipulation not given in Example...

Instructions

Write the definitions of the member functions of the class integerManipulation not given in Example 10-11. Also, add the following operations to this class:

  1. Split the number into blocks of n-digit numbers starting from right to left and find the sum of these n-digit numbers. (Note that the last block may not have n digits. If needed add additional instance variables.)
  2. Determine the number of zeroes.
  3. Determine the number of even digits.
  4. Determine the number of odd digits

Also, write a program to test theclass integerManipulation.

The header file for class integerManipulation has been provided for you.

integerManipulation.h:

class integerManipulation
{
public:
void setNum(long long n);
//Function to set num.
//Postcondition: num = n;

long long getNum();
//Function to return num.
//Postcondition: The value of num is returned.

void reverseNum();
//Function to reverse the digits of num.
//Postcondition: revNum is set to num with digits in
// in the reverse order.

void classifyDigits();
//Function to count the even, odd, and zero digits of num.
//Postcondition: evenCount = the number of even digits in num.
// oddCount = the number of odd digits in num.

int getEvensCount();
//Function to return the number of even digits in num.
//Postcondition: The value of evensCount is returned.

int getOddsCount();
//Function to return the number of odd digits in num.
//Postcondition: The value of oddscount is returned.

int getZerosCount();
//Function to return the number of zeros in num.
//Postcondition: The value of zerosCount is returned.

int sumDigits();
//Function to return the sum of the digits of num.
//Postcondition: The sum of the digits is returned.

integerManipulation(long long n = 0);
//Constructor with a default parameter.
//The instance variable num is set accordingto the parameter,
//and other instance variables are set to zero.
//The default value of num is 0;
//Postcondition: num = n; revNum = 0; evenscount = 0;
// oddsCount = 0; zerosCount = 0;

private:
long long num;
long long revNum;
int evensCount;
int oddsCount;
int zerosCount;
};

integerManipulationlmp.cpp:

main.cpp:

#include <iostream>

using namespace std;

int main() {
// Write your main here
return 0;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

integerManipulation.h

class integerManipulation

{

public:

    void setNum(long long n);

    //Function to set num.

    //Postcondition: num = n;

    long long getNum();

    //Function to return num.

    //Postcondition: The value of num is returned.

    void reverseNum();

    //Function to reverse the digits of num.

    //Postcondition: revNum is set to num with digits in

    // in the reverse order.

    void classifyDigits();

    //Function to count the even, odd, and zero digits of num.

    //Postcondition: evenCount = the number of even digits in num.

    // oddCount = the number of odd digits in num.

    int getEvensCount();

    //Function to return the number of even digits in num.

    //Postcondition: The value of evensCount is returned.

    int getOddsCount();

    //Function to return the number of odd digits in num.

    //Postcondition: The value of oddscount is returned.

    int getZerosCount();

    //Function to return the number of zeros in num.

    //Postcondition: The value of zerosCount is returned.

    int sumDigits();

    //Function to return the sum of the digits of num.

    //Postcondition: The sum of the digits is returned.

    integerManipulation(long long n = 0);

    //Constructor with a default parameter.

    //The instance variable num is set accordingto the parameter,

    //and other instance variables are set to zero.

    //The default value of num is 0;

    //Postcondition: num = n; revNum = 0; evenscount = 0;

    // oddsCount = 0; zerosCount = 0;

private:

    long long num;

    long long revNum;

    int evensCount;

    int oddsCount;

    int zerosCount;

};

integerManipulation.cpp

#include "integerManipulation.h"

void integerManipulation::setNum(long long n){

    num = n;

}

long long integerManipulation::getNum(){

    return num;

}

void integerManipulation::reverseNum(){

    long long temp = num;

    revNum = 0;

    while(temp>0){

        revNum = revNum*10 + temp%10;

        temp = temp/10;

    }

}

void integerManipulation::classifyDigits(){

    long long temp = num;

    int digit;

    while(temp>0){

        digit = temp%10;

        if(digit%2==0)

            evensCount++;

        else

            oddsCount++;

        if(digit==0)

            zerosCount++;

        temp = temp/10;

    }

}

int integerManipulation::getEvensCount(){

    return evensCount;

}

int integerManipulation::getOddsCount(){

    return oddsCount;

}

int integerManipulation::getZerosCount(){

    return zerosCount;

}

int integerManipulation::sumDigits(){

    int sum = 0;

    long long temp = num;

    while(temp>0){

        sum += temp%10;

        temp = temp/10;

    }

    return sum;

}

integerManipulation::integerManipulation(long long n){

    num = n;

    revNum = 0;

    evensCount = 0;

    oddsCount = 0;

    zerosCount = 0;

}

main.cpp

#include <iostream>

#include "integerManipulation.h"

using namespace std;

int main()

{

    integerManipulation im1;

    im1.setNum(328473676734036);

    im1.classifyDigits();

    im1.reverseNum();

    cout<<"Number: "<<im1.getNum()<<endl;

    cout<<"Evens count: "<<im1.getEvensCount()<<endl;

    cout<<"Odds count: "<<im1.getOddsCount()<<endl;

    cout<<"Zeros count: "<<im1.getZerosCount()<<endl;

    cout<<"Sum of digits: "<<im1.sumDigits()<<endl;

    return 0;

}


Related Solutions

Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()). Ex: If the input is: 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is: Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770 Note: printInfo() should use three spaces for...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
Using C++ programming. Thank you. Money class has the following member variables and functions. -member variable...
Using C++ programming. Thank you. Money class has the following member variables and functions. -member variable : dollar, cent -member function : setMoney(int dollar, int cent), printMoney(), operator overloading(+) Program the Money class so that the following function can be performed. int main(){ Money a, b, c; A.setMoney(10, 60); B.setMoney(20, 70; (a+b).printMoney(); c = a + b; c.printMoney(); }
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
Instructions Modify the Product class from the Practice Exercise 7 by adding a quantity member. Include...
Instructions Modify the Product class from the Practice Exercise 7 by adding a quantity member. Include a getter and setter, using decorators, and modify the appropriate constructor to also accept a quantity parameter. Then modify the inventory.py file from the practice exercise to include quantity values in the constructor calls with a quantity of 100 for product1 (hammers) and 3000 for product2 (nails). Add print statements to display the quantity values as shown in the Expected Output included below. Submission...
Read the function definitions of the two functions: “compute_st_ave” and “compute_quiz_ave”. Write a function that calculates...
Read the function definitions of the two functions: “compute_st_ave” and “compute_quiz_ave”. Write a function that calculates the average of student averages. (Student averages are stored in st_ave [5] array). Include a statement to print this average on the screen. Declare this function above the “main” function where other functions are declared. In the main program, invoke this function. //Reads quiz scores for each student into the two-dimensional array grade //(but the input code is not shown in this display). Computes...
What is the advantage of defining the member functions outside a class rather than defining inside...
What is the advantage of defining the member functions outside a class rather than defining inside a class? How the following are achieved in C++? a) static polymorphism. b) dynamic polymorphism What will happen if you forget to implement the pure virtual function in the derived class? How a function templates differs from function overloading?
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class 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 accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class 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 accessor function for the radius variable. • getArea. Returns the area of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT