# Modular Arithmatic

Page 1 of 1

## 5 Replies - 8583 Views - Last Post: 06 November 2008 - 08:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=70819&amp;s=59493418f1ae031e3afcd0183128af22&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jab0015

Reputation: 1
• Posts: 16
• Joined: 20-October 08

# Modular Arithmatic

Posted 06 November 2008 - 07:46 PM

I started on a code to do modular arithmetic (where getting into some cryptography in future classes). I've gotten pretty far in the code and only need to finish two more problems here's my code
```  import java.util.*;
public class Z
{
public Z(int n)
{
z = n;
}
public int mod(int a)
{
int b = a % n
if b < 0
b = b + n;
}
public int add(int a, int b)
{
int c = mod(a) + mod(b)
return mod(c)
}
public int subtract(int a, int b)
{
int c = mod(a) - mod(b)
return mod(c)
}
public int multiply(int a, int b)
{
int c = mod(a) * mod(b)
return mod(c)
}
public int ai(int a)
{
for(int i = 0; i < z; i++)

```

after this I'm lost,
this is what the last two programs are supposed to do

Is This A Good Question/Topic? 0

## Replies To: Modular Arithmatic

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Modular Arithmatic

Posted 06 November 2008 - 07:58 PM

And what are the 2 problems ?

### #3 jab0015

Reputation: 1
• Posts: 16
• Joined: 20-October 08

## Re: Modular Arithmatic

Posted 06 November 2008 - 08:00 PM

oh sorry, I got distracted
Create a class named "Z" that has one constructor that takes a positive integer as a parameter, which will be its "n" value. For example,
Z z10 = new Z(10);
would create an object that can do modular arithmetic on the set Z (mod 10).

Your "Z" class needs to provide the following methods:

int mod(int a), returns: a mod n

int add(int a, int , returns: (a + mod n

int subtract(int a, int , returns: (a - mod n

int multiply(int a, int , returns: (a x mod n

int ai(int a), returns: the additive inverse of a in Zn

int mi(int a), returns: the multiplicative inverse of a in Zn or a -1 if a does not have an MI

ArrayList<Integer> members(), returns: an ArrayList of Integer objects that contains all the members of Zn

ArrayList<Integer> starMembers(), returns: an ArrayList of Integer objects that contains all the members of Zn*

The names, parameter lists, and return types must be exactly as specified, since I will be providing a test program to be run with your utility class, and everything needs to match up.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Modular Arithmatic

Posted 06 November 2008 - 08:21 PM

jab0015, on 6 Nov, 2008 - 07:00 PM, said:

oh sorry, I got distracted
Create a class named "Z" that has one constructor that takes a positive integer as a parameter, which will be its "n" value. For example,
Z z10 = new Z(10);
would create an object that can do modular arithmetic on the set Z (mod 10).

Your "Z" class needs to provide the following methods:

int mod(int a), returns: a mod n

int add(int a, int , returns: (a + mod n

int subtract(int a, int , returns: (a - mod n

int multiply(int a, int , returns: (a x mod n

int ai(int a), returns: the additive inverse of a in Zn

int mi(int a), returns: the multiplicative inverse of a in Zn or a -1 if a does not have an MI

ArrayList<Integer> members(), returns: an ArrayList of Integer objects that contains all the members of Zn

ArrayList<Integer> starMembers(), returns: an ArrayList of Integer objects that contains all the members of Zn*

The names, parameter lists, and return types must be exactly as specified, since I will be providing a test program to be run with your utility class, and everything needs to match up.

As far as the ArrayList are concerned you will have to describe what is "a member of Zn"
But I guess th rest of the code should look like this assuming multiplication and addition inversed is - their respective equivalent

```import java.util.*;
public class Z
{
// the n whith which we will do the modulo
int n;

public Z(int n)
{
this.n = n;
}
public int mod(int a)
{
return a % n;
}
public int add(int a, int b)
{
return a + mod(b);
}
public int subtract(int a, int b)
{
return a - mod(b);
}
public int multiply(int a, int b)
{
return a * mod(b);
}
public int ai(int a, int b)
{
}
public int mi(int a, int b)
{
return -multiply(a,b);
}
}

```

Edited * instead of / in multiply

This post has been edited by pbl: 06 November 2008 - 08:24 PM

### #5 jab0015

Reputation: 1
• Posts: 16
• Joined: 20-October 08

## Re: Modular Arithmatic

Posted 06 November 2008 - 08:28 PM

oh thanks alot

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Modular Arithmatic

Posted 06 November 2008 - 08:31 PM

jab0015, on 6 Nov, 2008 - 07:28 PM, said:

oh thanks alot

No problem but this does not fix your ArrayList issue
do you have to keep tract of all "a" or "b" entered ? Don't see relation with cryptography
and thanks for the thank