-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
43 lines (39 loc) · 1003 Bytes
/
Person.java
File metadata and controls
43 lines (39 loc) · 1003 Bytes
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
import java.lang.*;
import java.util.Scanner;
public class Person {
int age;
public Person(int initialAge) {
if (initialAge < 0) {
age = 0;
System.out.println("Age is not valid, setting age to 0.");
} else {
age = initialAge;
}
}
public void am_I_Old() {
String ans;
if (age < 13) {
ans = "You are young.";
} else if (age >= 13 && age < 18) {
ans = "You are a teenager.";
} else {
ans = "You are old.";
}
System.out.println(ans);
}
public void yearPasses() {
age += 1;
}
}
class Test
{
public static void main(String[] args) {
System.out.println("Enter age");
Scanner sc=new Scanner(System.in);
int age=sc.nextInt();
Person obj=new Person(age);
obj.am_I_Old();
obj.yearPasses();
System.out.println(obj.age);
}
}