Home

My Interests

My Favorites

My Documents

My Pictures

My Contact

...: .:  Tagboard

...: .:  Blogger

...: .:  Favorites

Monday, May 10 ::

new rational 

/**
* @Homework: Assignment 3
* @Exercise: 6.3 in textbook, page 342
* @Author: Richard Parry
* @Version: 1.0 - July 20, 1998
* @Filename: Rational.java
* @School: UCSD, Summer 98 Java 1 class
*
* Description:
* This program Rational accepts two rational numbers given
* by the user and performs aritmetic operations: add, substract, multiply,
* and divide.
*
* Usage:
* r1 = new Ration(numerator, denominator); // create object
* r2 = new Ration(numerator, denominator); // create object
*
* Ration result = Rational.add(r1, r2); // to add
* Ration result = Rational.sub(r1, r2); // to subtract
* Ration result = Rational.mul(r1, r2); // to multiply
* Ration result = Rational.div(r1, r2); // to divide
* r1.toString(); // convert to string
**/

// declare class
public class Rational {

// private instance variable
private int numerator, denominator;


/**"no-argument" Constructor
* default values in case no initialzers are provided.
* Rational object starts in a default state
* @param none
* @return none
* @exception none
**/
public Rational() {
this.numerator = 1;
this.denominator = 2;
}


/**
* Constructor which initializes the numerator and denominator variables.
**/
public Rational(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}


/**
* getNumerator() - return the numerator of the rational number.
**/
public int getNumerator() {
return numerator;
}

/**
* getNumerator() - returns the demoninator part of the rational number.
**/
public int getDenominator() {
return denominator;
}


/**
* toString() - converts a Ration number to a string.
* can be printed out with System.out.println()
**/
public String toString() {
return numerator + "/" + denominator;
}


/**
* rational2Decimal() - accepts a rational number and converts a float (decimal)
* If the numerator is zero, then force results to zero. This solves
* the problem of divide by zero AND the special case problem of
* 0/0.
**/
public static float rational2Decimal( Rational rationalNum ) {
float floatNumerator = rationalNum.getNumerator();
float floatDenominator = rationalNum.getDenominator();
if ( floatNumerator == 0 ) {
return 0;
}
else {
return floatNumerator / floatDenominator;
}
}


/**
* reduce() - accepts a rational number consisting
* of a numerator and denominator and reduces it.
**/
private static Rational reduce( Rational ration ) {

int smallest, largest;
int gcd = 1; // need to initialize

// to get numerator and denominator
int n = ration.getNumerator();
int d = ration.getDenominator();

/** to reduce find smallest number then modulus divide
* if no remainder, then see it will also divide into
* the larger number, if so it is a possible candidate as
* a divisor so save it. Continue looking and save the highest
* value
**/
smallest = Math.abs( Math.min(n, d) ); // absolute value, so there no negative values
largest = Math.max(n, d);

for (int i = 2; i <= smallest; i++) {
if( smallest % i == 0 ) {
if( largest % i == 0 ) {
gcd = i; // save to greatest common denominator
}
}
}

// reducedNumber object - return reduced rational number
Rational reducedNumber = new Rational( n/gcd, d/gcd );
return reducedNumber;
}



/**
* Static method to Add.
* add them and returns the result as a third number.
**/
public static Rational add(Rational rat1, Rational rat2) {
// add rational numbers
int denom = rat1.denominator * rat2.denominator;
int numer = rat1.numerator * rat2.denominator + rat2.numerator * rat1.denominator;

// make new object for results, reduce, and return rational number
Rational r3 = new Rational( numer, denom );
return r3.reduce( r3 );
}


/**
* Static method to substract
* substract them and returns the result as a third number.
**/
public static Rational sub(Rational rat1, Rational rat2) {
int denom = rat1.denominator * rat2.denominator;
int numer = rat1.numerator * rat2.denominator - rat2.numerator * rat1.denominator;

// For special case of zero numerator, force denominator to zero
// To assume that (1/2 - 1/2) expressed as a rational number is 0/0
if ( numer == 0 ) {
denom = 0;
}

// make new object for results, reduce, and return rational number
Rational r3 = new Rational( numer, denom );
return r3.reduce( r3 );
}


/**
* Static method to multiple
* multiply them and returns the result as a third number.
**/
public static Rational mul(Rational rat1, Rational rat2) {
int denom = rat1.denominator * rat2.denominator;
int numer = rat1.numerator * rat2.numerator;

// make new object for results, reduce, and return rational number
Rational r3 = new Rational( numer, denom );
return r3.reduce( r3 );
}


/**
* Static method to divide
* divides them and returns the result as a third number.
**/
public static Rational div(Rational rat1, Rational rat2) {
int numer = rat1.numerator * rat2.denominator;
int denom = rat1.denominator * rat2.numerator;

// make new object for results, reduce, and return rational number
Rational r3 = new Rational( numer, denom );
return r3.reduce( r3 );
}
} // end class declaration
.:: posted by kj : 11:22 AM
C:\New Updates

This is my currents blogger news. check back soon.
C:\Archives


 
This page was created by kyawjerry ,

SUNDAY
August 11, 2002. 1:26 am.
   
Today is , 2003. . .