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.
15 3639
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
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
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 % ?
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
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: -
int gcd(int n, int m) {
-
int r;
-
// your algorithm here with a few parentheses sprinkled in;
-
// the compiler tells your where they are needed.
-
}
-
kind regards,
Jos
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);
}
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
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
since dividing the numerator and denominator by their gcd will simplify the fraction.
Yep, we know that. What's your question?
kind regards,
Jos
^ I can't seem to come up with the code to do the fraction.
^ 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?
You have already provided two possible answers to your own question... even in code. Sorry, but what in the world is the problem?
-blazed
^ 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: -
private int gcd(int n, int m) { /* implementation here */ }
-
int nonSimplifiedNumerator;
-
int nonSimplifiedDenominator;
-
It doesn't take rocket science to do this: -
int r= gcd(nonSimplifiedNumerator, nonSimplifiedDenominator);
-
int simplifiedNumerator= nonSimplifiedNumerator/r;
-
int simplifiedDenominator= nonSimplifiedDenominator/r;
-
Now this is what I call a spoiler.
kind regards,
Jos
Now this is what I call a spoiler.
kind regards,
Jos
Now you ruined it for me! :P
-blazed
Now you ruined it for me! :P
-blazed
Yep, that's me: I've always been a partypooper. ;-)
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: 7asco |
last post by:
i want the code of greater common divisor algorithm in c++
|
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...
|
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)
{
|
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;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |