Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Class

$
0
0
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 n’s 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 object’s z and its parameter’s 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 constructor’s first parameter is the sum of the Add method’s object’s n and the Add method’s parameter’s n. The constructor’s second parameter is the Add method’s object’s z. Finally the Add method returns this local ModuloZ object as its – the Add method’s - value.

E.g.,

ModuloZ a(7, 8);
ModuloZ b(-5, 8);
ModuloZ c = a.Add(B);

What the value of c’s 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 c’s n is 2.

ModuloZ a(7,8);
ModuloZ b(-5,4);
ModuloZ c = a.Add(B) 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.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>