CSC212 Examples for Lecture 3
--D. Thiebaut (talk) 11:14, 10 September 2014 (EDT)
Student Class
public class Student {
String name = "";
int age = 0;
static int MAX = 5;
Student( String n, int a ) {
name = n;
age = a;
}
public void display() {
System.out.println(
String.format( "Student: %5s: %d years old",
name, age ) );
}
public static void main(String[] args) {
Student[] students = new Student[MAX];
int i;
char n;
for ( i=0, n='a'; i<MAX; n++, i++ ) {
students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string
}
for ( i=0; i<MAX; i++ ) {
students[i].display();
}
}
}