-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessonSeven.java
More file actions
68 lines (58 loc) · 2.18 KB
/
LessonSeven.java
File metadata and controls
68 lines (58 loc) · 2.18 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package Lesson07;
import logic.Lesson;
public class LessonSeven {
public void theLesson() {
// Here we create a bunch of Person objects.
Person carl = new Person("Carl", 5, 'c');
Person jack = new Person("Jack", 15, '9');
Person sam = new Person("Sam", 12, 'c');
Person carly = new Person("Carly", 6, '9');
Person mike = new Person("Mike", 33, 'c');
Person sarah = new Person("Sarah", 115, '9');
Person jill = new Person("Jill", 75, 'c');
Person matt = new Person("Matt", 96, '9');
Person tony = new Person("Tony", 12, 'c');
Person mark = new Person("Mark", 5, '9');
// Here we assign a bunch of Person objects some friends.
carl.setFriend(sam);
sarah.setFriend(mike);
tony.setFriend(jack);
matt.setFriend(tony);
jill.setFriend(carly);
mark.setFriend(sarah);
// Here we have a bunch of Person objects speak and speak to some other objects.
carl.speak();
sarah.speakTo(jack);
matt.speakTo(tony);
tony.speakTo(matt);
sarah.speakTo(mike);
mark.speakTo(sarah);
}
public class Person {
private String name;
private int age;
private char favoriteLetter;
private Person friend;
public Person(String personsName, int personsAge, char favoriteLetter) {
name = personsName;
this.age = personsAge;
this.favoriteLetter = favoriteLetter;
}
public String getName() {
return this.name;
}
public void setFriend(Person newFriend) {
this.friend = newFriend;
}
public void speak() {
System.out.printf("Hi, my name is %s! I am %d years old and my favorite letter is: %s\n", this.name, this.age, this.favoriteLetter);
}
public void speakTo(Person person) {
if (this.friend != null && this.friend.equals(person)) {
System.out.printf("Hello %s! You are my friend and my name is %s! I am %d years old and my favorite letter is: %s\n", person.getName(), this.name, this.age, this.favoriteLetter);
} else {
System.out.printf("Hello %s, my name is %s! I am %d years old and my favorite letter is: %s\n", person.getName(), this.name, this.age, this.favoriteLetter);
}
}
}
}