Kendall Ponder

Ranch Hand
+ Follow
since Sep 11, 2014
Cows and Likes
Cows
Total received
4
In last 30 days
0
Total given
0
Likes
Total received
5
Received in last 30 days
0
Total given
124
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kendall Ponder

I have the following code (which I found on a post on this site at textfield struts2) which correctly displays the date in a text field.


but I need to have my format use a global format.date value which is stored in the global-messages.properties file so if the specs for the date change it can be changed in one place. I have tried various ways to replace the "MM/dd/yyyy" with format.date but haven't found one which worked. Any help would be appreciated. Thanks!
3 years ago
I agree with you about Enums. Ritchie Campbell worked a lot with me on just that and if you want to see the results on another thread go here.. I just got a new job about a month ago doing JavaEE programing so I haven't done much with the baseball program lately.
3 years ago

Henry Wong wrote:
Have you looked at the Java tutorials?

https://docs.oracle.com/javase/tutorial/

Henry


When I started learning Java I didn't find the tutorials very helpful. I think it makes a big difference where you are starting. I had never done OOP before and spent a lot of time on various free sites and learned the mechanics of Java without learning how OOP really worked. The links zm listed may be better than the ones I used. My biggest problem with most sites is a lack of ideas on practice programs. I think you have to write a lot of code to see how it works. My first big step was when I bought Sierra & Bates book Sierra & Bates to study for the exam. It does an excellent job of explaining OOP concepts. The second big step was when I asked for some help on some code on this site and it was promptly ripped to shreds and then people helped me write better code.

I guess my point is if I had to do it over again I would invest in a good book (based on my programming background) and work my way through it and use this site when I got stuck.
3 years ago

Not sure why this result is so different. Is it because every time I call readline(), it moves the file cursor up 1?


I think this is correct. Your readLine in the conditional of the while returns a line and moves the cursor. Just because you don't put the result in a variable doesn't change that.
3 years ago
I put your code in a class called Temp so you could see if I am understanding your question correctly. I ran the following code:

This gives me an output of: nulltopbottom

If you change line 2 from

to
you get an output of: topbottom which is what I think you want and I think that is what Henry was referring to.

You might want to consider writing a separate method for the test as follows. It gets rid of your do-nothing branch. I think naming the test makes it easier to understand what you are trying to accomplish (I learned that on this forum).


3 years ago
The following also works, but there may be a reason not to do it this way I don't know about.
3 years ago
Changing your split line as shown below would have fixed the problem like Henry said.

3 years ago

Greg Charles wrote:Yes, you did. Sorry, Kendall. My admonition against downloading pirated tech books wasn't aimed at you specifically. I just didn't want to talk about pirating without mentioning reasons not to do it.


No problem. I have always thought copying software/movies/etc without the writers permission was the same as shoplifting. I appreciate the feedback. I have deleted the PDF copy of the book.
3 years ago

Greg Charles wrote:Unless the download comes directly from the publisher's site, it's safe to assume that it's a pirated copy. Also this book is still for sale, which is another strong indicator it isn't also being given away free. Remember, downloading a pirated PDF is essentially the same as walking into a bookstore and stealing the book. Authors, particularly technical book authors, work extremely hard to write these books and deserve to be compensated for their work.


If I didn't understand why you shouldn't use a pirated copy I wouldn't have asked the question. I did not understand why they would make the PDF form available (although their are some books which offer free e-versions but sell hard copies, not sure I understand why). Thanks for the input. I suspected it was pirated but couldn't find any info when I googled the question.
4 years ago
I did not know where to post this question but I thought this would be the best place. I did a search for the book Head First Design Patterns and found free PDF downloads. My concern is if these are pirated copies. If the authors/publishers have made it available in PDF form I will use it, but if not I won't. Thanks for any advice.
4 years ago

Dave Tolls wrote:

