Difference between revisions of "CSC212 Examples for Lecture 3"
(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...") |
(→Student Class) |
||
Line 4: | Line 4: | ||
<br /> | <br /> | ||
<source lang="java"> | <source lang="java"> | ||
+ | /** | ||
+ | * Student: A class implementing a student with a name and age. | ||
+ | */ | ||
public class Student { | public class Student { | ||
String name = ""; | String name = ""; | ||
int age = 0; | int age = 0; | ||
− | static int MAX = 5; | + | static int MAX = 5; // max number of students in array |
+ | /** | ||
+ | * Constructor: initializes both the name and age | ||
+ | * @param n: the name | ||
+ | * @param a: the age | ||
+ | */ | ||
Student( String n, int a ) { | Student( String n, int a ) { | ||
name = n; | name = n; | ||
Line 14: | Line 22: | ||
} | } | ||
+ | /** | ||
+ | * displays the name and age nicely formatted. | ||
+ | */ | ||
public void display() { | public void display() { | ||
System.out.println( | System.out.println( | ||
Line 20: | Line 31: | ||
} | } | ||
+ | /** | ||
+ | * main entry point. | ||
+ | * @param args | ||
+ | */ | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
+ | //--- array of students --- | ||
Student[] students = new Student[MAX]; | Student[] students = new Student[MAX]; | ||
+ | |||
+ | //--- Create MAX students and store in array students --- | ||
int i; | int i; | ||
char n; | char n; | ||
Line 27: | Line 45: | ||
students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string | students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string | ||
} | } | ||
− | for ( i=0; i<MAX; i++ ) | + | |
+ | //--- display all students --- | ||
+ | for ( i=0; i<MAX; i++ ) | ||
students[i].display(); | students[i].display(); | ||
− | |||
} | } | ||
} | } | ||
+ | |||
</source> | </source> |
Revision as of 10:18, 10 September 2014
--D. Thiebaut (talk) 11:14, 10 September 2014 (EDT)
Student Class
/**
* Student: A class implementing a student with a name and age.
*/
public class Student {
String name = "";
int age = 0;
static int MAX = 5; // max number of students in array
/**
* Constructor: initializes both the name and age
* @param n: the name
* @param a: the age
*/
Student( String n, int a ) {
name = n;
age = a;
}
/**
* displays the name and age nicely formatted.
*/
public void display() {
System.out.println(
String.format( "Student: %5s: %d years old",
name, age ) );
}
/**
* main entry point.
* @param args
*/
public static void main(String[] args) {
//--- array of students ---
Student[] students = new Student[MAX];
//--- Create MAX students and store in array students ---
int i;
char n;
for ( i=0, n='a'; i<MAX; n++, i++ ) {
students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string
}
//--- display all students ---
for ( i=0; i<MAX; i++ )
students[i].display();
}
}