Here is the assignment and i'm pretty much stuck at Add method and can't get any progress.
Can anyone help me out with the coding?
Urgent.
Thank you.
Create a C++ console project and name the project test1.
Add a class, ModuloZ, to the project. Your representation of the class must appear in two files an interface file, ModuloZ.h, and an implementation file, ModuloZ.cpp, where the files contents conform to the files standard meanings and uses.
The class has two private int members, z and n. The class has three constructors - a no parameter constructor, a one parameter constructor and a two parameter constructor; two public accessors, N and Z; one private custom method, normalize; and three public custom methods, Add, Mul and Show.
The no parameter constructor assigns 2 to z and 1 to n. The one-parameter constructor assigns its parameter to n and assigns 2 to z and normalizes n. The two-parameter constructor assigns its first parameter to n and its second parameter to z; it then asserts that z is greater than or equal to 2; finally it normalizes n.
The normalize method is a private method. Its return type is void and its parameter list is void. The normalize method guarantees that n lies between 0 and z 1 inclusive.
For example, if the value of z is 8 then ns possible values are 0, 1, 2, 3, 4, 5, 6 and 7.
How does normalize normalize n?
n = n % z
guarantees that n lies between z + 1 and z 1. Now all you have to do is test whether n is less than 0. If n is less than 0 then add z to n. E.g.,
n is 13 and z is 8
-13 modulo 8 is -5 and -5 + 8 is 3
The normalized value of -13 modulo 8 is 3
n is 13 and z is 8
13 % 8 is 5
The normalized value of 13 is 5
n is 7 and z is 8
7 modulo 8 is 7
The normalized value of 7 is 7
The public method Add has one ModuloZ parameter and returns a ModuloZ object. The Add method first compares its objects z and its parameters z. If they are not equal then Add crashes the program. If they are equal then Add declares a local ModuloZ object using a 2-parameter constructor. The constructors first parameter is the sum of the Add methods objects n and the Add methods parameters n. The constructors second parameter is the Add methods objects z. Finally the Add method returns this local ModuloZ object as its the Add methods - value.
E.g.,
ModuloZ a(7, 8);
ModuloZ b(-5, 8);
ModuloZ c = a.Add(
;
What the value of cs n?
7 modulo 8 + (-5) modulo 8 = 2 modulo 8
or
7 modulo 8 + (-5) modulo 8 =
7 modulo 8 + 3 modulo 8 =
10 modulo 8 =
2 modulo 8
The value of cs n is 2.
ModuloZ a(7,8);
ModuloZ b(-5,4);
ModuloZ c = a.Add(
crashes the program.
The Mul method multiplies in modulo z.
E.g.
3 mod 8 x 4 mod 8 = 4 mod 8
The public method Show method has one parameter the stream that it writes its data to. The parameter has a default value cout. You decide how to format the data.
E.g.
(n, z)?
<n, z>
n mod z
are possibilities.
Write a main function to test your class as thoroughly as possible.
Can anyone help me out with the coding?
Urgent.
Thank you.
Create a C++ console project and name the project test1.
Add a class, ModuloZ, to the project. Your representation of the class must appear in two files an interface file, ModuloZ.h, and an implementation file, ModuloZ.cpp, where the files contents conform to the files standard meanings and uses.
The class has two private int members, z and n. The class has three constructors - a no parameter constructor, a one parameter constructor and a two parameter constructor; two public accessors, N and Z; one private custom method, normalize; and three public custom methods, Add, Mul and Show.
The no parameter constructor assigns 2 to z and 1 to n. The one-parameter constructor assigns its parameter to n and assigns 2 to z and normalizes n. The two-parameter constructor assigns its first parameter to n and its second parameter to z; it then asserts that z is greater than or equal to 2; finally it normalizes n.
The normalize method is a private method. Its return type is void and its parameter list is void. The normalize method guarantees that n lies between 0 and z 1 inclusive.
For example, if the value of z is 8 then ns possible values are 0, 1, 2, 3, 4, 5, 6 and 7.
How does normalize normalize n?
n = n % z
guarantees that n lies between z + 1 and z 1. Now all you have to do is test whether n is less than 0. If n is less than 0 then add z to n. E.g.,
n is 13 and z is 8
-13 modulo 8 is -5 and -5 + 8 is 3
The normalized value of -13 modulo 8 is 3
n is 13 and z is 8
13 % 8 is 5
The normalized value of 13 is 5
n is 7 and z is 8
7 modulo 8 is 7
The normalized value of 7 is 7
The public method Add has one ModuloZ parameter and returns a ModuloZ object. The Add method first compares its objects z and its parameters z. If they are not equal then Add crashes the program. If they are equal then Add declares a local ModuloZ object using a 2-parameter constructor. The constructors first parameter is the sum of the Add methods objects n and the Add methods parameters n. The constructors second parameter is the Add methods objects z. Finally the Add method returns this local ModuloZ object as its the Add methods - value.
E.g.,
ModuloZ a(7, 8);
ModuloZ b(-5, 8);
ModuloZ c = a.Add(

What the value of cs n?
7 modulo 8 + (-5) modulo 8 = 2 modulo 8
or
7 modulo 8 + (-5) modulo 8 =
7 modulo 8 + 3 modulo 8 =
10 modulo 8 =
2 modulo 8
The value of cs n is 2.
ModuloZ a(7,8);
ModuloZ b(-5,4);
ModuloZ c = a.Add(

The Mul method multiplies in modulo z.
E.g.
3 mod 8 x 4 mod 8 = 4 mod 8
The public method Show method has one parameter the stream that it writes its data to. The parameter has a default value cout. You decide how to format the data.
E.g.
(n, z)?
<n, z>
n mod z
are possibilities.
Write a main function to test your class as thoroughly as possible.