Kendall Ponder wrote:
In my program I need to be able to copy a baseball team and modify it so I now have two separate teams. The baseball team is mainly a list of players so I need the second team to have the same information the first team had, but not the same objects so I can change a player stat in the second team without affecting the first team.



Ah right.
Just checking there wasn't some odd requirement that might have another solution.

How many different classes of player do you have for a team?
If it's only a couple then I'd stick the clone bit for a Player into some factory method that simply did the work of deciding what class to create. If that starts to be a problem to maintain then you can always change the method to do something fancier.


My issue is I wrote some code to store teams in a file. Then I needed to store the results of a simulation. Then I realized I was probably going to have lots of different classes of simulation results and I wanted to use the same code to do file I/O with all of them. I also realized I needed to be able to add a new simulation result class and not have to insert code all over the place to make it work. So as Sheriff Ritchie taught me I set up the following enum where each StorageObject enum has the name of the storage file for that simulation result attached to it.


My StorageAndRetrieval class handles file I/O and now includes the following code:

So I pass the StorageObject enum to the StorageAndRetrieval class when I instantiate it. When I just had teams to store I had list of teams in my main menu that I used to store all of the saved teams in the file when the program started and then I wrote the updated list of teams back to the file when the program ended. When I started adding other objects I needed to store in a file I didn't want to have separate code for each list so I made a list of lists which contained Storable items. I then wanted to use the same methods to manage all of the lists which led to my original question. So my solution looks like this (I only included the code related to my question):

The loadAllData() method runs at the start and the saveAllStorableLists() runs at the end and they do the file IO. The get and set methods allows other classes to get independent copies of the list they want, modify the list and then update it. They use the overridden deepCopy() method which was what my original question was about. Now when I add a new simulation result all I have to do is have it implement the Storable interface and add a enum constant to the StorableObject enum class and all of the file IO will be taken care of automatically. This is probably way more info than anyone wanted but it does show the advantage of using enums. The Storable interface is
and the rest of the StorageAndRetrieval methods are below just in case anyone wants to look at them.
4 years ago
I did a google search on constructors and polymorphism before I asked this question but after I asked it I tried googling copy constructors and polymorphism (asking the question made me think of googling copy constructors) which led me to the following link SO polymorphic cloning. I think the code below will do what I want. Sorry I didn't find it before I asked the question but I still appreciate all of your feedback and learned from it. If anyone sees a problem with this code I would love to hear it.


4 years ago

Of course whichever method you choose, once other code has a copy of a List it is the work of a moment to create a new List from that copy, or to create a Stream from it, and you now have data which you can manipulate to your heart's content


This would work but doesn't that violate encapsulation? Don't I want to only allow access to data through getters and setters? If my getTeam gives a copy of the list of players then the user has direct access to the players on the team. I thought you were supposed to avoid that.
4 years ago

Dave Tolls wrote:I think it's using reflection to call the copy constructor, so handling the "I don't know what class of object I am creating".

That aside, Kendall, why do you think you need to create copies of the contents of the List?
Why do you think you nee to create a new List?


In my program I need to be able to copy a baseball team and modify it so I now have two separate teams. The baseball team is mainly a list of players so I need the second team to have the same information the first team had, but not the same objects so I can change a player stat in the second team without affecting the first team.
4 years ago
The code below doesn't accomplish anything but is intended to illustrate the problem with my real code. My issue is I need getList() to return a copy of list but I need the copy of the list to contain copies of the objects in the list. The code below will return a copy of the original list but both lists will hold the same objects.


If the list only contained Parent objects I could accomplish what I want by writing getList as follows

But I want to use getList() to return a copy of the list if it contains Parent or Child objects. I can let getList() know what kind of objects are in the list when I call it but I don't see how that helps me make independent copies of the objects because I don't think you can use polymorphism with a copy constructor. I could use a switch() statement but that seems to defeat the purpose of reusing similar code. Any ideas on the best way to accomplish this would be appreciated.
4 years ago