What annoys me the most is it's an adaptation from something I found on google for BigInteger, and with the few things I changed I can't see where it went wrong :x
package Fraction;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.io.Serializable;import java.util.Vector;
public abstract class SmallExponent {
//--
// Given a proper fraction, return its simple continued
// fraction representation in list form
//--
public static Vector to (BigFraction f) {
Vector v = new Vector ();
if (f.denominator () == BigInteger.ONE) {
v.addElement ((f.numerator ()));
}
else if (f.numerator () == BigInteger.ONE) {
v.addElement (BigInteger.ZERO);
v.addElement ((f.denominator ()));
}
else {
BigFraction x = new BigFraction (f);
BigInteger quotient, remainder;
while (x.denominator ().compareTo(BigInteger.ONE) == 1) {
quotient = x.numerator().divide( x.denominator ());
remainder = x.numerator().mod(x.denominator ());
v.addElement ((quotient));
x = new BigFraction (x.denominator (), remainder);
if (x.denominator () == BigInteger.ONE) {
v.addElement ((x.numerator ()));
}
}
}
return v;
}
public static void main (String[] args) {
BigFraction fractions[] = {new BigFraction (777, 89)};
Vector v;
BigFraction f;
for (int i = 0; i < fractions.length; i++) {
f = fractions[i];
v = SmallExponent.to (f);
System.out.println (v.toString ());
}
}
}
And there's the other BigFraction class (compiled the two at the same time)
//-
// An adaptation of Doug Lea's Fraction.java with
// BigInteger (instead of long) numerator and denominator
//-
package Fraction;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.io.Serializable;
public class BigFraction implements Cloneable, Comparable, Serializable {
protected final BigInteger numerator_;
protected final BigInteger denominator_;
//--
// Accessor methods
//--
public BigInteger numerator () {
return numerator_;
}
public BigInteger denominator () {
return denominator_;
}
//-
// Constructors
//-
public BigFraction (BigInteger num, BigInteger den) {
// Reduce to lowest terms
boolean numNonnegative = gteq (num, BigInteger.ZERO);
boolean denNonnegative = gteq (den, BigInteger.ZERO);
BigInteger a = numNonnegative? num : num.negate ();
BigInteger b = denNonnegative? den : den.negate ();
BigInteger g = a.gcd (b);
if (numNonnegative == denNonnegative) {
numerator_ = a.divide (g);
}
else {
numerator_ = a.negate ().divide (g);
}
denominator_ = b.divide (g);
}
public BigFraction (BigFraction f) {
numerator_ = f.numerator();
denominator_ = f.denominator();
}
public BigFraction (String s) {
this (new BigInteger (s.substring (0, s.indexOf ('/'))),
new BigInteger (s.substring (s.indexOf ('/') + 1)));
}
public BigFraction (long num, long den) {
this (new BigInteger (Long.toString (num)),
new BigInteger (Long.toString (den)));
}
//
// Override toString
//
public String toString () {
return numerator ().toString () + "/" +
denominator ().toString ();
}
//--
// Required to implement Cloneable
//--
public Object clone () {
return new BigFraction (this);
}
//-
// Utility comparison routines
//-
private boolean gt (BigInteger x, BigInteger y) {
return x.compareTo (y) > 0;
}
private boolean gteq (BigInteger x, BigInteger y) {
return x.compareTo (y) >= 0;
}
private boolean lt (BigInteger x, BigInteger y) {
return x.compareTo (y) < 0;
}
private boolean lteq (BigInteger x, BigInteger y) {
return x.compareTo (y) <= 0;
}
//
// Get minimum
//
public BigFraction min (BigFraction val) {
if (compareTo (val) <= 0) {
return this;
}
else {
return val;
}
}
//
// Get maximum
//
public BigFraction max (BigFraction val) {
if (compareTo (val) > 0) {
return this;
}
else {
return val;
}
}
//-
// Convert to BigDecimal
// Rounding mode is any of BigDecimal.ROUND_xxx constants
//-
public BigDecimal asBigDecimal (int scale, int roundingMode) {
BigDecimal num = new BigDecimal (numerator ());
BigDecimal den = new BigDecimal (denominator ());
return num.divide (den, scale, roundingMode);
}
//
// Get negated value
//
public BigFraction negate () {
return new BigFraction (numerator ().negate (), denominator ());
}
//
// Get multiplicative inverse
//
public BigFraction inverse () {
return new BigFraction (denominator (), numerator ());
}
//-
// Add
//-
public BigFraction add (BigFraction b) {
BigInteger an = numerator ();
BigInteger ad = denominator ();
BigInteger bn = b.numerator ();
BigInteger bd = b.denominator ();
return new BigFraction (an.multiply (bd).add (bn.multiply (ad)), ad.multiply (bd));
}
public BigFraction add (BigInteger n) {
return add (new BigFraction (n, BigInteger.ONE));
}
public BigFraction add (long n) {
return add (new BigInteger (Long.toString (n)));
}
//
// Subtract
//
public BigFraction subtract (BigFraction b) {
BigInteger an = numerator();
BigInteger ad = denominator();
BigInteger bn = b.numerator();
BigInteger bd = b.denominator();
return new BigFraction(an.multiply (bd).subtract (bn.multiply (ad)), ad.multiply (bd));
}
public BigFraction subtract (BigInteger n) {
return subtract (new BigFraction (n, BigInteger.ONE));
}
public BigFraction subtract (long n) {
return subtract (new BigInteger (Long.toString (n)));
}
//
// Multiply
//
public BigFraction multiply (BigFraction b) {
BigInteger an = numerator();
BigInteger ad = denominator();
BigInteger bn = b.numerator();
BigInteger bd = b.denominator();
return new BigFraction (an.multiply (bn), ad.multiply (bd));
}
public BigFraction multiply (BigInteger n) {
return multiply (new BigFraction (n, BigInteger.ONE));
}
public BigFraction multiply (long n) {
return multiply (new BigInteger (Long.toString (n)));
}
//-
// Divide
//-
public BigFraction divide (BigFraction b) {
BigInteger an = numerator ();
BigInteger ad = denominator ();
BigInteger bn = b.numerator ();
BigInteger bd = b.denominator ();
return new BigFraction (an.multiply (bd), ad.multiply (bn));
}
public BigFraction divide (BigInteger n) {
return divide (new BigFraction (n, BigInteger.ONE));
}
public BigFraction divide (long n) {
return divide (new BigInteger (Long.toString (n)));
}
//
// Required to implement Comparable
//
public int compareTo (Object other) {
BigFraction b = (BigFraction) (other);
BigInteger an = numerator ();
BigInteger ad = denominator ();
BigInteger bn = b.numerator ();
BigInteger bd = b.denominator ();
BigInteger left = an.multiply (bd);
BigInteger right = bn.multiply (ad);
if (lt (left, right)) {
return -1;
}
if (left.equals (right)) {
return 0;
}
else {
return 1;
}
}
public int compareTo (BigInteger n) {
Object obj = new BigFraction (n, BigInteger.ONE);
return compareTo (obj);
}
//-
// Override equals
//-
public boolean equals (Object other) {
return compareTo ((BigFraction) other) == 0;
}
public boolean equals (BigInteger n) {
return compareTo (n) == 0;
}
public boolean equals (long n) {
return equals (new BigInteger (Long.toString (n)));
}
//
// Override hashCode
//
public int hashCode() {
int num = numerator().intValue ();
int den = denominator ().intValue ();
return num ^ den;
}
public static void main (String args[]){
}
}
:/
when i try java SmallComponent.class I have this error :
Exception in thread "main" java.lang.NoClassDefFoundError: SmallExponent/class
And I have warnings about AddElement in vectors when I compile, don't think it's a problem..
Message was edited by:
Hydex
Wait a second-- when you're trying to run the class, you're invoking it as:
java SmallExponent.class
?
If that's what you're doing, the problem is, get rid of the ".class". You invoke java programs by saying "java" and then the class name, in this example it would be:
java SmallExponent
I may be interpreting that completely wrong, if I am, let me know.
Jezzica85
lol. Been working too long on this **** thing (I have to implement Wiener attack on RSA in a java program) and I forgot the basis.
Thanks anyway, now I have this error :
Exception in thread "main" java.lang.NoClassDefFoundError: SmallExponent (wrong
name: Fraction/SmallExponent)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
/me cries
Don't worry, I've done that more times than I can count. It's really irritating when you've been banging your head against the wall for hours, you walk away for a while, then come back and all of a sudden-- (gasp!) light bulb! I get migraines too...those aren't pleasant when programming either, so I feel your pain. :)
Anyway, this is an error I've never seen myself, so I had to look it up--what I've got is, this means you got the package name wrong at the top of the source file. The package name is case-sensitive, so it might be something as easy as changing the capital F in "Fraction" to lowercase. Like I said, I've never seen this error myself, if that fix doesn't work let me know and we'll keep looking.