Java Reference
In-Depth Information
Display 4.4
Methods with Parameters
(part 1 of 2)
1
import
java.util.Scanner;
The significance of the modifier
private
is
discussed in the subsection “
public
and
private
Modifiers” in Section 4.2.
2
public
class
DateThirdTry
3{
4
private
String month;
5
private
int
day;
6
private
int
year; //a four digit number.
7
public
void
setDate(
int
newMonth,
int
newDay,
int
newYear)
8
{
9
month = monthString(newMonth);
10
day = newDay;
11
year = newYear;
12
}
13
public
String monthString(
int
monthNumber)
14
{
15
switch
(monthNumber)
The method
setDate
has an
int
parameter for the
month, even though the month instance variable is
of type
String
. The method
setDate
converts the
month
int
value to a string with a call to the
method
monthString
.
16
{
17
case
1:
18
return
"January";
19
case
2:
20
return
"February";
21
case
3:
22
return
"March";
23
case
4:
24
return
"April";
25
case
5:
26
return
"May";
27
case
6:
This is the file
DateThirdTry.java
.
28
return
"June";
29
case
7:
30
return
"July";
31
case
8:
32
return
"August";
33
case
9:
34
return
"September";
35
case
10:
36
return
"October";
37
case
11:
38
return
"November";
39
case
12:
40
return
"December";