Print Page Print Page

CH9 – Characters

  1. Create a new class named Characters.
  2. Create a main method and add the following code.
public class Characters {
  public static void main(String[] args){
    String fruit = args[0];
    char letter = fruit.charAt(0);
    if(letter == 'a'){
      System.out.println("it's an a");
    }
    else {
      System.out.println("character: " + letter);
    }
  }
}
  1. Compile and run the program. First, using banana as the argument and then apple.
$ javac Characters.java
$ java Characters banana
character: b
$ java Characters apple
it's an a
  1. Add the following lines to the end of main.
System.out.println("Roman alphabet: ");
  for(char c = 'A'; c <= 'Z';c++){
    System.out.print(c + " ");
  }
  System.out.println("");
  System.out.println("Greek alphabet: ");
  for(int i = 913; i <= 937; i++){
    System.out.print((char)i + " ");
  }
  System.out.println();
}
  1. Compile and run the program.
$ javac Characters.java
$ java Characters apple
it's an a
Roman alphabet: 
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
Greek alphabet: 
Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ ΢ Σ Τ Υ Φ Χ Ψ Ω