import java.util.Scanner;
import static java.lang.Math.*;
public class Rational
{
private int num;
private int den;
public Rational()
{
num = 0;
den = 1;
}
public Rational( int num, int den)
{
}
public Rational( int wholeNumber )
{
wholeNumber = wholeNumber/1;
}
public String finalDisplay ()
{
return " " ;
}
public void setRational( int r1 )
{
r1 = num/den;
}
public void setRational2( int r2)
{
r2 = num/den;
}
public void getRational()
{
}
public void scoreInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter numerator");
num = keyboard.nextInt();
System.out.println ("Enter Denomenator");
den = keyboard.nextInt();
}
}
Refactorings
No refactoring yet !
Urban
November 6, 2007, November 06, 2007 09:34, permalink
This is the way to implements yor add, sub, mult, div, etc... functions.
You can optimize the result by computing least common multiple and greatest common divisor to simplify the fraction.
import java.util.Scanner;
import static java.lang.Math.*;
public class Rational
{
private int num;
private int den;
public Rational()
{
num = 0;
den = 1;
}
public Rational( int num, int den)
{
this.num = num;
this.den = den;
}
public Rational( int wholeNumber )
{
Rational( wholeNumber, 1 );
}
public String finalDisplay ()
{
return Integer.toString(num) + "/" + Integer.toString(den) ;
}
public void setNum( int num )
{
this.num = num;
}
public void setDen( int den )
{
this.den = den;
}
public int getNum()
{
return num;
}
public int getDen()
{
return den;
}
public void scoreInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter numerator");
num = keyboard.nextInt();
System.out.println ("Enter Denomenator");
den = keyboard.nextInt();
}
public static Rational add( Rational a, Rational b ) {
int den = a.den * b.den;
int num = a.num * b.den + b.num * a.den;
return new Rational( num, den );
}
public void add( Rational a ) {
this = add( this, a );
}
}
Michael Coyne
November 8, 2007, November 08, 2007 01:24, permalink
Here's a Rational Class I had to make for class.
import java.lang.Math.*;
public class Rational {
private long m;
private long n;
public static void main(String args[]) {
Rational p = new Rational(3,3);
}
public Rational() { this(0,1); }
public Rational(long a) { this(a,1); }
public Rational(long a, long b) {
long gcd = this.gcd(a,b);
this.m = a / gcd;
this.n = Math.abs(b / gcd);
}
public long getDenominator() { return this.n; }
public long getNumerator() { return this.m; }
// Find the Greatest Common Denominator
private long gcd(long p, long q) {
p = Math.abs(p);
q = Math.abs(q);
long r = p%q;
while (r > 0) {
p = q;
q = r;
r = p%q;
}
return q;
}
public Rational add(Rational r) {
long p = this.m * r.getDenominator() + this.n * r.getNumerator();
long q = this.n * r.getDenominator();
return new Rational(p, q);
}
public Rational subtract(Rational r) {
long p = this.m * r.getDenominator() - this.n * r.getNumerator();
long q = this.n * r.getDenominator();
return new Rational(p, q);
}
public Rational multiply(Rational r) {
long p = this.m * r.getNumerator();
long q = this.n * r.getDenominator();
return new Rational(p, q);
}
public Rational divide(Rational r) {
long p = this.m * r.getDenominator();
long q = this.n * r.getNumerator();
return new Rational(p, q);
}
public String toString() {
if (this.n == 1)
return "" + this.m;
return this.m + "/" + this.n;
}
public int compareTo(Rational r) {
if (this.equals(r))
return 0;
return (this.subtract(r).getNumerator() > 0 ? 1:-1);
}
// Returns true if and only if obj are the same number or reference the same object
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (this.subtract((Rational)obj).getNumerator() == 0)
return true;
return false;
}
}
dragon
October 29, 2009, October 29, 2009 22:20, permalink
please help me with this problem
Implement a rational number class: Rational
Augment your class with methods for:
a) Initialization (Constructor): parameters are numerator and denominator as integers. You must have 3 constructors as follows
ï‚· No Parameters: 0 / 1
ï‚· One Parameter (x): x / 1
ï‚· Two Parameters (x, y): x / y
b) float getValue(): returns the value of the number
c) Rational add(Rational r): adds to another rational number
All your numbers should be saved in Reduced Form
Augment your class with a main method that constructs two Rational numbers, get the average of the two numbers and prints it on the screen.
I'm trying to define a class to muli, div, add, and subtract rational numbers. I'm new to programing and need a push in the right direction..