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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:20:07+00:00 2026-06-01T16:20:07+00:00

Hi guys could anyone tell me where im going wrong? The basic aim of

  • 0

Hi guys could anyone tell me where im going wrong?

The basic aim of this class is to define a favourite items arraylist which in this case is about cars. The cars objects have a car name and a rating for car 1-5.

how do you see if a string is equal to the car objects rating.
im messing up the part where you compare a string or int to an car object in array list. what is wrong with my equals() method? can contains() method work the same way?

The numberOfItemsOfRating method allows user to specify rating and hence the method returns the no cars with the rating. the searchForItems method checks if String description specified matches the cars name in array list, and hence returns the car in arraylist.

here is a glimpse of my two methods with constructors and variables:

public class FavouriteItems
{
    private ArrayList<Item> cars; 

    /**
     * Constructor for objects of class FavouriteItems
     */
    public FavouriteItems()
    {
        cars= new ArrayList<Item>();

    }

    /**
     * Add a new Item to your collection
     * @param newItem The Item object to be added to the collection.
     */
    public void addToFavourites(Item newItem) 
    {
        cars.add(newItem);

    }
    /**
     * Count the number of Items with a given rating 
     * @return The number of Items (Item objects) 
     *          whose rating is rating (could be 0).
     *          If the rating parameter is outside the valid
     *          range 1..5 then print an error message and return 0.
     */
    public int numberOfItemsOfRating(int rating)
    {
        int counter = 0;
        if(rating >= 1 && rating <=5) 
        {
            for ( int i =0; i < cars.size(); i++)
            {
                int num = rating;
                String al = Integer.toString(rating);
                if(cars.get(i).equals(al))
                {
                    counter++;
                }
            }
        }
        else 
        {
            System.out.println("No cars match your ratings");
            counter = 0;
        }
        return counter;
    }

    /**
     * Find the details of a Item given its description
     * @return Item object if its description is in the collection
     * or null if there is no item with that description
     */
    public Item searchForItem(String description) 
    {
         for(int i=0; i<cars.size(); i++)
        { 
            if(cars.equals(description))
            { 
                 return cars.get(i);
            } 
            else 
            { 
                return null;
            }
        }  
      }
} 
  • 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-01T16:20:08+00:00Added an answer on June 1, 2026 at 4:20 pm

    This is not the way you should use the equals method, instead I suggest you use or implement Item#getRating() and Item#getDescription(). Use cars.get(i).getDescription().equals(description) to check for the description. To check for the rating use cars.get(i).getRating() == rating.

    You should not use equals to compare an Item with a string, as this would violate the equals contract.

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

Sidebar

Related Questions

Guys could anyone tell me what's wrong am I doing here? Below are four
I could be not more sorry guys - the question was a very wrong
Ok guys, could you tell me is there a certain bug in current code:
Would be great if you guys could shed some light on this, has baffled
Hi guys I was wondering if anyone could help me with the following: I
I was wondering if you guys could help me. Here are my .h: Class
Hi guys wonder if you guys could help me I'm trying to compile a
Ok I give up and would really appreciate it if you guys could cast
Could you guys give me an example how to read and write from/to xml
Hey guys I was wondering if someone could provide a little help. I've been

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.