Printing Things out Slowly
public class printIt
{
public static void main (String args [])
{
new printIt ();
}
public printIt ()
{
printSlow ("Hello! \n");
printSlow ("How are you?");
printSlow ("I am fine.");
System.out.println ("Just moving a little slowly today.");
printSlow ("Got to go now. \n");
printSlow ("G O O D B Y E ");
}
public void printSlow (String s)
{
for (int i = 0 ; i < s.length () ; i++)
{
System.out.print ("" + s.charAt (i));
//sleep for a bit after printing a letter
try
{
Thread.sleep (100);
}
catch (InterruptedException m)
{
;
}
}
System.out.println ();
}
} //class!
Instructions
- Paste the red
section into your code under your constructor, but above the class closing
bracket.
- Watch the class
closing bracket! Students always plunk the printSlow method in anywhere
and call me over to ask why it isn't working.
- The red section is called a method.
Specifically, it is the printSlow method. We can call it inside our constructor
to print things slowly for us.
- Instead of System.out.println,
just put printSlow