Saturday 17 September 2011

EX 1


1. Develop Rational number class in Java. Use JavaDoc comments for documentation. Your implementation should use efficient representation for a rational number, i.e. (500 / 1000) should be represented as (½).

ALGORITHM:
STEP 1: Get two inputs from the user through command line arguments.
STEP 2: Store the numerator to variable a and denominator to variable b.
STEP 3: If both a and b are either positive or negative, set the flag as 0.
STEP 4: If either a or b is negative, set flag as 1.
STEP 5: Compare the values of a and b and assign the lowest value to c.
STEP 6: Set the for loop for i=2.
STEP 7: If both a and b values are divisible by i, then perform
  (i)  a=a/i;
(ii) b=b/i;
(ii) i=1;
STEP 8: Repeat the loop if the value of i is less than c. Break the loop if the condition fails.
STEP 9: If flag is 1, display the result as negative number; else display it as positive number.

0 comments:

Post a Comment