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 6839509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:43:37+00:00 2026-05-26T23:43:37+00:00

Below is selected code from one of 5 classes for this assignment. Each class

  • 0

Below is selected code from one of 5 classes for this assignment.

Each class must have a equals() method that can compare an object of its class to an object of any and all of the 5 classes.

My strategy is to convert each object value to a double for the sake of precision.

As each Class extends Number each class has a doubleValue() method to utilize.

the code would not compile unless I typecasted x to RationalN before executing doubleValue()

but when executing the code it complains when a object of another class is compared as it can’t be typecasted to that Class.

I where do I go from here?

        public class RationalN extends Number{
          private int numerator;
          private int denominator;

          public RationalN(int x, int y){
            if (y == 0){
              throw new ArithmeticException("cannot devide by zero");
            } else {
              this.numerator=x;
              this.denominator=y;
            }
          }

          public double doubleValue(){
            double value = (double)numerator/(double)denominator;
            return (double)value;
          }

          public boolean equals(Object x){
            if (((RationalN)x).doubleValue() == this.doubleValue()){
                  return true;
                } else {
                  return false;
          }
        }
  • 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-05-26T23:43:38+00:00Added an answer on May 26, 2026 at 11:43 pm

    From what I get from your post, you want to check in equals whether the numeric value represented by your object equals the numeric value of the argument object. Therefore, comparing the classes is not appropriate. Instead, your equals() method has too look something like this:

    public boolean equals(Object x) {
        if(this == x)
            return true;
        if(x == null)
            return false;
        if(!(x instanceof Number))
            return false;
        Number n = (Number)x;
        return (n.doubleValue() == this.doubleValue());
    }
    

    This, however, violates the equals() contract:
    new RationalN(1, 1).equals(new Integer(1)) would return true, but new Integer(1).equals(new RationalN(1, 1)); wouldn’t – the above equals() method violates symmetry. It would therefore be appropriate to introduce an abstract class implementing Number (say, MyNumber) which is extended only by your five classes and implements equals() in the above fashion (using instanceof MyNumber).

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

Sidebar

Related Questions

I have a class that has to take product information from one system's database
From the code below I am getting a cell value from the selected row
I grabbed this compact news reader from here Below is the code for animating
I have the code below that makes a list of div's drag and droppable,
I have an issue with this jquery code below. Please give me advice or
I'm creating a game that uses cards. I have an AppController class with one
I have a class definition that looks like this: public class SolrObj { [SolrUniqueKey(id)]
I'm using the code below to try and duplicate information from one set of
In the code below i`m trying to read a list of selected projects and
The problem I am facing with below code is that whenever I try to

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.