Saturday, November 19, 2016

Rational Numbers in Java

In this article a good friend of mine and a fellow software engineer Mr. Alfel Benvic G. Go would like to share with us his program written using Java programming language in solving Rational Numbers. The source code is well documented and easy to understand. I would like to say thank you very much Sir Bon for sharing your work to us and the rest of programmers around the world who might visiting and reading this blog.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.


Program Listing


/*(Rational.java)

1. Define a class for rational numbers. A rational number is a number that can be represented as the
quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so
on we mean the everyday fraction, not the integer division this expression would produce in a Java
program.) Represent rational numbers as two values of type int, one for the numerator and one for the
denominator. Call the class Rational.
2. Include a constructor with two arguments that can be used to set the member variables of an object to
any legitimate values. Also include a constructor that has only a single parameter of type int; call this
single parameter wholeNumber and define the constructor so that the object will be initialized to the
rational number wholeNumber/1.
3. Include a default constructor that initializes an object to 0 (that is 0/1).
4. Numbers are to be input and output in the form 1/2, 15/32, 300/401, and so forth. Note that the
numerator, the denominator, or both may contain a minus sign, so -1/2, 15/-32, and -300/-401 are also
possible inputs.
5. Write a test program to test your class. Hints: Two rational numbers a/b and c/d are equal if a*d equals
c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b.
6. You should include a function to normalize the values stored so that, after normalization, the denominator
is positive and the numerator and denominator are as small as possible. For example, after normalization
4/-8 would be represented the same as -1/2.
*/

import java.util.Scanner;

public class Rational { //begin class
private int Numerator;
private int Denominator;

public Rational (int num, int den){
Numerator = num;
Denominator = den;
}

public Rational (int wholeNumber){
this(wholeNumber,1);
}
public Rational(){
this(0);
}
public void Normalize(){
int num = Numerator;
int den = Denominator;
boolean flag = true;

if(den < 0 && num > 0){
num = num*-1;
den = Math.abs(den);
}
else if(den < 0 && num < 0){
num = Math.abs(num); den = Math.abs(den);
}

while(flag){
for(int i = 9; i > 1; i--){
if(num%i == 0 && den%i == 0){
num = num/i;
den = den/i;
}
}
for(int i = 9; i > 1; i--){
if(num%i == 0 && den%i == 0){
flag = true;
break;
}
else
flag = false;
}
}
Numerator = num;
Denominator = den;
}//end while

public void getNumber(){
System.out.print("\n");
System.out.println("Output: ");
System.out.print(Numerator + "/" + Denominator);
System.out.print("\n");
}


//Test Program
public static void main ( String [] args ){
Scanner test = new Scanner(System.in);
System.out.print("  RATIONAL CLASS   ");
System.out.print("\n======================\n");
System.out.print("Input Numerator: ");
int num = test.nextInt();
System.out.print("Input Denominator: ");
int den = test.nextInt();

Rational r = new Rational(num,den);
   //debug();
r.Normalize();
r.getNumber();
}
} //end class

No comments:

Post a Comment