-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.java
More file actions
35 lines (32 loc) · 799 Bytes
/
constructor.java
File metadata and controls
35 lines (32 loc) · 799 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
import java.io.*;
class Person
{
private String name;
private int age;
// parameterized Constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
}
void check()
{
if(age <= 30) System.out.println(name + " is young");
else if(age <=50) System.out.println(name + " is middle-aged");
else System.out.println(name + " is old");
}
}
class Demo
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name: ");
// Again declare with the same name and age as now we are in another class
String name = br.readLine();
System.out.print("Enter age: ");
int age = Integer.parseInt(br.readLine());
Person p = new Person(name, age);
p.check();
}
}