Programming an Animated Hourglass Display in Java
by A_Maile in Design > Digital Graphics
108 Views, 0 Favorites, 0 Comments
Programming an Animated Hourglass Display in Java
Introductory Java programming project that uses the basic skills I have been learning in my AP Computer Science A class recently! Skills/control structures used:
- Static methods
- For loops
- While loops
- Screen clearing command
- “Wait” x seconds function
If you are new to Java, learn with me as I build this project!
Supplies
- Any sandbox/IDE capable of running Java (I use CodeHS because I am familiar with it, but websites like https://www.online-java.com are great too!)
- Only one file needed, but this is a stylistic choice
Pseudo Code
Pseudocode is used to plan out the structure of what you want a program to look like. I include basic features like what I want to happen in a sequential order, different loop types with steps underneath them, declaration of variables, and where to put them (scope!)
TIPS:
- Your pseudocode doesn't have to be perfect. Get the general structure down (along with any questions and notes you have for your later self)
- This outline can turn into your documentation comments later!
Create Your Hourglass Shape to Print
- Create the hourglass shape using forward and backwards slashes (\ /) for the walls,
- Use asterisks (*) to represent sand, only filling the top triangle completely
NOTE:
- It is important that you use exactly 30 asterisks for our program, since this is a 60 second timer, they will change at a rate of 1 * per 2 seconds!
Animate Stars
- Create timer
- Establish the number of stars
- While loop that:
- Clears the screen
- Prints out updated timer
- Prints hourglass
- On every even time, removes one * from the top triangle and adds it to the bottom
- Updates timer and totalStar variables.
- Wait 1 second
Notes:
Use lines:
to clear the screen, so the print statements don't stack on top of eachother 60 times.
Use line
to wait 1 second before moving on.
Methods Called in the Above Step
- String removeStar(String line) --> Remove a * from the top of the hourglass
- String addStar(String line) --> Adds a * to the bottom of the hourglass
- boolean hasSpace(String line) --> checks for space inside the houglass before moving up/down a line
Print Final Line
Created because the if statement breaks when time is 0, and the final frame still needs to be printed.