CSCI240 Program 9 Classes myInt class solution

$25.00

Original Work ?

Download Details:

  • Name: Assign9-xf90bt.zip
  • Type: zip
  • Size: 4.58 KB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (8 votes)

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

  1. Use a seed value of 815 for the random number generator.
  2. Make sure to add #include statements for the cstdlib library.
  3. Each method must have a documentation box like a function.
  4. 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

Extra Credit

For up to 5 points of extra credit, modify the class so that it can hold integer values with a range larger than the current + 2 billion. This should be done by changing the data type of the data member from int to long long. The long long data type will increase the range of possible values to roughly + 9 quintillion.

Any method that uses the numeric value should be updated to use the data type long long rather than int. However, don’t simply change every single int in the class to long long. Change only the ones that need to be changed.

After updating the data type, make sure that the code that was previously written in main still works as before.

Finally, add code to main that will create a new myInt object with the value 90237104628012645. Call the sumDigits, reverse, oddDigitCount, evenDigitCount, and zeroCount methods for the new object and display the values that are returned with labels. Use the setInt method to try to change the value to -5058675309. Call the 5 methods again and display the new results.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit Output

Extra Credit

The extra credit number is 90237104628012645
----------------------------------------------------
Adding the digits result                          60
Reversing the digits result        54621082640173209

Odd digits                                         6
Even digits                                       11
Zeroes                                             3

The extra credit number is now 5058675309
----------------------------------------------------
Adding the digits result                          48
Reversing the digits result               9035768505

Odd digits                                         6
Even digits                                        4
Zeroes                                             2