Forums Register Login

Taxi Rank using ArrayList

1
+Pie Number of slices to send: Send
hi All,

I am trying to figure out my first OO programming assignment for a beginner java course...it is an array list for a taxi rank using taxi objects....and I have been stuck for a couple of days now on how to search the arraylist for the taxiPlate Number element . See if( number == 3) - at the bottom of the main method. Any help would be greatly appreciated.




+Pie Number of slices to send: Send
That is a lot of code, but I don't see a specific question. What problem are you having?
+Pie Number of slices to send: Send
Hello and welcome to the Ranch!

In order to get the best out of Ranchers you need to be more specific and TellTheDetails (<-click). It's a lot of code to pass in detail in order to guess what might be causing problem(s). The only thing I can tell you for now is that the main method is very long, though it should be just a starting point for your application.
1
+Pie Number of slices to send: Send
Welcome to the Ranch
Paul Clapham has added code tags (I think), which you should always use, and you can see how much better the post looks. Unfortunately, it is not only your main method which is too long, but also your lines, which I have tried to shorten by breaking them into smaller parts. You should also break that main method into smaller parts; you have lots of repetition of "enter Y to continue", which ought to be a method of its own.
I am afraid that code suggests you have not yet worked out what object programming is. I think it may be kinder (and you are going to hate me for saying this) to put that code out of its misery and start from scratch.
As Fred Rosenberger always says, programming is more about thinking, so stop coding and get yourself what I always tell people: pencil paper and rubber. (At least you are not in America so you won’t misunderstand rubber, which is the most important member of that trio ‍).
I suggest you go back to the Taxi class and redesign it by writing down what taxis do. And do it in stages, starting with booking and completing a journey. Leave out things like lifts and how many seats for the time being. You can add them later. Get the bit about booking, taking a journey and returning to the rank working first.

Beware: although that hashCode method you wrote fulfils the letter of the law about its general contract, it is very harmful to return the same value regardless. You can really mess up the performance of other classes which depend on the hash code; Joshua Bloch says somewhere in Effective Java™ a badly‑written hash code method can make the difference between an app which works and an app which doesn’t. But you will have to leave the hash code method out for the time being. Get other things working first, then ask about hash code again.

Have you been told specifically to use a List? It would be awkward to find a particular taxi from a List. You would have to iterate the List, until you find the taxi with a particular number. If only you could find a kind of collection allowing you to map from the number to the taxi. You would have to find something which implements mappings from the number (as an Integer) to the Taxi. But that will have to wait until later. Non illegitimis carborundum, or maybe nil desperandum; there is a nice Tutorial available. So when you have got your taxi class working, you can look there and see how you could map from numbers to taxis.

Some of your methods show confusion. A lot of beginners write too much code, and I have sometimes deleted nearly 50% of the code without the functionality changing at all
You are passing things like plate number, and never using them. There is no point in having parameters you don’t use. Even worse, there is one where you pass a boolean, which you then assign to true, then assigning the field from that. Why pass arguments to that method? Why not simply use that method to set the field to true?
+Pie Number of slices to send: Send
Hi all
Thanks for your comments. The code is crazy long and a bit of a work in progress to say the least. My problem is how to go through the array list to find a specific taxiplatenumber element. I have tried .contains but it doesn't work I'm afraid. Any suggestions greatly appreciated.
+Pie Number of slices to send: Send
Well, that particular part is because you asked if the ArrayList contained an integer value which you got from the user. But the ArrayList contains Taxi objects, not Integer objects, so the answer is always "Sorry, don't have that". You're going to have to do something like going through the ArrayList yourself and asking if each Taxi in the list has that number.
+Pie Number of slices to send: Send
As I said, do you really have to use a List?
+Pie Number of slices to send: Send
Hi Guys,

Thanks for fixing up the look of my code, it does look a lot better and much easier to read although not sure exactly how to do it. I appreciate your replies to my question. We have only covered arrays and arraylists in class, although i understand there are other things out there such as sets, hashsets, linked lists etc that could possibly be used but unfortunately not at all familiar with how to use them.

The code is a very rough first draft that I was hoping to get working initially before trying to refactor with methods for the redundant bits. I am struggling with the OO stuff as we learned the "old way" first and only now have switched over to OO and it is a bit headwrecking, especially when the variables are private and you need to access them through the getters & setters.

I haven't been able to find any examples on the web that show arraylists being used in this fashion....with scanner, creating objects in this manner, and then accessing the arraylist objects to change one particular attribute of one particular object...perhaps someone knows if there is such an example.

I think I might need to use a for loop to access the arraylist ....so something like:





Would that be heading in the right direction or maybe an iterator?

Any advice most welcome.
+Pie Number of slices to send: Send
 

Peter McDonnell wrote: . . . I was hoping to get working initially before trying to refactor . . .

I hope you don’t really mean refactor. Creating code without a plan and refactoring it later is much more time‑consuming than getting it working first time.
If you mean enhance of augment, that is a good way to develop code.
+Pie Number of slices to send: Send
Yes that is what I meant.

Thanks Again.
+Pie Number of slices to send: Send
The get method should not take any parameters. It should return the plate number. Consider whether the plate number is a number, or is it like a house number. You should find a typical get method in the Java Tutorials.
If it is a number, the difference between 1 and 2 is the half the difference between 1 and 3.
If it is a house number, the difference between 1 and 2 is not half the difference between 1 and 3. If the plate numbers are like house numbers, they should be a different type from ints. If they are simply serial numbers, then they can be ints. I suspect they really are numbers and can be ints. The difference may seem pedantic, but it determines whether you can or cannot use the == operator or not. Remember that ints are primitives.
+Pie Number of slices to send: Send
Thanks very much. That is very informative. I basically needed to create a getTaxi method to iterate through the arraylist using a for loop and then use



to find the taxi object with the particular taxi plate number.

Many thanks for your kind advice.
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2008 times.
Similar Threads
Calling inner classes?
Booking bus tickets with each member only max of 4 seats
need help: I'm just not getting how this whole "private" / "public" class definition thing works.
Create a counter that increments once a second until a given number of seconds is reached
print invalid selction not working
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:25:55.