-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses.java
More file actions
55 lines (39 loc) · 1.23 KB
/
Copy pathClasses.java
File metadata and controls
55 lines (39 loc) · 1.23 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
package oop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* Classes and objects: attributes, constructor, methods, this.
*/
class Person {
private static final Logger logger = LoggerFactory.getLogger(Person.class);
String name;
int age;
boolean adult;
// Constructor to initialize the name and age attributes
Person(String name, int age) {
this.name = name;
this.age = age;
this.adult = age >= 18;
}
// Method to introduce the person which prints their name and age
void introduceOneself() {
logger.info("Hello, I am {} and I am {} years old", name, age);
if(adult) logger.info("I am an adult");
else logger.info("I am not an adult");
}
// Method to celebrate a birthday which increases the age
void haveBirthday() {
age++;
}
}
public class Classes {
public static void main(){
final Logger logger = LoggerFactory.getLogger(Classes.class);
Person p1 = new Person("exampleName1", 28);
Person p2 = new Person("exampleName2", 10);
p1.introduceOneself();
p2.introduceOneself();
p1.haveBirthday();
logger.info("{} is now {} years old.", p1.name, p1.age);
}
}