-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGettersAndSetters.java
More file actions
48 lines (40 loc) · 1.29 KB
/
Copy pathGettersAndSetters.java
File metadata and controls
48 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class UsingGetters{
private int day;
private String month;
private int year;
public void setDay(int day){
this.day = day;
}
public int getDay(){
return day;
}
public void setMonth(String month){
this.month = month;
}
public String getMonth(){
return month;
}
public void setYear(int year){
this.year = year;
}
public int getYear(){
return year;
}
}
class GettersAndSetters{
public static void main(String[] args){
UsingGetters now = new UsingGetters();
//setting the date
now.setDay(24);
now.setMonth("September");
now.setYear(2019);
System.out.println("The current month is " + now.getMonth() + ".\n");
System.out.println("In fact, it is " + now.getMonth() + " " + now.getDay() + ", " + now.getYear() + ".");
//using math to add to the day.
int tomorrow = now.getDay() + 1;
System.out.println("But tomorrow it will be " + now.getMonth() + " " + tomorrow + ", " + now.getYear() + ".");
//using a setter to change the day.
now.setDay(26);
System.out.println("The day after that, it will be " + now.getMonth() + " " + now.getDay() + ", " + now.getYear() + ".");
}
}