Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:28:00+00:00 2026-06-11T16:28:00+00:00

I am trying to write a class Rational that had a few methods relating

  • 0

I am trying to write a class Rational that had a few methods relating to adding, subtracting, etc. I want to make it so that within the constructor, I add the values to the private variables and find the GCD to find simplify the fraction. The problem I run into is with my if statements. I want to check if the numbers within the object parameter are negative so I use the if statement to check. The only problem is when I run the program, it doesn’t give me a negative value i.e. I have Rational p = new Rational(-24, 48) and it only returns 1/2.

public class TestRational {

    public static void main(String... args) {
        Rational p = new Rational(-24, 48);
    }

    public Rational(long a, long b){
        numerator = a;
        denominator = b;
        boolean isNegative = false;
        if (numerator*denominator < 0)
            isNegative = true;
        long gd = gcd(numerator, denominator);
        numerator /= gd;
        denominator /= gd;
        if (isNegative)
            numerator = -numerator;;
    }

    private long gcd(long p, long q){
        //checks to see if numerator greater than denominator
        if(p<q)
            return gcd(q,p);
        if(Math.abs(q) == 0)
            return p;
        long remainder = Math.abs(p)%Math.abs(q);
        return gcd(Math.abs(q), Math.abs(remainder));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:28:02+00:00Added an answer on June 11, 2026 at 4:28 pm

    You dont need this

    if (isNegative)
     numerator = -numerator;;
    

    So the constructor becomes

    public Rational(long a, long b){
     numerator = a;
     denominator = b;
     boolean isNegative = false;
     if (numerator*denominator < 0)
     isNegative = true;
     long gd = gcd(numerator, denominator);
     numerator /= gd;
     denominator /= gd;
    }
    

    Hope it works …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write class that inherits from FMX TStyledControl. When style is
I'm trying to write a class that will return a string of HTML with
I'm trying to write a class that would enable me to just write .save();
I'm trying to write a class that implements 64-bit ints for a compiler that
I'm trying to write a class that has a generic member variable but is
Guys, I am trying to write a class in C# that can be used
I'm trying to write a class that allows data to be access by multiple
I'm trying to write a class that can add a bookmark to my Delicious
I am trying to write a class that can parse an iCalendar file and
I am trying to write a class that will calculate checksums using multiple processes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.