Modular Arithmatic

Writing a little program and got in trouble

Page 1 of 1

5 Replies - 7585 Views - Last Post: 06 November 2008 - 08:31 PM Rate Topic: -----

#1 jab0015  Icon User is offline

  • New D.I.C Head

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

Modular Arithmatic

Post icon  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  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Modular Arithmatic

Posted 06 November 2008 - 07:58 PM

And what are the 2 problems ?
Was This Post Helpful? 0
  • +
  • -

#3 jab0015  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 B), returns: (a + B) mod n

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

int multiply(int a, int B), returns: (a x B) 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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Modular Arithmatic

Posted 06 November 2008 - 08:21 PM

View Postjab0015, 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 B), returns: (a + B) mod n

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

int multiply(int a, int B), returns: (a x B) 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) 
  {
	  return -add(a,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

Was This Post Helpful? 1
  • +
  • -

#5 jab0015  Icon User is offline

  • New D.I.C Head

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

Re: Modular Arithmatic

Posted 06 November 2008 - 08:28 PM

oh thanks alot
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Modular Arithmatic

Posted 06 November 2008 - 08:31 PM

View Postjab0015, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1