Rational number program

fractions being added...it wont work...

Page 1 of 1

8 Replies - 11817 Views - Last Post: 20 October 2009 - 03:42 PM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Rational number program

Posted 19 October 2009 - 07:59 PM

I have been working on this one for awhile now. I had to write a Rational class that extended java.lang.Number, and write a tester for it. I am trying to get it to add the fractions with the methods that I declared in my Rational class...this is what i have.

public class Rational extends Number implements Comparable
{
	//data fields
	private long numerator = 0;
	private long denominator = 1;
	
	//constructs a default rational
	public Rational()
	{
		this(0, 1);
	}
	
	//constructs specific rational
	public Rational(long numerator, long denominator)
	{
		long gcd = gcd(numerator, denominator);
		this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd;
		this.denominator = Math.abs(denominator) / gcd;
	}
	
	//finding gcd
	private static long gcd(long n, long d)
	{
		long n1 = Math.abs(n);
		long n2 = Math.abs(d);
		int gcd = 1;
		
		for (int k = 1; k <= n1 && k <= n2; k++)
		{
			if (n1 % k == 0 && n2 % k == 0)
				gcd = k;
		}
		
		return gcd;
	}
	
	//return numerator
	public long getNumerator()
	{
		return numerator;
	}
	
	//return denominator
	public long getDenominator()
	{
		return denominator;
	}
	
	//add a rational number to this rational
	public Rational add(Rational secondRational)
	{
		long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator();
		long d = denominator * secondRational.getDenominator();
		return new Rational(n, d);
	}
	
	//subtracting a rational from this rational
	public Rational subtract(Rational secondRational)
	{
		long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator();
		long d = denominator * secondRational.getDenominator();
		return new Rational(n, d);	
	}
	
	//multiply a rational to this rational
	public Rational multiply(Rational secondRational)
	{
		long n = numerator * secondRational.getNumerator();
		long d = denominator * secondRational.getDenominator();
		return new Rational(n, d);
	}
	
	//divide a rational from this rational
	public Rational divide(Rational secondRational)
	{
		long n = numerator * secondRational.getDenominator();
		long d = denominator * secondRational.numerator;
		return new Rational(n, d);
	}
	
	//override toString method
	public String toString()
	{
		if (denominator == 1)
			return numerator + "";
		
		else 
			return numerator + "/" + denominator;
	}
	
	//override equals method in Object class
	public boolean equals(Object parm1)
	{
		if ((this.subtract((Rational)(parm1))).getNumerator() == 0)
			return true;
		
		else 
			return false;
	}
	
	//implement abstract intValue method in java.lang.Number
	public int intValue()
	{
		return (int)doubleValue();
	}
	
	//implement abstract floatValue method in java.lang.Number
	public float floatValue()
	{
		return (float)doubleValue();
	}
	
	//implement doubleValue method in java.lang.Number
	public double doubleValue()
	{
		return numerator * 1.0 / denominator;
	}
	
	//implement abstract longValue method in java.lang.Number
	public long longValue()
	{
		return (long)doubleValue();
	}
	
	//implement compareTo method in java.lang.Comparable
	public int compareTo(Object o)
	{
		if ((this.subtract((Rational)o)).getNumerator() > 0)
			return 1;
		
		else if ((this.subtract((Rational)o)).getNumerator() < 0)
			return -1;
		
		else
			return 0;
	}
}



That works fine.... Now, this is the tester for it.

import java.util.*;
import java.io.*;

public class RationalTester extends Rational
{
	//main method
	public static void main(String[] args)
	{
		//create and initialize
		Rational c1 = new Rational(4, 2);
		Rational c2 = new Rational(2, 3);
		
		//output results
		System.out.println(c1 + " + " + c2 + " = " + c1.add(c2));
		System.out.println(c1 + " - " + c2 + " = " + c1.subtract(c2));
		System.out.println(c1 + " * " + c2 + " = " + c1.multiply(c2));
		System.out.println(c1 + " / " + c2 + " = " + c1.divide(c2));
		
		System.out.println(c2 + " is " + c2.doubleValue());
	}
}	


And that works just fine...

Here is my output:

----jGRASP exec: javac -g C:\Users\Christopher\Documents\Rational.java


----jGRASP: operation complete.

----jGRASP exec: javac -g C:\Users\Christopher\Documents\RationalTester.java


----jGRASP: operation complete.


----jGRASP exec: java RationalTester

