• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java addition of Time  RSS feed

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

to stres the major fact so far is that I am a beginner Anyways since I work as a golf staff I would like to create application which would tell me the playing time and the time when players need to reach certain playing field/hole.

my ideas is to make a program which would ask the user to input their starting time and than select the hole number, where the end result would be amount of actuall time.

I have been having hard time to figure out howe to properly structure the input conversion so it is recognized as a time (Exampke: 10:15). Do I need to use the calendar method in Java or ?

thanks for ideas and help

regards from Slovenia
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch,

If you are using Java 8, I suggest you read the tutorial on the new Date/Time system http://docs.oracle.com/javase/tutorial/datetime/TOC.html.
If you are using an earlier version of Java then you may be better of using Joda Time although it is perfectly possible and acceptable to do it using the pre version 8 Date Time system.

If you let us known what version of Java you are using and what you are actually doing with the times ie are you adding, subtracting times, will they span more than one day etc. it will be easier to give advice.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lovro Bajc wrote:Anyways since I work as a golf staff I would like to create application which would tell me the playing time and the time when players need to reach certain playing field/hole.

Well, apart from Tony's good advice, I suspect it's going to depend very much on how sophisticated you want to make it. I suspect you could probably enter in expected times for scratch golfers for each hole, but the actual time is likely to depend very much on things like par, yardage and difficulty; and also the player's own handicap.

My suggestion would be to write out, in English (or your native language), exactly how you intend to calculate these timings before you write a single line of code - and therefore, long before you have to worry about how to add or display times.

Not that it's something you shouldn't do when the time comes.

Winston

PS: Welcome to JavaRanch.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well initially paper version of what we have now looks like this:



so if the user would put in the starting time at 10:15 and current hole number 3, he would get the result, that he needs to finish the 3 hole at 10:50.
I know the IF functiuons can be used for selection and calculation process, the only issue what I have is time addition.

based on the rating of the course and its structure game should not take longer than 4 hours 20 minutes.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lovro Bajc wrote:based on the rating of the course and its structure game should not take longer than 4 hours 20 minutes.

You clearly haven't see me playing golf
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Lovro Bajc wrote:based on the rating of the course and its structure game should not take longer than 4 hours 20 minutes.

You clearly haven't see me playing golf


hahha, we want to raise awareness and their time sence, because we are quite fully booked
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be an idea then to have your app output the expected start times for each hole given an initial start time for playing 9/18 holes rather than doing it on a hole by hole basis.
I think you should also consider the points raised by Winston for example allowing more time for those less gifted golfers, allowing more time for a 4 as opposed to a pair etc.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
If you put code tags round your times, and maybe change the word java to text, it preseves the spacing; I did it on your post and I hope it looks better

Maybe you should have something likeMaybe you would need 19 times for 18 holes and no “end” times. 19 is a fencepost number.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, for ideas, I will try to figure something out, and I mostlikely will be back with questions since we have also 4 golf marshalls working at the course it would be also as a application to speed up their process.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its me again. Before you start to check anything let me give you a hint I am a total beginner in programming trying to conquer the world (joke). Code posted bellow is done up to hole 3 with no conversion back to actual time.
I believe everything can be done easier way as suggested in previous posts with Joda time, however I have no clue how to use it as of now.
Any tips comments ?




Thank You very much
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Tags, please, not quote. I have changed them. That awkward code shows how much better it would be to use a data structure for the numbers.
You are also putting far too much code in the main method.

Install Java8 and use the new time package.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Code Tags, please, not quote. I have changed them. That awkward code shows how much better it would be to use a data structure for the numbers.
You are also putting far too much code in the main method.

Install Java8 and use the new time package.


I agree with you, but taking into consideration that I am a total beginner, with no previous coding experience and different schooling background, your help is even more appreciates.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 8, you'll want to use the class java.time.LocalTime, which represents a time of day. Maybe you also want to use the class java.time.Duration, which represents an amount of time (a number of hours, minutes, seconds etc.).

Lookup those classes in the API docs of package java.time.

Simple example:

 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something to consider...what if you were doing something where the number of holes was 100? or 1000? or 10,000?

explicitly coding something out X times is a sure sign you are doing something wrong. all programming languages have loops and collections. So instead of explicitly writing out the code for all 18 holes, you want to put stuff into a collection - perhaps an array - and then write a loop that will process them all. That way, if in the future you requirements change and you have to process exactly nine holes, or 36, you can just change how big your array is and the loop takes care of itself.

If you had an array, you could simplify your code starting at line 48. You could write a loop that iterates through all the holes up to the holeNo to add up all the times. something like:


Next...

This line (and the other 17 like it) makes me shudder:



a constant variable named "NO10" to me sounds like "Number ten" - yet it is set to a value of 11. your other ones are similarly confusing. Remember that at some point, you will be deep in your code, and will come across "NO17" and won't have easy access to the above lines, and if you are like me, you'll think "NO17 must be equal to 17".

If this is supposed to represent the time allowed for hole 17, then name it something like that:
final int TIME_ALLOWED_HOLE_10 = 11;

Next..You should perhaps consider learning to create Objects, since you are writing in an OO language. Perhaps you could create a Hole object that has several pieces - which number it is, how long you can take on it, etc.
 
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never played golf. I do not know much about the holes and time and rules of the game. I wish i have played to understand and comment on this post
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:I have never played golf. I do not know much about the holes and time and rules of the game. I wish i have played to understand and comment on this post


You are not missing anything In our local golf club we have calculated the estimated playing time per hole in order to keep the game at reasonable speed as shown bellow:



My idea is to create program which would shorten the calculation process. Golf staff would enter the starting time, select the hole number and receive a desirable finishing time for that hole.

Example:
Starting time = 10:00
Hole number = 2
End time = 10:22

 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:something to consider...what if you were doing something where the number of holes was 100? or 1000? or 10,000? ...


I have had a plan to take a look also at this things, when i will progress throughout the books.

fred rosenberger wrote:
Next...

This line (and the other 17 like it) makes me shudder:



a constant variable named "NO10" to me sounds like "Number ten" - yet it is set to a value of 11. your other ones are similarly confusing. Remember that at some point, you will be deep in your code, and will come across "NO17" and won't have easy access to the above lines, and if you are like me, you'll think "NO17 must be equal to 17".

If this is supposed to represent the time allowed for hole 17, then name it something like that:
final int TIME_ALLOWED_HOLE_10 = 11;

Next..You should perhaps consider learning to create Objects, since you are writing in an OO language. Perhaps you could create a Hole object that has several pieces - which number it is, how long you can take on it, etc.


After all of your notes, I decided to skip the programming part and learn chapter by chapter and make changes on the fly.

thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!