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
*/
10
class
Student {
11
String name;
12
int
id;
13
}
14
15
public
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
}
question1a
Definition
question1a.java:15
question1a.main
static void main(String[] args)
Definition
question1a.java:16
pastpaper
2014_2
question1a.java
Generated on Sat Dec 7 2024 17:10:09 for COMP2396 by
1.12.0