COMP2396
Loading...
Searching...
No Matches
question1a.java
Go to the documentation of this file.
1// 2014 Dec COMP2396 Question 1a)
2/* The problem is, the array of students were initialized,
3* But the individual student objects were not initialized.
4* A fix would be add "cs0396[0] = new Student();" and "cs0396[1] = new Student();"
5* before assigning values to the student objects.
6* The output would be
7* Kevin 12345
8* Haoyuan 67890
9 */
10class Student {
11 String name;
12 int id;
13}
14
15public class question1a {
16 public static void main(String[] args) {
17 // Initialize the student array
18 Student[] cs0396 = new Student[2];
19
20 // Initialize individual student objects
21 cs0396[0] = new Student();
22 cs0396[1] = new Student();
23
24 cs0396[0].name = "Kevin";
25 cs0396[0].id = 12345;
26 cs0396[1].name = "Haoyuan";
27 cs0396[1].id = 67890;
28
29 for (int i = 0; i < 2; ++i) {
30 System.out.println(cs0396[i].name + " " + cs0396[i].id);
31 }
32 }
33}
static void main(String[] args)