8 Replies - 1452 Views - Last Post: 15 September 2013 - 01:43 AM Rate Topic: -----

#1 TrueFan22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-September 13

I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:44 PM

A constructor that allows you to specify the number’s numerator and denominator when a RationalNumber object is created. Also make the constructor automatically put the rational number in simplest form when called. For example, calling new RationalNumber(3,6,) should created RationalNumber object with a numerator of 1 and a denominator of 2, since 3/6 = 1/2 in simplest form. Also make sure that a RationalNumber object with a denominator of 0 cannot be created

public class RationalNumber
{
    private int numerator=0;
    private int denominator=1;

    public RationalNumber(int numerator,int denominator)
    {

        this.numerator=numerator; 
        this.denominator=denominator; 
        boolean g=true;
        if(denominator==0)
        {
            //System.out.println("Denominator must be >0");
            return g;

        }
        else if(numerator==0)
        {
            numerator=0;
            return numerator;
        }

        int gcd = gcd(numerator,denomerator);
        this.numberator=numberator/gcd;
        this.denomerator=denomerator/gcd;

        if(denomerator<0)
        {
            denomerator *=-1;
            numberator*=-1;
        }
    }

    public static int gcd(int numerator,int denominator)
    {
        if(numerator==0||denominator==0)
        {
            return-1;//error code

   
            public int getNumerator;
            {
                return numerator;
            }

            public int getDenominator;
            {
                return denominator;
            }

This post has been edited by modi123_1: 14 September 2013 - 01:50 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I need help with a java program (Rational Number) OOP

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 14036
  • View blog
  • Posts: 56,156
  • Joined: 12-June 08

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:47 PM

What is the question?
Was This Post Helpful? 0
  • +
  • -

#3 TrueFan22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-September 13

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:50 PM

I would like to know ,Did I set up my code correctly? If not, what are the mistake?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 14036
  • View blog
  • Posts: 56,156
  • Joined: 12-June 08

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:51 PM

Okay... Let's start from the top. Have you tried running the code?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12298
  • View blog
  • Posts: 45,399
  • Joined: 27-December 08

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:52 PM

Constructors don't return values within their definitions. Beyond that, it is really your responsibility to test your code and determine what features aren't working correctly.
Was This Post Helpful? 0
  • +
  • -

#6 TrueFan22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-September 13

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:54 PM

I haven't try to run the code yet, but I m confuse with the part about the constructor automatically put the rational number in simplest form.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12298
  • View blog
  • Posts: 45,399
  • Joined: 27-December 08

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 01:59 PM

Confused about what specifically? You will get better help if you ask specific questions.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: I need help with a java program (Rational Number) OOP

Posted 14 September 2013 - 02:05 PM

View PostTrueFan22, on 14 September 2013 - 08:54 PM, said:

I haven't try to run the code yet..

Why not? You'll be disappointed when you eventually run it and possibly receive very many error messages - better to fix them as you proceed.
Was This Post Helpful? 0
  • +
  • -

#9 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 86
  • View blog
  • Posts: 169
  • Joined: 27-May 13

Re: I need help with a java program (Rational Number) OOP

Posted 15 September 2013 - 01:43 AM

First of all, check your code for missing { and }.
I count 3 missing }.

Secondly: why is getNumerator and getDenominator so indented. They are just class functions and don't need to be so indented, it makes things confusing.

Remember functions always need parentheses () .

Thirdly: constructors can't, by definition, return values.
You use functions for that.

Also remember: you can't nest functions. You can't define functions inside other functions.

Before you attempt to proceed in this assignment, fix these errors.
Next actually running non-finished code is very useful for debugging purposes.
You write a couple of new lines of code --> you run it. Correct mistakes. And write some new lines of code --> run it, correct mistakes.

Next: putting the rational form in it's simplest form.
You were on the right track for thinking you would need the gcd.
The gcd of 3 and 6 is 3.
3/3 is 1
6/3 is 2

So you just need to create a method that calculates the gcd.

This post has been edited by axel1994: 15 September 2013 - 01:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1