1.5 Exercises

1. A Simple Application

Create a file named hello.java with the flawed Java code shown here:
class Hi {
  public static void main (string[] args) {
    System.out.println("Hi "+ args[0])
  }
}
  1. Adapt the source code so that it can be compiled without any difficulty.
  2. See how the Java program executes when it is called in the following four ways:
    java Hi everybody
    java hello everybody
    java Hi
    java Hi Ana and Bob
    java Hi "Ana and Bob"
    


2. Hello Applet

  1. Write a Hello applet that welcomes the person(s) mentioned in a parameter in the Web page that includes your applet.
  2. Write a Hello applet that welcomes the person(s) mentioned in a text field in the Web page that includes your applet.



3. A Button

One of the classes defined in the AWT package is Button. To create a new button, say with name b and with the text push me, use:
b = new Button("push me");
The method setLabel can be used to change the text on the button. For example, to change the text from push me into thank you use:
b.setLabel("thank you");
Use the last example of the echo applet to write a Java applet with a "push me" button, i.e., a button that changes its label from push me to thank you when pressed. It should look like:



4. Traffic Light (optional exercise)

Write a "Traffic Light" applet with a "switch color" button that looks like:

To work with the color red, yellow, and green, you have to know the following: