Implementing comparable interface to rational class(java) - java

I am a beginner in Java and this is my first time using Comparable interface. I don't understand why it keeps returning zero when comparing r1 and r2. Can someone explain to me what is wrong with my code? Thank you.
public class Rational implements Comparable<Rational>{
private int num; //numerator
private int denom; //denominator
public Rational() {
num = 0;
denom = 1;
}
public Rational(int num, int denom) {
if(denom <=0 ){
throw new ArithmeticException("You cannot divide by a non-positive number");
}
this.num = num;
this.denom = denom;
}
//returns numerator of this rational number
public int getNum() {
return num;
}
//returns denominator of this rational number
public int getDenom() {
return denom;
}
public Rational add(Rational rhs) {
return new Rational(num*rhs.denom+rhs.num*denom, denom*rhs.denom);
}
public Rational subtract(Rational rhs) {
return new Rational(num*rhs.denom-rhs.num*denom, denom*rhs.denom);
}
public Rational multiply(Rational rhs) {
return new Rational(num*rhs.num, denom*rhs.denom);
}
public Rational divide(Rational rhs) {
return new Rational(num*rhs.denom, denom*rhs.num);
}
public String toString() {
String result;
if (num == 0)
result = "0";
else if(denom == 1)
result = num + "";
else
result = num + "/" + denom;
return result;
}
public int compareTo(Rational rhs){
double r1 = ((double) getNum()/getDenom());
double r2 = ((double)rhs.getNum() / rhs.getDenom());
return (int) (r1 - r2);
}
public static void main(String[] args) {
Rational r1 = new Rational(1, 2); // 1/2
Rational r2 = new Rational(3, 4); // 3/4
Rational result = r1.add(r2);
Rational result1 = r1.subtract(r2);
Rational result2 = r1.multiply(r2);
Rational result3 = r1.divide(r2);
System.out.println( "r1 + r2 = " + result + "\n" + "r1 - r2 = " + result1 + "\n" + "r1*r2 = "+ result2 + "\n" + "r1/r2 = " + result3 + "\n" + (r1.compareTo(r2)));
}
}

This formula
return (int) (r1 - r2);
produces zero when the difference between r1 and r2 is less than 1, which is the case that you are testing.
Since both denominators are positive by construction of your class, you can cross-multiply and subtract without using division at all:
long a = (long)getNum() * rhs.getDenom();
long b = (long)rhs.getNum() * getDenom();
return Long.compare(a, b);
Use long to avoid overflowing on multiplication.

Related

Using two methods in a class (Rational) to simplify a fraction