2 + 2/3 = 8/3
2 - 2/3 = 4/3
2 * 2/3 = 4/3
2 / 2/3 = 3
2/3 is 0.6666666666666666

----jGRASP: operation complete.

But, I am now having a lot of trouble trying to get this other tester to work... I need it to add fractions, like so, 1/2 + 2/3 + 3/4 ... and so forth till 99/100. This is all I can get it to do though... Do I need another loop?


import java.io.*;
import java.util.*;

public class Fraction extends Rational
{
		
	public static void main(String[] args)
	{
		int n = 1;
		int d = 2;
	   int count = 0;
		
		Rational r = new Rational(n, d);
		
		for (int now = 0; now < 98; now++)
		{
		n++;
		d++;
		}
		
		r = new Rational(n, d);
		System.out.println(r);
		
	}
}	


And this is my output...

----jGRASP exec: javac -g C:\Users\Christopher\Documents\Fraction.java


----jGRASP: operation complete.


----jGRASP exec: java Fraction

99/100

----jGRASP: operation complete.

Is This A Good Question/Topic? 0
  • +

Replies To: Rational number program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Rational number program

Posted 19 October 2009 - 08:14 PM

May be ?

		Rational r = new Rational(n, d);
		
		for (int now = 2; now < 98; now++)
		{
			 Rational t = new Rational(now, now + 1);
			 r = r.add(t);
		}


Was This Post Helpful? 1
  • +
  • -

#3 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Rational number program

Posted 19 October 2009 - 08:30 PM

[quote name='pbl' date='19 Oct, 2009 - 07:14 PM' post='804402']
May be ?

public class Fraction extends Rational
{
		
	public static void main(String[] args)
	{
		int n = 1;
		int d = 2;

		Rational r = new Rational(n, d);
		
		for (int now = 2; now < 98; now++)
		{
		Rational t = new Rational(now, now + 1);
			 r = r.add(t);
		
		
		System.out.println(t);
		
		}
		
		
	}
}		


And this is the output:


----jGRASP exec: java Fraction

2/3
3/4
4/5
5/6
6/7
7/8
8/9
9/10
10/11
11/12
12/13
13/14
14/15
15/16
16/17
17/18
18/19


Not exactly what I am looking for, but how do I get them added together? Ya know, like 1/2 + 2/3 + 3/4 all the way to 99/100, I know it will error out due to integer overflow, but I need to do it....
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Rational number program

Posted 19 October 2009 - 08:35 PM

System.out.println(" + " + t + " = " + r); :)
Was This Post Helpful? 1
  • +
  • -

#5 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Rational number program

Posted 19 October 2009 - 08:39 PM

I think that is it, I am not too used to working with long numbers, and it is still compiling, geez, I guess that is why the prof wants a copy of our run i/o, it is still running......TY pbl, you are AWESOME!!!! I just wish you could gimme part of your brain....BTW, did ya see I am now a DIC++!!!

This post has been edited by theautokustomizer: 19 October 2009 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Rational number program

Posted 19 October 2009 - 08:42 PM

View Posttheautokustomizer, on 19 Oct, 2009 - 07:39 PM, said:

I just wish you could gimme part of your brain....

:) it is 56 years old... ready to exchange a part of it with a part of your youngest cells
Was This Post Helpful? 0
  • +
  • -

#7 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Rational number program

Posted 19 October 2009 - 08:57 PM

LMAO!!!! Well, I would be more than willing to trade off, some of my ummm.... adventurous knowledge brain parts, I have enough of that, just gimme some java brain...lol. Thanks again sensei...

This post has been edited by theautokustomizer: 19 October 2009 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 Tshiknn  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 18-October 09

Re: Rational number program

Posted 19 October 2009 - 09:45 PM

In the second tester, what you're doing is incrementing n and d until they're 99 and 100, and then creating the fraction. What you need to do is this:

public class Fraction extends Rational {
	public static void main(String[] args) {
		int n, d;
		Fraction total = new Fraction(0, 1);

		for (n = 1, d = 2; n <= 99; )
			total.add(new Fraction(n++, d++));

		System.out.println(total);
	}
}

This post has been edited by Tshiknn: 19 October 2009 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Rational number program

Posted 20 October 2009 - 03:42 PM

I am not using the variable Fraction, and I am using my Rational class to create this class... The code that you posted doesn't work for what I need, but I appreciate your post and I hope you might be able to help me in the future or maybe I can help you...Ty again for trying. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1