Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

The Count

Source: TheCount.java

Seen this before. The TextArea is defined by its rows (10) and columns/character spaces (20).

 

 

 

 

 

Typical while loop setup:

- initialize the counter

- test the counter

- increment the counter

Surprising how many people forget to increment the counter!

TheCount.java

 

import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.*;
 /**
 * Runs an arbitrary loop, intro to while loops example
 *
 * @author Mr J
 * @version 020204
 */

 public class TheCount extends Applet implements ActionListener
 {
   // awt objects
   private TextArea display = new TextArea(10, 20);
   private Button pressMe = new Button("Press me!");
   private Label messages = new Label("Any messages will appear here");

 /**
   * Add objects to the Applet
   */

   public void init()
   {
     add(display);
     add(pressMe);
     add(messages);
     pressMe.addActionListener(this);
   }
   /**
   *
   * @param e carries details about the event that occurred
   */

   public void actionPerformed(ActionEvent e)
   {
     // run the loop
     int count = 0;
     messages.setText("I am The Count, I will count to 10!");
     while (count <= 10)
     {
       display.append("" + count + "\n");
       count = count + 1;
     }
   }
}

Related: [ Java Core | previous repetition | next:Hemper methods ]

Dedicated to the character from Sesame Street who helped my kids learn to count.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009