THIS NEEDS TO BE DONE IN C AND C ONLY PLEASE FOLLOW ALL DIRE
THIS NEEDS TO BE DONE IN C++ AND C++ ONLY!!! PLEASE FOLLOW ALL DIRECTIONS AND MAKE SURE IT WILL COMPILE AND OUTPUT IS CORRECT. PLEASE DOCUMENT EVERYTHING!! // AND EXPLAIN IF POSSIBLE TOO THANK YOU!!!!!
Overview
For this assignment, implement and use the methods for a class called myInt.
myInt class
The myInt class is used to represent a non-negative integer and some tasks that can be performed on the integer.
Data Members
The class contains one data member.
An integer that holds a value
Constructor
This class has two constructors.
The first constructor is a default constructor (i.e. one that takes no arguments). It should simply initialize the integer data member to 0.
The second constructor takes one argument: an integer that will be used to initialize the integer data member. Call the setInt method that is described below to do the initialization.
Methods
void print()
This method displays the integer data member. It takes no arguments and returns nothing.
int getInt()
This accessor method returns the value of the integer data member. It takes no arguments and returns an integer.
void setInt( int )
This accessor method is used to change the value of the integer data member. It takes one argument: an integer that will potentially be used to update the value of the integer data member. It returns nothing.
If the passed in value is non-negative, use it to update the value of the integer data member. If the passed in value is negative, use the absolute value of the passed in value to update the value of the integer data member.
The cmath library has a function called abs that takes one integer as its argument. It returns the absolute value of the integer.
int sumDigits()
This method will calculate and return the sum of adding the individual digits of the integer data member. For example, if an object contains an integer data member with the value 641, the method should return 11 (the sum of 6 + 4 + 1).
The method takes no arguments and returns an integer: the sum of the digits.
To isolate each of the digits in an integer number, use modulus division by 10. For example, 641 % 10 = 1. To remove a digit from an integer number, use integer division by 10. For example, 641 / 10 = 64. Therefore, to calculate the sum of the digits in an integer number, the logic that is described above can be put inside of a loop that executes until there are no longer any digits to remove from the integer number. Each iteration of the loop should take each isolated number and add it to a running total.
One final not about sumDigits, the value in the integer data member should not be changed.
int reverse()
This method will reverse the digits in the integer data member. For example, if an object contains an integer data member with the value 641, the method should return 146.
The method takes no argument and returns an integer that has a value equal to the reversed number.
To reverse the digits in a number, take advantage of the two properties that were described in the description of the sumDigits method. Namely, that modulus division by 10 can be used to isolate a number and integer division by 10 can be used to remove a number. The reversed number can then be calculated by using simple addition.
To start the process, initialize a variable (say reverseNum) to 0. This will be the variable that holds the reversed number. Inside of a loop that executes as long as the integer number has digits to be removed:
multiply reverseNum by 10
isolate a digit from the number
add the isolated digit to reverseNum
remove a digit from the number
For example, assuming the original value is 641, following the process described above results in:
Iteration #1:
0 * 10 = 0 (remember that reverseNum initially contains 0)
isolate the 1 from 641
0 + 1 = 1
remove the 1 from 641 so the next value is 64
Iteration #2:
1 * 10 = 10
isolate the 4 from 64
10 + 4 = 14
remove the 4 from 64 so the next value is 6
Iteration #3:
14 * 10 = 140
isolate the 6 from 6
140 + 6 = 146
remove the 6 from 6 so the next value is 0
Now the digits have been reversed and the result is 146 and is saved in reverseNum.
As with the sumDigits method, the value in the integer data member should not be changed.
int oddDigitCount()
This method will count the number of odd digits that are part of the value in the integer data member. It takes no argument and returns an integer: the count of the odd digits.
To count the number of odd digits in a number, take advantage of the two properties that have been used in the sumDigits and reverse methods.
The value in the integer data member should not be changed.
int evenDigitCount()
This method will count the number of even digits that are part of the value in the integer data member. It takes no argument and returns an integer: the count of the even digits.
To count the number of even digits in a number, take advantage of the two properties that have been used in the sumDigits and reverse methods.
The value in the integer data member should not be changed.
Note: zero is considered an even number.
int zeroCount()
This method will count the number of zeroes that are part of the value in the integer data member. It takes no argument and returns an integer: the count of the zeroes.
To count the number of zeroes in a number, take advantage of the two properties that have been used in the sumDigits and reverse methods.
The value in the integer data member should not be changed.
Using the myInt class
For this part of the program, a myInt object will be created and the various methods that were written for the class will be called.
In a loop that executes exactly 10 times, get a random number, use the random number to set the value of the myInt object, and display the number, the sum of the digits, the reversed digits, the number of odd digits, the number of even digits, and the number of zeros.
Programming Requirements
Use a seed value of 815 for the random number generator.
Make sure to add #include statements for the cstdlib library.
Each method must have a documentation box like a function.
Hand in a copy of the source code using Blackboard.
Output
The number is 2700
----------------------------------------------------
Adding the digits result 9
Reversing the digits result 72
Odd digits 1
Even digits 3
Zeroes 2
The number is 18614
----------------------------------------------------
Adding the digits result 20
Reversing the digits result 41681
Odd digits 2
Even digits 3
Zeroes 0
The number is 31546
----------------------------------------------------
Adding the digits result 19
Reversing the digits result 64513
Odd digits 3
Even digits 2
Zeroes 0
The number is 18773
----------------------------------------------------
Adding the digits result 26
Reversing the digits result 37781
Odd digits 4
Even digits 1
Zeroes 0
The number is 26625
----------------------------------------------------
Adding the digits result 21
Reversing the digits result 52662
Odd digits 1
Even digits 4
Zeroes 0
The number is 818
----------------------------------------------------
Adding the digits result 17
Reversing the digits result 818
Odd digits 1
Even digits 2
Zeroes 0
The number is 21877
----------------------------------------------------
Adding the digits result 25
Reversing the digits result 77812
Odd digits 3
Even digits 2
Zeroes 0
The number is 5852
----------------------------------------------------
Adding the digits result 20
Reversing the digits result 2585
Odd digits 2
Even digits 2
Zeroes 0
The number is 32516
----------------------------------------------------
Adding the digits result 17
Reversing the digits result 61523
Odd digits 3
Even digits 2
Zeroes 0
The number is 673
----------------------------------------------------
Adding the digits result 16
Reversing the digits result 376
Odd digits 2
Even digits 1
Zeroes 0
Solution
#include <iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
class myInt
{
private:
int number;
public:
myInt() //default constructor
{
number = 0;
}
myInt(int number) //default constructor
{
setInt(number);
}
void setInt(int number) //set method
{
if(number < 0)
number = abs(number);
this->number = number;
}
void print() //print method
{
cout<<number;
}
int getInt() //get method
{
return number;
}
int sumDigits() //method to sum the digits of the number
{
int digit,sum;
sum =0;
int temp = number;
while (number > 0)
{
digit = number % 10;
sum = sum + digit;
number= number/10;
}
number = temp;
return sum;
}
int reverse() //function to reverse the number
{
int rev,temp;
temp = number;
rev = 0;
while (number != 0)
{
rev = rev * 10;
rev = rev + number%10;
number = number/10;
}
number = temp;
return rev;
}
int oddDigitCount() //function to count the odd digits in number
{
int digit,nodd,temp;
nodd =0;
temp = number;
while (number> 0)
{
digit = number % 10;
if (digit % 2 == 1)
nodd++;
number =number/10;
}
number = temp;
return nodd;
}
int evenDigitCount() //function to count the even digits of number
{
int digit,neven,temp;
neven =0;
temp = number;
while (number> 0)
{
digit = number % 10;
if (digit % 2 == 0)
neven++;
number =number/10;
}
number = temp;
return neven;
}
int zeroCount() //function to count the number of zeros in the integer number
{
int digit,nzero,temp;
nzero =0;
temp = number;
while (number> 0)
{
digit = number % 10;
if (digit == 0)
nzero++;
number =number/10;
}
number = temp;
return nzero;
}
};
int main()
{
srand(815); //seed of random number is 815
myInt int1(rand()%10000);
cout<<\"The number is \"<<int1.getInt();
cout<<\"\ --------------------------------------\";
cout<<\"\ Adding the digits result\\t\\t: \"<<int1.sumDigits();
cout<<\"\ Reversing the digits results\\t: \"<<int1.reverse();
cout<<\"\ Odd Digits\\t\\t\\t: \"<<int1.oddDigitCount();
cout<<\"\ Even digits\\t\\t\\t: \"<<int1.evenDigitCount();
cout<<\"\ Zeros\\t\\t\\t\\t: \"<<int1.zeroCount();
return 0;
}
output:






