CSC212 Examples for Lecture 3

From dftwiki3
Revision as of 10:14, 10 September 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Student Class= <br /> <source lang="java"> public class Student { String name = ""; int age = 0; static int MAX = 5; Student( String n, int a ) { name = n...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--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();
		}
	}

}