Java Reference
In-Depth Information
1
// Fig. 4.4: Student.java
2
// Student class that stores a student name and average.
3
public class
Student
4
{
5
private
String name;
6
private double
average;
7
8
// constructor initializes instance variables
9
public
Student(String name,
double
average)
10
{
11
this
.name = name;
12
13
// validate that average is > 0.0 and <= 100.0; otherwise,
14
// keep instance variable average's default value (0.0)
15
if
(average >
0.0
)
if
(average <=
100.0
)
this
.average = average;
// assign to instance variable
16
17
18
}
19
20
// sets the Student's name
21
public void
setName(String name)
22
{
23
this
.name = name;
24
}
25
26
// retrieves the Student's name
27
public
String getName()
28
{
29
return
name;
30
}
31
32
// sets the Student's average
33
public void
setAverage(
double
studentAverage)
34
{
35
// validate that average is > 0.0 and <= 100.0; otherwise,
36
// keep instance variable average's current value
37
if
(average >
0.0
)
if
(average <=
100.0
)
this
.average = average;
// assign to instance variable
38
39
40
}
41
42
// retrieves the Student's average
43
public double
getAverage()
44
{
45
return
average;
46
}
47
48
// determines and returns the Student's letter grade
49
public
String getLetterGrade()
50
{
51
String letterGrade =
""
;
// initialized to empty String
52
Fig. 4.4
|
Student
class that stores a student name and average. (Part 1 of 2.)