All pastes #774769 Raw Edit

java test x2

public java v1 · immutable
·published 2007-11-15 11:52 UTC
rendered paste body
public class Rational {private int numerator;private int denominator;private static int num;private static int den;		/* * * * * * * * * * * * * * * * * * * * * * *\	* Constructors                                *	* Initializes numerator and denominator       *	\* * * * * * * * * * * * * * * * * * * * * * */		//Sets numbers if there are two arguments 	public Rational(int number1, int number2) {		this.numerator = number1;		this.denominator = number2;	}	//Sets numbers if there is one argument		public Rational(int wholeNumber) {		numerator = wholeNumber;		denominator = 1;	}		//Sets numbers if there are no arguments	public Rational() {		numerator = 0;		denominator = 1;	}	/* * * * * * * * * * * * * * * * * * * * * * *\	* First Set of math Methods                   *	*          +        -       x        /        *	\* * * * * * * * * * * * * * * * * * * * * * */		//Adds two objects of type Rational		public static Rational add(Rational r1,Rational r2) {		num = r1.numerator+r2.numerator;		den = r1.denominator+r2.denominator;				fixneg(num, den);		return new Rational(num, den);	}		//Subtracts two objects of type Rational	public static Rational sub(Rational r1, Rational r2) {		num = r1.numerator-r2.numerator;		den = r1.denominator-r2.denominator;				fixneg(num, den);		return new Rational(num, den);	}		//Multiplies two objects of type Rational	public static Rational mult(Rational r1, Rational r2) {		num = r1.numerator*r2.numerator;		den = r1.denominator*r2.denominator;				fixneg(num, den);		return new Rational(num, den);	}		//Divides two objects of type Rational	public static Rational div(Rational r1, Rational r2) {		num = r1.numerator*r2.denominator;		den = r1.denominator*r2.numerator;				fixneg(num, den);		return new Rational(num, den);	}		/* * * * * * * * * * * * * * * * * * * * * * *\	* Second Set of Methods (overloaded).         *	* Same as above, but with calling an object   *	\* * * * * * * * * * * * * * * * * * * * * * */		public Rational add(Rational r2) {		num = numerator+r2.numerator;		den = denominator+r2.denominator;				fixneg(num, den);		return new Rational(num, den);	}		public Rational sub(Rational r2) {		num = numerator-r2.numerator;		den = denominator-r2.denominator;				fixneg(num, den);		return new Rational(num, den);	}		public Rational mult(Rational r2) {		num = numerator*r2.numerator;		den = denominator*r2.denominator;				fixneg(num, den);		return new Rational(num, den);	}		public Rational div(Rational r2) {		num = numerator*r2.denominator;		den = denominator*r2.numerator;				fixneg(num, den);		return new Rational(num, den);	}		/* * * * * * * * * * * * * * * * * * * * * * *\	* Utility Methods                             *	* Equality, disp, fixing negatives, etc   *	\* * * * * * * * * * * * * * * * * * * * * * */		//Checks if the two objects are equal	public static boolean equals(Rational r1, Rational r2) {		if ((r1.numerator * r2.denominator) == (r1.denominator * r2.numerator)) {			return true;		} else {			return false;		}	}		//Converts to string (displays)	public static String disp (Rational obj){		return obj.numerator+"/"+obj.denominator;	}		//Fixes negatives	public static void fixneg(int top, int bottom) {		if ((top >= 0 && bottom < 0) || (top < 0 && bottom < 0)) {			top = (-1)*top;			bottom = (-1)*bottom;		} else {			top = top;			bottom = bottom;			}		num = top;		den = bottom;			}		//Main method, for testing	public static void main(String[] args) {		Rational t1 = new Rational();		Rational t2 = new Rational(3, -2);		Rational t3 = new Rational(0);		Rational t4 = new Rational(-4, 3);		Rational t5 = new Rational(1, 6);				System.out.println( "Let's test this out...\n" +				"Take note that fractions are NOT being simplified\n\n" + 				"Here's a list of the numbers being used:\n" + 				"r1 is blank, which turns into 0/1\n" + 				"r2 is 3/-2\n" + 				"r3 is 0, which turns into 0/1\n" + 				"r4 is -4/3\n" + 				"r5 is 1/6\n\n" + 						"Let's add r1 and r2...\n" + 					Rational.disp(t1) + " plus " + Rational.disp(t2)   + " = " + Rational.disp(Rational.add(t1, t2))     + // -3/1				"\n\nOkay! Now let's subtract r3 from r2\n\t" + 					Rational.disp(t2) + " minus " + Rational.disp(t4)  + " = " + Rational.disp(Rational.sub(t2, t4))    + // -7/5				"\n\nOkay! Now let's multiply r5 and r4 by calling r5 instead of Rational\n\t" + 					Rational.disp(t5) + " times " + Rational.disp(t4)  + " = " + Rational.disp(t5.mult(t4))             + // -4/18				"\n\nOkay! Now let's divide r2 by r5 by calling r2 instead of Rational\n\t" + 					Rational.disp(t2) + " divided by " + Rational.disp(t5)  + " = " + Rational.disp(t2.div(t5))         + // -18/2				"\n\nOkay! Now let's check if r1 and r5 are equal\n\t" + 					"True or false: Is " + Rational.disp(t1) + " equal to " + Rational.disp(t5) + "? " + Rational.equals(t1, t5) + // false				"\n\nOkay! Now let's check if r1 and r3 are equal\n\t" + 					"True or false: Is " + Rational.disp(t1) + " equal to " + Rational.disp(t3) + "? " + Rational.equals(t1, t3) + // true				"\n\nOkay! Everything seems to be working. Ending test . . .");	}		}