• Post Reply Bookmark Topic Watch Topic
  • New Topic

Milliseconds Class  RSS feed

 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, do you guys think there is a way to improve the math, because it seems to me that if i had 10 variables i had to do 9 subtractions.
I want to add that i'm allowed to only use Scanner class, and the basic java that i have learned to this point.

I would also like to hear a feedback about the following program in general. This is a project we are told to do.


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Consider adding a variables like


And you get the idea. Using these, you can build on them. And your code can use less of them in the more complex calculations.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Yes. Consider adding a variables like


And you get the idea. Using these, you can build on them. And your code can use less of them in the more complex calculations.


Do you mean changing some of mine? or adding?

but how will that make row 27 more elegant?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try that again starting from your code. You have:



What would this code look like if you had a variable that contained the value of DAYS*D_MS? You'd be able to make three lines a bit shorter (and have less duplicated code.)

What if you had a variable that contained that new variable - HRS*H_MS. You'd be able to make two lines shorter. What if you applied this technique to days/hrs/mins?
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Let me try that again starting from your code. You have:



What would this code look like if you had a variable that contained the value of DAYS*D_MS? You'd be able to make three lines a bit shorter (and have less duplicated code.)

What if you had a variable that contained that new variable - HRS*H_MS. You'd be able to make two lines shorter. What if you applied this technique to days/hrs/mins?


If i want to make a variable that contain DAYS*D_M i must add it after right? because DAYS variable depends on the input.
I mean, it's not those three that you have mentioned.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Let me try that again starting from your code. You have:



What would this code look like if you had a variable that contained the value of DAYS*D_MS? You'd be able to make three lines a bit shorter (and have less duplicated code.)

What if you had a variable that contained that new variable - HRS*H_MS. You'd be able to make two lines shorter. What if you applied this technique to days/hrs/mins?


I have rewritten the variables as you suggested, now should i add more variables for each multiplication?:

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should i add more variables for each multiplication?

I think you should consider what makes the code clearer and add variables as needed.  There is no hard and fast rule.

If i want to make a variable that contain DAYS*D_M i must add it after
DAYS = (long) ms / D_MS ;
right? 

That's correct.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you learned about the remainer operator in java yet?
That would look cleaner to me than all that subtraction.

Also, by convention, only constants should have names in all upper case.

So DAYS should probably be called days
and MS_IN_SEC like the other constants should be "public static final"

 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Have you learned about the remainer operator in java yet?
That would look cleaner to me than all that subtraction.

Also, by convention, only constants should have names in all upper case.

So DAYS should probably be called days
and MS_IN_SEC like the other constants should be "public static final"



the remainder? not yet, but can you give an example on how will it use less code?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:


thanks, much better indeed. i will surely use it on the next program.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!