1 2 3 /* Demo for: Classes, Inheritance

advertisement
1 /* Demo for: Classes, Inheritance
2
Student.java
3
This class defines a simple template for student objects */
4
5 public class Student
6 {
7
//***** Static Variable *****//
8
static int studentCount;
9
10
//***** Instance Variables *****//
11
private String untId;
12
private String name;
13
private char gender;
14
private float gpa;
15
16
// ***** Constructor *****//
17
// It doesn't make sense to accept the default initial
18
// values for these instance variables because each student
19
// would have the name "null", ID "null" and is of the ' '
20
// gender. Therefore, a non-default constructor method is
21
// created below, instead of using the default constructor
22
// that Java runs implicitly for classes without an explicitly
23
// created constructor method.
24
public Student(String i, String n, char g, float gpa)
25
{
26
untId = i;
27
name = n;
28
gender = g;
29
this.gpa = gpa;
30
}
31
32
// Like regular methods, a constructor method can be overloaded.
33
// If warranted by software specs, we can create multiple version of the
34
// constructor method. Below, we create a version that emulates the
35
// "default" or implicit constructor that Java will run if no construtor
36
// method is defined. (As an aside, if a constructor is created, Java will
37
// assume that we want to be control and will not run the "default"
38
// constructor any more.)
39
public Student()
40
{
41
}
42
43
// Another version of the constructor. This version only initializes the ID
44
// and name. A realistic business scenario may be that some demographic data
45
// of the customers are optional. So it may be that the only mandatory fields
46
// of input are ID and name. In this case, we only initialize ID and name.
47
public Student(String i, String n)
E:/BCIS 3680/07a-inheritance/src/Student.java
1 of 3
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
untId = i;
name = n;
}
// ***** Setters and Getters *****//
// For UNT ID //
public void setUNTID(String id)
{
untId = id;
}
public String getUNTID()
{
return untId;
}
// For Name //
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
// For Gender //
public void setGender(char gender)
{
this.gender = gender;
}
public char getGender()
{
return gender;
}
// For Age //
public void setGPA(float gpa)
{
this.gpa = gpa;
}
public float getGPA()
E:/BCIS 3680/07a-inheritance/src/Student.java
2 of 3
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 }
{
return gpa;
}
// ***** Static Methods *****//
static void addToCount()
{
studentCount++;
}
static int readStudentCount()
{
return studentCount;
}
// ***** Instance Method *****//
public void retrieveInfo()
{
String msg = "================================\n";
msg += "Generated in Student Class\n";
msg += untId + "\t" + name + "\t" + gpa + "\n";
msg += "================================";
System.out.println(msg);
}
E:/BCIS 3680/07a-inheritance/src/Student.java
3 of 3
Download