473,498 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fraction Calculator Problem

1 New Member
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!

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Rational
  4. {
  5.     private int num;
  6.     private int den;
  7.  
  8.     public Rational(int n, int d)
  9.     {
  10.         num = n/gcd(n,d);
  11.         den = d/gcd(n,d);
  12.     }
  13.     private int gcd(int M, int N)
  14.     {
  15.         int rem;
  16.         while (N!=0) {
  17.             rem = M%N;
  18.             M = N;
  19.             N = rem;
  20.         }
  21.         return M;
  22.     }
  23.     public void getFraction()
  24.     {
  25.         JOptionPane.showMessageDialog(null, num + "/" + den);
  26.     }
  27.  
  28.     public void getFloat()
  29.     {
  30.         JOptionPane.showMessageDialog(null, num/den);
  31.     }
  32.     public int getNum()
  33.     {
  34.         return num;
  35.     }
  36.  
  37.     public int getDen()
  38.     {
  39.         return den;
  40.     }
  41.  
  42.  
  43.     public int getAdd(Rational fract2, int num, int den, int n, int d)
  44.     {
  45.         int num1 = num;
  46.         int den1 = den;
  47.  
  48.         num1 = ((num*fract2.getDen()) + (den*fract2.getNum())); 
  49.         den1 = den*fract2.getDen();
  50.  
  51.         num = num1; 
  52.         den = den1;
  53.  
  54.         num = n/gcd(n,d);
  55.         den = d/gcd(n,d);
  56.  
  57.         return num/den;
  58.  
  59.     }
  60.     public int getSubtract(Rational fract2, int num, int den, int n, int d)
  61.     {
  62.         int num1 = num;
  63.         int den1 = den;
  64.  
  65.  
  66.         num1 = ((num*fract2.getDen()) - (den*fract2.getNum()));
  67.         den1 = den*fract2.getDen();
  68.  
  69.  
  70.         num = num1;
  71.         den = den1;
  72.  
  73.         num = n/gcd(n,d);
  74.         den = d/gcd(n,d);
  75.  
  76.         return num/den;
  77.     }
  78.     public int getMultiply(Rational fract2, int num, int den, int n, int d)
  79.     {
  80.         int num1 = num;
  81.         int den1 = den;
  82.  
  83.  
  84.         num1 = ((num*fract2.getDen()) * (den*fract2.getNum()));
  85.         den1 = den*fract2.getDen();
  86.  
  87.         num = num1;
  88.         den = den1;
  89.  
  90.         num = n/gcd(n,d);
  91.         den = d/gcd(n,d);
  92.  
  93.         return num/den;
  94.     }
  95.     public int getDivide(Rational fract2, int num, int den, int n, int d)
  96.     {
  97.         int num1 = num;
  98.         int den1 = den;
  99.  
  100.  
  101.         num1 = ((num*fract2.getDen()) / (den*fract2.getNum()));
  102.         den1 = den*fract2.getDen();
  103.  
  104.         num = num1;
  105.         den = den1;
  106.  
  107.         num = n/gcd(n,d);
  108.         den = d/gcd(n,d);
  109.  
  110.         return num/den;
  111.     }
  112. }
  113.  
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class RationalTest
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         int num;
  8.         int den;
  9.  
  10.  
  11.         Rational fract1 = new Rational(num, den);
  12.  
  13.         num = Integer.parseInt(JOptionPane.showInputDialog("Enter first numerator"));
  14.         den = Integer.parseInt(JOptionPane.showInputDialog("Enter first denominator"));
  15.  
  16.         Rational fract2 = new Rational(num, den);
  17.  
  18.         num = Integer.parseInt(JOptionPane.showInputDialog("Enter second numerator"));
  19.         den = Integer.parseInt(JOptionPane.showInputDialog("Enter second denominator"));
  20.  
  21.  
  22.         fract1.getAdd(fract2);
  23.  
  24.         System.out.printf("The addition results are: %s\n", fract1.getFraction()); 
  25.         System.out.printf("And the decimal results for addition are: %s\n", fract1.getFloat());
  26.     }
  27. }
  28.  
Oct 11 '07 #1
3 3171
Ganon11
3,652 Recognized Expert Specialist
Exactly what error are you getting? Copy and paste will do fine.
Oct 11 '07 #2
r035198x
13,262 MVP
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.
Oct 11 '07 #3
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:

Expand|Select|Wrap|Line Numbers
  1. public Rational add(Rational that) {
  2.    return add(that.num, that.den);
  3. }
  4.  
  5. public Rational add(int num, int den) {
  6.    return new Rational(this.num*den+this.den*num, this.den*den);
  7. }
  8.  
  9. // the same for the other operators
  10.  
For convenience you should implement the toString() method as well:

Expand|Select|Wrap|Line Numbers
  1. public String toString() {
  2.    return num+"/"+den;
  3. }
  4.  
kind regards,

Jos
Oct 11 '07 #4

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

Similar topics

13
9795
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...
24
6283
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...
6
13379
evilmonkey
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...
1
2187
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 ...
1
5362
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...
1
3390
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...
135
4147
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
3
3085
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...
10
5299
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...
0
7125
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
7205
jinu1996
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...
0
7379
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...
0
5462
agi2029
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,...
1
4910
isladogs
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...
0
4590
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...
0
3093
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...
0
1419
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.