-
Notifications
You must be signed in to change notification settings - Fork 0
/
G1 Gist of object.java
111 lines (84 loc) · 2.5 KB
/
G1 Gist of object.java
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
47
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Arrays;
// 171. work book part 6 (The world of objects)
/**
* App
*/
public class App {
public static void main(String[] args) {
Person person = new Person("Name", "Nationality", "Birth Date", 2);
if (person.applyPassport() == true) {
person.setPassport();
}
System.out.println("Name: " + person.getName() + "\n"
+ "Nationality: " + person.getNatio() + "\n"
+ "Date of Birth: " + person.getDOB() + "\n"
+ "Seat Number: " + person.getSeatNumber() + "\n"
+ "Passport: " + Arrays.toString(person.getPassport()) + "\n");
}
}
// ******************
import java.util.Arrays;
public class Person {
private String name;
private String nationality;
private String dateOfBirth;
private String[] passport;
private int seatNumber;
// constructor
public Person(String name,String natio,String DOB,int seatNumber){
this.name = name;
this.nationality = natio;
this.dateOfBirth = DOB;
this.seatNumber = seatNumber;
this.passport = new String[3];
}
// getter
public String getName(){
return this.name;
}
public String getNatio(){
return this.nationality;
}
public String getDOB(){
return this.dateOfBirth;
}
public String[] getPassport(){
return Arrays.copyOf(this.passport, this.passport.length);
}
public int getSeatNumber(){
return this.seatNumber;
}
// copy constructor
public Person(Person source){
this.name = source.name;
this.dateOfBirth = source.dateOfBirth;
this.seatNumber = source.seatNumber;
this.nationality = source.nationality;
this.passport = Arrays.copyOf(source.passport, source.passport.length);
}
// setter
public void setPassport(){
this.passport = new String[]{this.name,this.nationality,this.dateOfBirth};
}
public void setName(String name) {
this.name = name;
}
public void setDOB(String dob) {
this.dateOfBirth = dob;
}
public void setNatio(String natio) {
this.nationality = natio;
}
public void setSeatNum(int seat) {
this.seatNumber = seat;
}
// apply passport
public boolean applyPassport(){
int draw = (int)Math.random()*2;
return (draw > 0 ? true : false);
}
// choose seat
public void chooseSeat(){
this.seatNumber = (int)(Math.random()*11 + 1);
}
}