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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:30:38+00:00 2026-06-18T15:30:38+00:00

First I have two objects created like so… Recipe recipeOne = new Recipe(Pepperoni Pizza);

  • 0

First I have two objects created like so…

Recipe recipeOne = new Recipe("Pepperoni Pizza");

    Ingredient one = new Ingredient("Dough", 1, UnitOfMeasurement.valueOf("Pounds"));
    Ingredient two = new Ingredient("Sauce", 8, UnitOfMeasurement.valueOf("Ounces"));
    Ingredient three = new Ingredient("Cheese", 10, UnitOfMeasurement.valueOf("Ounces"));

    recipeOne.addIngredient(one);
    recipeOne.addIngredient(two);
    recipeOne.addIngredient(three);

    RecipeBook.addRecipe(recipeOne);

    Recipe recipeTwo = (Recipe) recipeOne.clone();
    recipeTwo.addIngredient(recipeOne.Ingredients[0]);
    recipeTwo.addIngredient(recipeOne.Ingredients[1]);
    recipeTwo.addIngredient(recipeOne.Ingredients[2]);

    RecipeBook.addRecipe(recipeTwo);

    recipeTwo.setName("Pineapple Pizza");

No surprises here, all obvious whats going on, but then I want to check them for equality! And I am looking to check all of their elements obviously to see if they are truly equal or not. So I call “System.out.println(recipeOne.equals(recipeTwo));” which will go here…

public boolean equals(Object obj){

    if(obj instanceof Recipe){

        Recipe tempRec = (Recipe) obj;

        for(int j = 0 ; j < Ingredients.length ; j++){

            if(Ingredients[j].equals(tempRec.Ingredients[j]) == true){

                return true;

            }

        }

    }

    return false;

}

Now I know it is incomplete and will only check the first Ingredient in recipeOne which is “Ingredients[]” and the first ingredient in recipeTwo, the copy, “tempRec.Ingredients[]”. Now my question is, how do I check the rest of the locations and make sure they are all equal before sending the “okay-of-equality” ? Is there a way to go back into the for-loop and check the next spot, maybe store all the trues then when they’re all figured out and finally return true? I’d rather not write out 10 if statements checking all the locations for whether they are null or not then checking to see if the ingredients are equal lol

(Almost forgot my Ingredient.equals(), here it is for reference but it works fine!)

public boolean equals(Object obj){

    if(obj instanceof Ingredient){

        Ingredient tempIngred = (Ingredient) obj;

        if(Name.equals(tempIngred.getName()) && Quantity == (tempIngred.getQuantity()) &&
                unitOfMeasurement.equals(tempIngred.getUnit()))
                return true;

    }

    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-06-18T15:30:39+00:00Added an answer on June 18, 2026 at 3:30 pm

    Invert the condition, and only return true at the very end:

    public boolean equals(Object obj){
    
        if (!obj instanceof Recipe) return false;
        if (obj == this) return true;
    
        Recipe tempRec = (Recipe) obj;
    
        for(int j = 0 ; j < Ingredients.length ; j++) {
            if(!Ingredients[j].equals(tempRec.Ingredients[j])) {
                return false;
            }
        }
    
        return true;
    }
    

    Better still, use the existing library method to do the work for you: Arrays.equals(Object[] a1, Object[] a2).

    public boolean equals(Object obj){
    
        if (!obj instanceof Recipe) return false;
        if (obj == this) return true;
    
        Recipe tempRec = (Recipe) obj;
        return Arrays.equals(this.Ingredients, tempRec.Ingredients);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Dozer to map two objects, I have: /** /* This first class uses
I have a screen with two RadioButtonField objects. By default, the first RadioButtonField shows
I have two objects defined something like this (simplified for sake of the question):
Suppose I have two classes. In the first one I declare this in Class1.h
I have a collection of objects in JavaScript like this: Object collection = new
In one of my projects, I have two data transfer objects RecordType1 and RecordType2
Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor
I have two questions, actaully... First off, Why cant I do this: List<Object> object
I have two table first is TABLE_SUBJECT and second is TABLE_CHAPTER.Now i want to
I have two dates in javascript: var first = '2012-11-21'; var second = '2012-11-03';

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.