Hello all,
I'm attempting to create a Fraction Calculator. But for some reasons on my RationalTest program it is giving me an error that getAdd in Rational cannot be applied to (Rational). I'm very confused by this and its probably something simple wrong, but can someone steer me in the right direction? The program is meant to take two fractions in and do the different get methods for arithmitic. the "gcd" is for reducing the fraction, and within the get methods i have it set to find a common denomenator and then reduce after the arithmitic is done. Thanks for the help! - import javax.swing.JOptionPane;
-
-
public class Rational
-
{
-
private int num;
-
private int den;
-
-
public Rational(int n, int d)
-
{
-
num = n/gcd(n,d);
-
den = d/gcd(n,d);
-
}
-
private int gcd(int M, int N)
-
{
-
int rem;
-
while (N!=0) {
-
rem = M%N;
-
M = N;
-
N = rem;
-
}
-
return M;
-
}
-
public void getFraction()
-
{
-
JOptionPane.showMessageDialog(null, num + "/" + den);
-
}
-
-
public void getFloat()
-
{
-
JOptionPane.showMessageDialog(null, num/den);
-
}
-
public int getNum()
-
{
-
return num;
-
}
-
-
public int getDen()
-
{
-
return den;
-
}
-
-
-
public int getAdd(Rational fract2, int num, int den, int n, int d)
-
{
-
int num1 = num;
-
int den1 = den;
-
-
num1 = ((num*fract2.getDen()) + (den*fract2.getNum()));
-
den1 = den*fract2.getDen();
-
-
num = num1;
-
den = den1;
-
-
num = n/gcd(n,d);
-
den = d/gcd(n,d);
-
-
return num/den;
-
-
}
-
public int getSubtract(Rational fract2, int num, int den, int n, int d)
-
{
-
int num1 = num;
-
int den1 = den;
-
-
-
num1 = ((num*fract2.getDen()) - (den*fract2.getNum()));
-
den1 = den*fract2.getDen();
-
-
-
num = num1;
-
den = den1;
-
-
num = n/gcd(n,d);
-
den = d/gcd(n,d);
-
-
return num/den;
-
}
-
public int getMultiply(Rational fract2, int num, int den, int n, int d)
-
{
-
int num1 = num;
-
int den1 = den;
-
-
-
num1 = ((num*fract2.getDen()) * (den*fract2.getNum()));
-
den1 = den*fract2.getDen();
-
-
num = num1;
-
den = den1;
-
-
num = n/gcd(n,d);
-
den = d/gcd(n,d);
-
-
return num/den;
-
}
-
public int getDivide(Rational fract2, int num, int den, int n, int d)
-
{
-
int num1 = num;
-
int den1 = den;
-
-
-
num1 = ((num*fract2.getDen()) / (den*fract2.getNum()));
-
den1 = den*fract2.getDen();
-
-
num = num1;
-
den = den1;
-
-
num = n/gcd(n,d);
-
den = d/gcd(n,d);
-
-
return num/den;
-
}
-
}
-
-
import javax.swing.JOptionPane;
-
-
public class RationalTest
-
{
-
public static void main(String args[])
-
{
-
int num;
-
int den;
-
-
-
Rational fract1 = new Rational(num, den);
-
-
num = Integer.parseInt(JOptionPane.showInputDialog("Enter first numerator"));
-
den = Integer.parseInt(JOptionPane.showInputDialog("Enter first denominator"));
-
-
Rational fract2 = new Rational(num, den);
-
-
num = Integer.parseInt(JOptionPane.showInputDialog("Enter second numerator"));
-
den = Integer.parseInt(JOptionPane.showInputDialog("Enter second denominator"));
-
-
-
fract1.getAdd(fract2);
-
-
System.out.printf("The addition results are: %s\n", fract1.getFraction());
-
System.out.printf("And the decimal results for addition are: %s\n", fract1.getFloat());
-
}
-
}
-
3 3171 Ganon11 3,652
Recognized Expert Specialist
Exactly what error are you getting? Copy and paste will do fine.
You passed one parameter to the getAdd method in your main method but when you defined it in the Rational class you specified that it takes five parameters.
JosAH 11,448
Recognized Expert MVP
I don't understand why the getAdd, getSubtract etc. methods return ints. I would've
expected something like this: -
public Rational add(Rational that) {
-
return add(that.num, that.den);
-
}
-
-
public Rational add(int num, int den) {
-
return new Rational(this.num*den+this.den*num, this.den*den);
-
}
-
-
// the same for the other operators
-
For convenience you should implement the toString() method as well: -
public String toString() {
-
return num+"/"+den;
-
}
-
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Steve |
last post by:
I am having trouble finding the answer to this question, I'm thinking
the solution must be blindingly obvious, so therefore of course I
cannot see it.
I wish to take a fraction in DEC say .4 and...
|
by: firstcustomer |
last post by:
Hi,
Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that
someone will be able to point me to a ready-made solution to my
problem!
A friend of mine (honest!) is wanting to...
|
by: evilmonkey |
last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
|
by: d0ugg |
last post by:
Hi,
I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know.
// fraction.cpp
...
|
by: Nkhosinathie |
last post by:
hello everyone,i'm writing this csalculator proogram that is using functions to perform functions in a fraction calculator.this is what i did and my problem now the readInFraction does not take the...
| |
by: jrw133 |
last post by:
So i was given this program in class. i am supposed to create a four-function calculator for fractions using a fraction class.
Heres what the requirements are:create a member function for each of...
|
by: robinsiebler |
last post by:
I've never had any call to use floating point numbers and now that I
want to, I can't!
*** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. ***
0.29999999999999999
0.29999999999999999
|
by: Myxamatosis |
last post by:
we're given an implementation file to base our class of Fraction off of.
Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle.
so to create an...
|
by: Jason |
last post by:
I'm making a program that will convert decimal inputs (in this case, in
inches) and output a fractional answer. At the moment, I'm only able to
output the fractional answer in three parts: A whole...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |