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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:37:25+00:00 2026-06-15T19:37:25+00:00

I am currently stuck on a particular part of my code. For my class,

  • 0

I am currently stuck on a particular part of my code. For my class, we are to create a train containing boxcars containing people or cargo. We use generics to define whether a boxcar can hold people or cargo. Then we load individual people/cargo onto the boxcar and if it has the same String “ID” as someone already on the boxcar then we log an error and do not load that person/cargo. This is where I’m having trouble. I cannot for the life of me figure out how to compare their “ID’s” to see if they are equal. The following is the code that I have so far,

package proj5;

public class Person implements Comparable<Person> {

private String id;
private String name;
private int age;

public Person(String id, String name, int age){
    this.id = id;
    this.id = id;
    this.name = name;
    this.age = age;
}   

public Person(String id){
    this.id = id;
}

public String getId(){
    return id;
}

public int getAge(){
    return age;
}

public String getName(){
    return name;
}

public String toString(){
    String str = "        " + "ID: " + id + "  " + " Name: " + name + "  " + " Age: " + age;
    return str;
}

public int compareTo(Person p) {
    int result = this.id.compareTo(p.getId());
    return result;
}

}

package proj5;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Boxcar<T extends Comparable<T>> {

private ArrayList<T> boxcar;
private int maxItems;
private int boxcarID;

public Boxcar(){
    boxcar = new ArrayList<T>();
}

public void load(T thing){
    for(int i = 0; i < boxcar.size(); i++){
    if(boxcar.size() < maxItems && !boxcar.get(i).equals(thing)){
        boxcar.add(thing);
        System.out.println(boxcar.get(i));
    }
    else{
        boxcar.remove(thing);
    }
    }
    Collections.sort(boxcar);
}

public int getBoxcarId(){
    return boxcarID;
}

public int getMaxItems(){
    return maxItems;
}

public void setMaxItems(int i){
    maxItems = i;
}

public void unload(T thing){
    for(T item : boxcar){
        if(item.equals(thing)){
            boxcar.remove(item);
        }
    }
}

public List<T> getBoxcar(){
    return boxcar;
}

public String toString(){
    String str = "";
    for(T item : boxcar){
        str += item + "\n";
    }
    return str;
}

}

The problem is with my load function. I do not know how to compare their ID’s. For clarification, the objects ID’s are Strings. I have other classes but I have only included the ones I thought were necessary. If you need more files I will be happy to provide them. I have been stuck on this for hours and would appreciate any help! Thank you very much in advance!

EDIT: I have tried using the contains() method from the Collections API but why does that not work? It sounds like it would work perfectly.

  • 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-15T19:37:26+00:00Added an answer on June 15, 2026 at 7:37 pm

    You have to implement the equals and hashCode for your class Person.

    The problem is that boxcar.get(i).equals(thing) is calling the generic equals and its only comparing the references.

    so a generic equals will look like.

    public boolean equals(Object obj){
      if (obj == null) return false;
      if (obj == this) return true;
      if (obj instanceof Person){
        Person p = (Person) obj;
        return p.getId().equals(this.getId());
      }
      return false;
    }
    

    and hashCode can be like this

    public int hashCode(){
      return 37*this.getId().hashCode();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently stuck on the part where user use his username and password
I am currently stuck at the part whenever I drag a HTML5 slider, it
I've been trying to learn how to code in xslt and currently am stuck
I'm creating quite a cool image viewer but am stuck in one particular part:
I'm currently stuck with using Atom CPU desktop for my Android app development. What
I am currently stuck at this problem right now where I don't seem to
I am currently stuck, having two separate glossaries: main & acronyms . Acronyms glossary
I'm currently stuck on this problem. I've hooked into the method_missing function in a
My app, tommienutest is currently stuck in Awaiting Recurring Charge Cancellation-limbo. Is there any
I'm just getting started with Visual Basic .NET and I'm currently stuck on the

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.