Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. //PRIME FACTOR OBJECT
  2. public class PrimeFactor
  3. {
  4.     public int prime;        // prime factor
  5.     public int multiplicity; // number of times the prime factor appears in a factorization
  6.  
  7.     /**
  8.      * Precondition: p is a prime number.  
  9.      *
  10.      * @param p  prime
  11.      * @param m  multiplicity
  12.      * @throws IllegalArgumentException if m < 1
  13.      */
  14.     public PrimeFactor(int p, int m) throws IllegalArgumentException
  15.     {
  16.         if(m < 1) {
  17.             throw new IllegalArgumentException();
  18.         }
  19.         prime = p;
  20.         multiplicity = m;
  21.         }
  22.  
  23.     @Override
  24.     public PrimeFactor clone()
  25.     {
  26.         return new PrimeFactor(prime, multiplicity);
  27.     }
  28.  
  29.     /**
  30.      * Prints out, for instance "2^3" if prime == 2 and multiplicity == 3, or "5" if
  31.      * prime == 5 and multiplicity == 1.
  32.      */
  33.     @Override
  34.     public String toString()
  35.     {
  36.         String X = "";
  37.         if(multiplicity == 1) {
  38.             X += prime;
  39.         }
  40.         else {
  41.             X = prime + "^" + multiplicity;
  42.         }
  43.         return X;
  44.        
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement