aspose file tools*
The moose likes Java in General and the fly likes checking for only part equivalence between Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "checking for only part equivalence between Objects " Watch "checking for only part equivalence between Objects " New topic
Author

checking for only part equivalence between Objects

Subhash L. Sriram
Greenhorn

Joined: Jan 30, 2005
Posts: 28
Hi,

I have a linked list called "flights", and between each flight number, there is a passenger list for that flight.

Now, I want to write a function that will print only the passengers for a certain list.

I figured what I can do, is print the elements between the first node, and the next node that starts with "Flight".

My question is, since none of my elements are exactly "Flight", they are all "Flight 100" or "Flight 101", how can I tell it to stop as soon as it finds something that starts with "Flight"?

Is it possible?

Thanks in advance!


Subhash
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

You've given us a lot of details here, but unfortunately you haven't really given any that are relevant to the question you're asking, and so I can't tell what you want. How about this: why not show us some code?


[Jess in Action][AskingGoodQuestions]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
From your description (yes, please post more details as well as code) I assume that you have a LinkedList where each node contains either a String in the format "Flight #" or a String holding the name of a passenger.

First, you can use String's startsWith(String s) method to see if a String starts with another String. While iterating through the elements of the LinkedList, you could use that method. Of course, if there's a passenger named "Flight McGregory", you'll have to also verify that the String also contains a number. This leads to my next statement.

This is not the best structure for your data. I don't know where you are in your experience level of Java, so don't worry if you're not to this level yet. Typically you want to give more structure to your data by encapsulating it inside classes to group related items.

In this case, you are tracking a list of flights and passengers, but you haven't captured the fact that a flight is a flight number and a list of passengers for that flight. Instead, you've thrown all the various data into a single list and are using code to provide structure. This inevitably leads to difficulty and coding errors.

If you've covered creating your own classes in your studies, think about creating a class to encapsulate all of the data for a single flight (for example, Flight). Then you can put populated instances of class Flight into the main LinkedList. To print the passengers for a single flight, you'd iterate through the list to find the correct Flight and then tell it to print its passenger list.
Subhash L. Sriram
Greenhorn

Joined: Jan 30, 2005
Posts: 28
Hi,

Thanks to both of you for the reply.

As far as my level, I am not a beginner, I would think a little more than intermediate, but I am also not an expert by any means.

I have done classes before, and was pretty sure I had to do it that way, but got confused and tried to do it this, what seemed to me then, as an "easier" way.

What I have now is, for example:



The way, I have the program written, lets say someone wants to be added as a passenger to Flight 100, I have it like this:



So, the linked list would now be:

Flight 100
John Smith
Flight 101
Flight 102

Then, I also have the remove function working. The problem is when they want to print the passengers for a particular flight.

I thought I could do something like I stated above, adding a printFlight() method to the LinkedList class, starting at the node for the flight they enter, and printing all the data in between that, and the next occurence of "Flight".

To be perfectly honest, I am a senior in Computer Engineering, and last week was midterms. In the mix, I totally forgot about this assignment, and am trying to get as much as I can done by tonight @ midnight!

Also, for engineering majors, they have a class that they claim is to be the equivalent of beginning & intermediate programming rolled into one semester. They cant obviously get all the info from both courses into one, so they skip on alot of stuff.

I took that course 2 yrs ago, and the next course in line was data structures & algorithms. I am having to review everything, and I could definitely use some work in my encapsulation.

If I had more time, I would definitely be trying to get it as structured as possible.

Thanks to both of you for your help. I really appreciate it, and I would like to say this site has been one of the most helpful sites I have visited, where you guys actually try to explain things, instead of just posting code.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
i'm not sure what the "right" way to do what you want done would be. most likely there is no one "right" way. however, what i would do, if this were me coding a problem such as you describe, would be:

i would have a dictionary (a map, HashMap, or similar; i'm not very familiar with the terminology of the Java Collections framework) of lists. the keys in the dictionary/Map would be strings, namely the flight numbers. the value of each key would be a List of all the passengers in it.

adding a passenger would be a matter of finding the right list in the map, using the flight number, and then adding a string object to that list. deleting passengers would be removing from the right list, and so on. printing all the passengers on a given flight is just finding the list for that flight, and printing its entire contents.

whether this is useful to you, only you can tell, of course.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Okay, your description fits what I had gathered from your previous post. I understand you need to get this working ASAP, and that probably involves not doing it the "right" way (of which there are many), but let me first explain how this relates to encapsulation.

Originally posted by Subhash L. Sriram:
I thought I could do something like I stated above, adding a printFlight() method to the LinkedList class, starting at the node for the flight they enter, and printing all the data in between that, and the next occurence of "Flight".
Yes, you could do that (and that's probably what you'll end up with to get it done in a hurry), but can you see how this is breaking encapsulation, or at least cohesion which is an aspect of encapsulation? You may as well name your class LinkedListOfFlightsAndPassengers now since it's responsible for so many Flight and PassengerList responsibilities. You class will be unsutable as a generic LinkedList.

For now, however, your top priority I assume is to get this working and move on. I think that's fine as you're under a deadline, but make sure you revisit this some time soon so you don't enforce too many bad habits.

You current structure will work fine, and your thought for the printFlight() method should be acceptable. Once you find the node holding the flight #, skip it and then print every node after it until you find a node that starts with "Flight ". As I stated above, it breaks if a passenger's name starts with "Flight " and it makes your LinkedList depend on storing Strings instead of Objects (it's no longer generic).

It sounds like you've started down that path. What have you gotten so far? Where are you getting errors: compilation or runtime? I'll gladly help with more hints, but I need to see where you're at now. To do this you'll need casting and the startsWith() method I mentioned above.
To be perfectly honest, I am a senior in Computer Engineering . . . and the next course in line was data structures & algorithms. I am having to review everything, and I could definitely use some work in my encapsulation.
Data Structures was the first class at UCLA that I found to truly address encapsulation in a non-trivial manner. Up to then, it was all syntax and basic concepts (looping, branching, variables, I/O, files, etc). Now that I recall, it was taught in C so it didn't even involve classes. We created ADT (Abstract Data Type) structs and functions that operated on them. This would have benefited from C++ or Java.

I'd actually recommend the tutorials here and at Sun. I never found any of my CS textbooks to be very good, though for C we of course used the K&R standard book of which I had no complaints.
Subhash L. Sriram
Greenhorn

Joined: Jan 30, 2005
Posts: 28
David,

Thank you very much for the hint. I got the printFlight method working, and it does not give me any errors.



Luckily, I got most of it working using my half-hazard way of doing it The last function is something I will have to leave out, because I need to get all the files doc'd up and ready to go....

I will definitely try to get more into cohesion & encapsulation. As a matter of fact, I had to write a paper for this assignment about how those incorporated into my program....oops

I will have to find some time to go over this stuff in more detail definitely!

Thanks again for all your help. I would not even have gotten this far without the great hints!
[ February 20, 2005: Message edited by: Subhash L. Sriram ]
Subhash L. Sriram
Greenhorn

Joined: Jan 30, 2005
Posts: 28
I have everything else ready, but am getting ready to submit, or would have posted it. Plus, it is pretty lengthy!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
If you write down corner cases (i.e. special conditions and limits of ranges) before you start coding, you can make sure to handle them in your code as you go and write tests to ensure you've handled them correctly. This is very helpful when writing unit tests on the job using JUnit and other testing frameworks. My classes barely scratched the surface of testing, and all tests were performed by submitting different input files.

What happens with the following test case?Does this happen for a more general case? If you've changed printFlight() since posting, maybe you've fixed it already.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: checking for only part equivalence between Objects
 
Similar Threads
Are Segments Virtual or Concrete ?
Linked List help
Save XML nodes in a String array for ChooseNodes
Interview question on Linked Lists
Route - Flight - Segment again.