I have a class definition for a class of rational numbers. My assignment is to be able to add, multiply and divide any fraction I put in my main function. My program can do all that, but I'm having trouble simplifying the fractions. I want to try and use only two methods to simplify, for example public void reduce(); and private static gcd();
public class Rational {
private int num;
private int denom;
public Rational() {
num = 0;
denom = 1;
}
public Rational(int n, int d) {
num = n;
denom = d;
reduce();
}
public Rational plus(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = (this.num * t.denom) + (this.denom * t.num);
tdenom = (t.denom * this.denom);
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational minus(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = (this.num * t.denom) - (this.denom * t.num);
tdenom = (t.denom * this.denom);
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational multiply(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = this.num * t.num;
tdenom = t.denom * this.denom;
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational divide(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = this.num / t.num;
tdenom = this.denom / t.denom;
Rational r = new Rational (tnum, tdenom);
return r;
}
private static int gcd(int n, int d) {
return gcd(d, n%d);
}
public void reduce() {
//call gcd
gcd(num, denom);
//divide num and denom by gcd by
num = num / gcd(num,denom);
denom = denom / gcd(num,denom);
}
public String toString() {
return String.format("%d/%d", num, denom);
}
}
public class RationalMain {
public static void main(String[] args) {
Rational x = new Rational();
Rational y = new Rational(1,4);
Rational z = new Rational(1,2);
//x = y - z;
x = y.plus(z);
System.out.printf("%s = %s + %s\n", x.toString(), y.toString(), z.toString());
x = z.minus(y);
System.out.printf("%s = %s - %s\n", x.toString(), z.toString(), y.toString());
x = z.multiply(y);
System.out.printf("%s = %s * %s\n", x.toString(), z.toString(), y.toString());
x = y.divide(z);
System.out.printf("%s = %s / %s\n", x.toString(), y.toString(), z.toString());
}
}
That's not how you might achieve the Greatest Common Divisor (GCD). Before you will be able to get your code to work properly you will need to at least fix your gcd() method since currently it will recurse until an ArithmeticException (/ by zero) is generated. You might achieve the task this way:
private static int gcd(int num, int den) {
num = Math.abs(num); // if numerator is signed convert to unsigned.
int gcd = Math.abs(den); // if denominator is signed convert to unsigned.
int temp = num % gcd;
while (temp > 0) {
num = gcd;
gcd = temp;
temp = num % gcd;
}
return gcd;
}
To convert your fractions too their Lowest Terms your reduce() method might look like this if it accepted a Fraction String as an argument (you modify the method parameters if you like):
/*
A Fraction String can be supplied as: "1/2", or "2 1/2", or
"2-1/2, or "-2 32/64", or "-2-32/64". The last 2 examples are
negative fraction values.
*/
private String reduce(String fractionString) {
// Fraction can be supplied as: "1/2", or "2 1/2", or "2-1/2".
// Make sure it's a Fraction String that was supplied as argument...
inputString = inputString.replaceAll("\\s+", " ").trim();
if (!inputString.matches("\\d+\\/\\d+|\\d+\\s+\\d+\\/\\d+|\\d+\\-\\d+\\/\\d+")) {
return null;
}
str2 = new StringBuilder();
String wholeNumber, actualFraction;
if (inputString.contains(" ")) {
wholeNumber = inputString.substring(0, inputString.indexOf(" "));
actualFraction = inputString.substring(inputString.indexOf(" ") + 1);
str2.append(wholeNumber);
str2.append(" ");
}
else if (inputString.contains("-")) {
wholeNumber = inputString.substring(0, inputString.indexOf("-"));
actualFraction = inputString.substring(inputString.indexOf("-") + 1);
str2.append(wholeNumber);
str2.append("-");
}
else {
actualFraction = inputString;
}
String[] tfltParts = actualFraction.split("\\/");
int tfltNumerator = Integer.parseInt(tfltParts[0]);
int tfltDenominator = Integer.parseInt(tfltParts[1]);
// find the larger of the numerator and denominator
int tfltN = tfltNumerator;
int tfltD = tfltDenominator;
int tfltLargest;
if (tfltNumerator < 0) {
tfltN = -tfltNumerator;
}
if (tfltN > tfltD) {
tfltLargest = tfltN;
}
else {
tfltLargest = tfltD;
}
// Find the largest number that divides the numerator and
// denominator evenly
int tfltGCD = 0;
for (int tlftI = tfltLargest; tlftI >= 2; tlftI--) {
if (tfltNumerator % tlftI == 0 && tfltDenominator % tlftI == 0) {
tfltGCD = tlftI;
break;
}
}
// Divide the largest common denominator out of numerator, denominator
if (tfltGCD != 0) {
tfltNumerator /= tfltGCD;
tfltDenominator /= tfltGCD;
}
str2.append(String.valueOf(tfltNumerator)).append("/").append(String.valueOf(tfltDenominator));
return str2.toString();
}
As you can see, a whole number can also be supplied with your fraction so, if you do a call to the above reduce() method like this:
System.out.println(reduce("12-32/64"));
System.out.println(reduce("12 32/64"));
The console window will display:
12-1/2
12 1/2

Exception on Number Class doubleValue

When I run my main method I get the following exception: Exception in thread "main" java.lang.StackOverflowError, followed by this: at Rational.doubleValue(Rational.java:62) like 100+ times. This is for an assignment where the main class is provided for me, so I can't change anything in there. Here is the my code:
public class Rational extends Number implements Comparable<Rational>
{
private int num;
private int denom;
public Rational(int num, int denom)
{
this.num = num;
this.denom = denom;
}
public int compareTo(Rational r)
{
if ((num/denom) - (r.num/r.denom) > 0)
{
return +1;
}
else if ((num/denom) - (r.num/r.denom) < 0)
{
return -1;
}
return 0;
}
public Rational add(Rational r)
{
return new Rational(num + r.num, denom + r.denom);
}
public Rational sub(Rational r)
{
return new Rational(num - r.num, denom - r.denom);
}
public Rational mul(Rational r)
{
return new Rational(num * r.num, denom * r.denom);
}
public Rational div(Rational r)
{
return new Rational(num * r.denom, denom * r.num);
}
public int gcd(Rational r)
{
int i = 1;
while(i != 0)
{
i = denom % r.denom;
denom = r.denom;
r.denom = i;
}
return denom;
}
public String toString()
{
String a = num + "/" + denom;
return a;
}
public double doubleValue()
{
return (double)doubleValue();
}
public float floatValue()
{
return (float)floatValue();
}
public int intValue()
{
return (int)intValue();
}
public long longValue()
{
return (long)longValue();
}
}
And here is the main class(which I cannot modify)
public class Main {
public static void main(String[] args) {
Rational a = new Rational(2, 4);
Rational b = new Rational(2, 6);
System.out.println(a + " + " + b + " = " + a.add(b));
System.out.println(a + " - " + b + " = " + a.sub(b));
System.out.println(a + " * " + b + " = " + a.mul(b));
System.out.println(a + " / " + b + " = " + a.div(b));
Rational[] arr = {new Rational(7, 1), new Rational(6, 1),
new Rational(5, 1), new Rational(4, 1),
new Rational(3, 1), new Rational(2, 1),
new Rational(1, 1), new Rational(1, 2),
new Rational(1, 3), new Rational(1, 4),
new Rational(1, 5), new Rational(1, 6),
new Rational(1, 7), new Rational(1, 8),
new Rational(1, 9), new Rational(0, 1)};
selectSort(arr);
for (int i = 0; i < arr.length-1; ++i) {
if (arr[i].compareTo(arr[i+1]) > 0) {
System.exit(1);
}
}
// for (Rational r : arr) {
// System.out.println(r);
// }
Number n = new Rational(3, 2);
System.out.println(n.doubleValue());
System.out.println(n.floatValue());
System.out.println(n.intValue());
System.out.println(n.longValue());
}
public static <T extends Comparable<? super T>> void selectSort(T[] array) {
T temp;
int mini;
for (int i = 0; i < array.length - 1; ++i) {
mini = i;
for (int j = i + 1; j < array.length; ++j) {
if (array[j].compareTo(array[mini]) < 0) {
mini = j;
}
}
if (i != mini) {
temp = array[i];
array[i] = array[mini];
array[mini] = temp;
}
}
}
}
These methods
public double doubleValue()
{
return (double)doubleValue();
}
public float floatValue()
{
return (float)floatValue();
}
public int intValue()
{
return (int)intValue();
}
public long longValue()
{
return (long)longValue();
}
all result in stack overflow because they're recursively calling themselves with no exit. This is not what you intended.
For each of these methods you need to write code that converts the number represented by your Rational into a double, float, int or long.
By the way, you also have other problems in your definitions of add(), subtract(), and compare() but those have to do with the algorithm you are using vis-a-vis the arithmetical rules that apply to those operations.
public class Rational extends Number implements Comparable<Rational> {
private int num;
private int denom;
public Rational(int num, int denom) {
this.num = num;
this.denom = denom;
}
public int compareTo(Rational r) {
if ((num / denom) - (r.num / r.denom) > 0) {
return +1;
} else if ((num / denom) - (r.num / r.denom) < 0) {
return -1;
}
return 0;
}
public Rational add(Rational r) {
return new Rational(num + r.num, denom + r.denom);
}
public Rational sub(Rational r) {
return new Rational(num - r.num, denom - r.denom);
}
public Rational mul(Rational r) {
return new Rational(num * r.num, denom * r.denom);
}
public Rational div(Rational r) {
return new Rational(num * r.denom, denom * r.num);
}
public int gcd(Rational r) {
int i = 1;
while (i != 0) {
i = denom % r.denom;
denom = r.denom;
r.denom = i;
}
return denom;
}
public String toString() {
String a = num + "/" + denom;
return a;
}
public double doubleValue() {
return (double)num / (double)denom;
}
public float floatValue() {
return (float)num / (float)denom;
}
public int intValue() {
return Math.round(num / denom);
}
public long longValue() {
return Math.round(num / denom);
}
}

java syntax help for ADT rational implementation [Error: cannot find symbol]

so I am trying to build this ADT for rational numbers in java but for some reason I keep getting this error that says cannot find symbol when trying to compile. What em I doing wrong? Is this error because of my syntax?
Author: Juan Suarez
// Class : CS1102 ~ java
// Date : 01/30/2018
// Topic : This porblem set focuse on the implemantation of an
// ADT for rational numbers.
public class RationalC implements Rational {
private int num;
private int den;
// ****************** CONSTRUCTOR **********************************
public RationalC (int numerator, int denominator) {
if (this.den == 0){
throw new ArithmeticException("*** WARNING! input non zero denominator");
}
int reduceFraction = gcd(numerator, denominator);
this.num = numerator / reduceFraction;
this.den = denominator / reduceFraction;
if (this.den < 0) {
this.den = this.den * -1;
this.num = this.num * -1;
}
}
//********************* GETTERS ************************************
public int getNumerator() { return this.num; }
public int getDenominator() { return this.den; }
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
// ******************* OPERATIONS **********************************
//return this + that
//
public RationalC plus(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 + num2;
return new RationalC (complete, commonDenominator);
}
//returns this - that
//
public RationalC subtract(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 - num2;
return new RationalC (complete, commonDenominator);
}
// return this * that
//
public Rational multiply(Rational b){
int top = this.getNumerator() * b.getNumerator();
int bottom = this.getDenominator() * b.getDenominator();
return new RationalC (top, bottom);
}
//return this / that
//
public Rational divide(Rational b){
int top = this.getNumerator() * b.getDenominator();
int bottom = this.getDenominator() * b.getNumerator();
return new RationalC (top, bottom);
}
//retuns value
//
public boolean equals(Rational b) {
if (num == b.getNumerator() && this.getDenominator() == b.getDenominator() )
return(true);
}
//********************* TOOLS **************************************
//returns the rational number to a string
//
public String toString() {
return "(" + this.num + "," + this.den + ")";
}
//returns -1 , 0, +1 if the value of the rational is <, >, or =
//
public int compareTo(Rational b) {
long leftHand = this.getNumerator() * b.getDenominator();
long rightHand = this.getDenominator() * b.getNumerator();
if (leftHand < rightHand) return -1;
if (leftHand > rightHand) return +1;
return 0;
}
private static int gcd(int m, int n) {
if(m < 0) m = -m;
if(n < 0) n = -n;
return m * (n / gcd(m,n));
}
public Rational reciprical(Rational b){
return new RationalC (this.getDenominator(), this.getNumerator() );
}
//******************* TEST UNIT ************************************
public static void main(String[] args) {
x = new Rational (1,2);
y = new Rational (1,3);
z = x.plus(y);
StdOut.println(z);
}
}
In the below piece of code, you didn't declare local variable v.
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
getNumerator and getDenominator are methods.
Call them as this.getNumerator() and this.getDenominator().
Also make sure Rational class is having getNumerator and getDenominator methods.

I have to make a rational class in java, i need to add gcd and a way to test it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
public class Main
{
public static void main(String[] args)
{
Rational a = new Rational(1,2);
Rational b = new Rational(1,4);
System.out.println(a + " + " + b + " = " + a.add(b));
System.out.println(a + " - " + b + " = " + a.sub(b));
System.out.println(a + " * " + b + " = " + a.mul(b));
System.out.println(a + " / " + b + " = " + a.div(b));
}
}
class Rational
{
private int numerator, denominator;
public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
}
public Rational add (Rational b)
{
int commonDenominator = denominator * b.getDenominator();
int numerator1 = numerator * b.getDenominator();
int numerator2 = b.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new Rational (sum, commonDenominator);
}
public Rational sub(Rational b) {
int commonDenominator = denominator * b.getDenominator();
int numerator1 = numerator * b.getDenominator();
int numerator2 = b.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new Rational (difference, commonDenominator);
}
public Rational mul(Rational b) {
int numer = numerator * b.getNumerator();
int denom = denominator * b.getDenominator();
return new Rational (numer, denom);
}
public Rational div(Rational b) {
return mul (b.reciprocal());
}
public int getNumerator ()
{
return numerator;
}
public int getDenominator ()
{
return denominator;
}
public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}
public String toString ()
{
String result;
if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
}
I have all of these done:
constructor
add
sub
mul
div
toString
i just need the gcd and a testing method, I'm not sure how to implement the GCD into my code, if anyone could help, that would be great.
Here's the pseudocode for it, taken from Wikipedia, as well as the recursive version. You should be able to implement either of them in Java. I would have posted this as a comment, since it's not really an answer, but the formatting would have been terrible.
function gcd(a, b)
while b ≠ 0
t := b;
b := a mod b;
a := t;
return a;
function gcd_r(a,b) {
if (b = 0) {
return a;
}
return gcd_r( b, a mod b );
}

Double function not returning Double value?

public class Nitin12assignA6 {
public static void main(String args[]) throws IOException {
series ob = new series();
ob.input();
ob.findSum();
ob.display();
}
}
class series {
int x, n;
double sum;
series() {
x = n = 0;
sum = 0.0f;
}
void input() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter the value of x = ");
x = Integer.parseInt(in.readLine());
System.out.print("Enter the value of n = ");
n = Integer.parseInt(in.readLine());
}
void display() {
System.out.println("The sum of Series upto " + n + " terms is: " + sum);
System.out.println();
}
long fact(int num) {
if(num == 1) {
return 1;
}
return num * fact(num - 1);
}
int power(int num, int exp) {
if(exp == 1) {
return 1;
}
return num * power(num, exp - 1);
}
double term(int numr, long denom) {
return(numr / denom);
}
void findSum() {
int u = 2, l = 4;
sum = 1.0f;
for(int i = 0; i < n; i++) {
if(l % 8 == 0) {
sum += term(power(x, u), fact(l));
// Test start
System.out.println("add" + sum + " power " + power(x, u) + " fact " + fact(l) + " x " + x + " u " + u
+ " l " + l);
System.out.println("term " + term(power(x, u), fact(l)));
System.out.println("test term " + term(5, 2)); // printing 2.0
// instead of 2.5
// Test end
} else {
sum -= term(power(x, u), fact(l));
// Test start
System.out.println("minus" + sum + " power " + power(x, u) + " fact " + fact(l) + " x " + x + " u " + u
+ " l " + l);
System.out.println("term " + term(power(x, u), fact(l)));
// Test end
}
u += 2;
l += 4;
}
}
}
//double term(int numr,long denom)
//{return (numr/denom);}
This function is not returning double value. Please help... This program is find a sum a series upto n terms. Example - test term is returning 2.0 instead of 2.5... Thanks.
You need to cast explicitly to double before devision.
double term(int numr, long denom) {
return ((double)numr / denom);
}
The casting to double is done on the value resulting from the division, so you can read
double term(int numr,long denom) {
return (double) (numr/denom);
}
So numr/denom is evaluated first, and since int/long type is long, the result would be 2L. Then it is casted to double. If you want it to be evaluated a double, do
double term(int numr,long denom) {
return ((double)numr/denom);
}
try
double term(int numr, long denom) {
return ((double) numr/denom);
}
your version had int/long which will always return a non floating point number.
See this:
int a = 2;
int b = 4;
double c = a/b; // 0.0
double d = ((double) a/b); // 0.5
double term(int numr, long denom) {
return (numr / denom);
}
The return value is double but the devision (numr / denom) creates a long which then is converted to double.
This may help.
double term(int numr, long denom) {
return ((double)numr / denom);
}

Categories