473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

gcd common divisor

24
I'm new to Java and I was looking for some help on this particular problem.

I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form.

I tried to calculate it with:
while n!=0
{
r = m mod n;
m = n;
n = r;
}
return m

I tried to implement this, but i'm having problem with it in java

class Fraction{

private int numerator;

private int denominator;

public Fraction(int num, int denom){

setNumerator(num);

setDenominator(denom);

}

public int getDenominator(){

return denominator;

}

public int getNumerator(){

return numerator;

}

public void setDenominator(int denom){

if(denom == 0){

System.err.println("Fatal Error");

System.exit(1); //terminate program

}

denominator = denom;

}



public void setNumerator(int num){

numerator = num;

}

public String toString(){

return getNumerator() + "/" + getDenominator();

}

}


Thank you in advance for any help.
Jul 16 '07 #1
15 3639
blazedaces
284 100+
Firstly, could you please try to put your code in code tags from now on (look at the box on the right when you're posting that says "REPLY GUIDELINES", thank you. It makes the code much easier to read.

Next, what exactly is the problem? What error is showing up when the compiler tries to run this program?

-blazed
Jul 16 '07 #2
judge82
24
I wonder what is the best method, but I'm missing something about naming because all my compiling errors are telling me that I have incompatible types
Jul 16 '07 #3
r035198x
13,262 8TB
I wonder what is the best method, but I'm missing something about naming because all my compiling errors are telling me that I have incompatible types
mod is for % ?
Jul 16 '07 #4
blazedaces
284 100+
I wonder what is the best method, but I'm missing something about naming because all my compiling errors are telling me that I have incompatible types
Repost your code with code tags and then show us all your compile errors so we know what lines they are supposedly on. Also pay attention to what was said in the post above...

-blazed
Jul 16 '07 #5
JosAH
11,448 Expert 8TB
I'm new to Java and I was looking for some help on this particular problem.

I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form.

I tried to calculate it with:
while n!=0
{
r = m mod n;
m = n;
n = r;
}
return m

I tried to implement this [ ... ]
Watch me do it:

Expand|Select|Wrap|Line Numbers
  1. int gcd(int n, int m) {
  2.    int r;
  3.    // your algorithm here with a few parentheses sprinkled in;
  4.    // the compiler tells your where they are needed.
  5. }
  6.  
kind regards,

Jos
Jul 16 '07 #6
judge82
24
like i said guys, I am new with java.. I am trying to help a cousin he knows a write C++

greatest common divisor of two positive integers is the largest integer that is a divisor of both of them. For example, 6 and 15 have 3 as their greatest common divisor, and 15 and 22 have 1 as their greatest common divisor. The following recursive function computes the greatest common divisor of two positive integers. First write a program to test the function; then write and test an equivalent iterative function.

int gcd(int a, int b)
{
int r;

if ((r = a % b) = = 0)
return b;
else
return gcd(b, r);
}
Jul 16 '07 #7
JosAH
11,448 Expert 8TB
Why are you changing a perfectly valid iterative solution of a gcd(n, m) to a
recursive version now? Please make up your mind and tell us what your problem
is and stop throwing code fragments at us; we know what programming is all
about.

kind regards,

Jos
Jul 16 '07 #8
judge82
24
Why are you changing a perfectly valid iterative solution of a gcd(n, m) to a
recursive version now? Please make up your mind and tell us what your problem
is and stop throwing code fragments at us; we know what programming is all
about.

kind regards,

Jos
since dividing the numerator and denominator by their gcd will simplify the fraction.
here is a sample run (user input)
enter a numerator:
6
enter a denominator:
8

your fraction is 6/8
the simplified form is 3/4
Jul 16 '07 #9
JosAH
11,448 Expert 8TB
since dividing the numerator and denominator by their gcd will simplify the fraction.
Yep, we know that. What's your question?

kind regards,

Jos
Jul 16 '07 #10
judge82
24
^ I can't seem to come up with the code to do the fraction.
Jul 17 '07 #11
r035198x
13,262 8TB
^ I can't seem to come up with the code to do the fraction.
You do realize of course that coming up with the gcd and simplifying the fraction are kind of related?
Jul 17 '07 #12
blazedaces
284 100+
You have already provided two possible answers to your own question... even in code. Sorry, but what in the world is the problem?

-blazed
Jul 17 '07 #13
JosAH
11,448 Expert 8TB
^ I can't seem to come up with the code to do the fraction.
But you already have all the ingredients right before your eyes:

Expand|Select|Wrap|Line Numbers
  1. private int gcd(int n, int m) { /* implementation here */ }
  2. int nonSimplifiedNumerator;
  3. int nonSimplifiedDenominator;
  4.  
It doesn't take rocket science to do this:

Expand|Select|Wrap|Line Numbers
  1. int r= gcd(nonSimplifiedNumerator, nonSimplifiedDenominator);
  2. int simplifiedNumerator= nonSimplifiedNumerator/r;
  3. int simplifiedDenominator= nonSimplifiedDenominator/r;
  4.  
Now this is what I call a spoiler.

kind regards,

Jos
Jul 17 '07 #14
blazedaces
284 100+
Now this is what I call a spoiler.

kind regards,

Jos
Now you ruined it for me! :P

-blazed
Jul 17 '07 #15
JosAH
11,448 Expert 8TB
Now you ruined it for me! :P

-blazed
Yep, that's me: I've always been a partypooper. ;-)

kind regards,

Jos
Jul 17 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Frederick Gotham | last post by:
Anyone want to give me a hand to write efficient algorithms for Highest Common Factor and Lowest Common Multiple? This is what I have so far... Caveat: I haven't gone over the following code in...
4
by: sdlt85 | last post by:
Hi, Can someone help me with an idea on how to start writing a C++ code for generating greatest common divisor and the linear combination of two intergers represented as gcd(m, n)= mx + ny and...
2
by: 7asco | last post by:
i want the code of greater common divisor algorithm in c++
4
by: KWSW | last post by:
Was wondering if there is an inbuilt function to find the GCD of 2 numbers. I know BigInteger has one and would like to know if there is one for Integers. Would prefer to use an inbuilt function...
4
by: jmf777 | last post by:
Hi I'm trying to write a program to find the greatest common divisor of 2 numbers input by the user. This is what I got so far: #include <stdio.h> int gcd(int a, int b); int main(void) {
5
by: tejas2991 | last post by:
this is my gcd prog.. #include<iostream> using namespace std; int GCD(int ,int ); void main() { int x,y; cout<<"Plz enter the two numbers : "; cin>>x>>y;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.