• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scrolling String - Change of Speed

 
Aldoria Robichaud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I have written a script for my Computer Science 120 course. The only problem is that I don't understand why the first string moves fast, and then the other four do not move as quickly. I thought it had to do with the runner.sleep(time); method, so I threw that into every scrolling statement. However, this didn't solve the problem or make the speed identical for all scrolling strings.

I would really appreciate it if someone could take a look at this and help me figure out why it isn't working :p I'm not looking for an answer, I would prefer an explanation

FiveSongs.java


FiveSongs.html


Thanks again

PS. The boolean checkEnd is there for a later part of the script that I need to do :p
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've rewritten it somewhat (for fun ) and it still runs slow. I think so many redraws are tiresome for the procesor (CPU/GPU). By the way, great song selection!

Code:


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We encourage our users to do their own homework (please see DoYourOwnHomework), and we're NotACodeMill. As such, we strive not to provide ready-made answers, but rather provide hints, suggestions, and so on. Particularly when it's explicitly stated this is for a course.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm Sorry I didn't know that.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem--no apologies necessary. Maybe posting hints regarding some of the cleanup you did would be helpful--it seemed easier to deal with than the original version.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aldoria Robichaud,

it seems that your code somewhat repeats itself. For example the part(s) that move your strings. Is there a way to do that in a different way, with less code, maybe with a flow control statement?

You are using several variables in a connected kind of a way, as if they were a whole. Java is object oriented, try thinking with objects and properties.

Keep in mind, that sleep(long) method that you're invoking is static. That means that it is not invoked on your instance of Thread (the runner variable) but on the Thread class itself. Hope that made sense It is good practice, as far as I understand, to invoke that kind of methods on a 'class level' ie. Thread.sleep(20), result is the same.

Also the thread goes to sleep for about the time you provide, results may vary. You can expect the thread to sleep for 20ms or more.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect :) Thanks for following up!
 
Aldoria Robichaud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys !
Thanks for the help. I got my full applet to work now

And Vanyavchich,
I don't even know what a flow control statement is.. we haven't learned about them yet.

Also, I figured out why the scrolling kept getting slower and slower. It was, in fact, due to all of my 'spammed and unnecessary'

Anyways, I have completed and submitted my final assignment. Here is my the final submitted code:


Now I was just wondering if someone could give me a short example of how this would be done using Objects as Vanyavchich had mentioned. Instead of doing it with all 5 strings, there could be less. I just want to see a better programming style that accomplishes the same thing so I can compare and see the differences, and hopefully it will help me improve myself

Thanks for all the help!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aldoria Robichaud wrote:I don't even know what a flow control statement is.. we haven't learned about them yet.

Sure you have, you just don't realize it: flow control statements.
Now I was just wondering if someone could give me a short example of how this would be done using Objects as Vanyavchich had mentioned. Instead of doing it with all 5 strings, there could be less. I just want to see a better programming style that accomplishes the same thing so I can compare and see the differences, and hopefully it will help me improve myself

Well, the first thing I'd do is start by looking to see where the repetition is: the first thing I notice is that there are a large number of statements that look exactly alike, but with slightly different values. I take aim at that kind of repetition first, to nail down the essence of what I'm trying to do.

Then I try to isolate *where* that functionality belongs. Martin's solution involved moving song information (title, position, color, visibility) into a Song class, and creating a method that would take a song and move it. Maybe try sketching out something doing that--even in pseudocode. Once you have that, I can repost his answer for comparison.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vanyavchich wrote:
Thank you for noticing, David.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic