Avatar

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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 !

11d755068b875511c919413da0da7668

Urban

November 6, 2007, November 06, 2007 09:34, permalink

No rating. Login to rate!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 );
	}
} 
C284673fa8884e78784d8fe0b64fd066

Michael Coyne

November 8, 2007, November 08, 2007 01:24, permalink

No rating. Login to rate!

Here's a Rational Class I had to make for class.

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
	}
}
Fca79a658b492313ca693685f05a0825

denny

November 27, 2007, November 27, 2007 15:39, permalink

No rating. Login to rate!
1
5/8 - 5/6 = 

Your refactoring





Format Copy from initial code

or Cancel