-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeDemo.java
More file actions
165 lines (137 loc) · 3.92 KB
/
EmployeeDemo.java
File metadata and controls
165 lines (137 loc) · 3.92 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Concept - Static Members Difficulty Level 2
Create a class Employee with below attributes:
id - int
name - String
designation - String
ssnNo - int
age - int
salary - double
idCounter - int which is a static field and initialize to 0
Make all the attributes private.Create corresponding getters and setters.
Create a constructor which takes all parameters except the static member and the attribute id in the above sequence.
The constructor should set the value of attributes as parameter values inside the constructor.Increment the value of the static member by 1 inside the constructor and assign to the attribute id
Test from the main method by creating 5 objects of the Employee class and print the idCounter value after creation of each object
Refer below sample main method and test the output:
public class EmployeeDemo {
public static void main(String args[]){
Employee employee1= new Employee("jirhehb","siarpha",35,70,776.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee2= new Employee("ycrdzsi","voivkqr",32,87,484.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee3= new Employee("txdxrzv","azpjbbb",4,63,369.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee4= new Employee("pefjukm","mctpbqe",36,0,951.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee5= new Employee("shbrzke","lpcnymz",51,86,181.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
}
}
Output
Value of idCounter : 1
Value of idCounter : 2
Value of idCounter : 3
Value of idCounter : 4
Value of idCounter : 5
*/
public class EmployeeDemo
{
public static void main(String args[])
{
Employee employee1= new Employee("jirhehb","siarpha",35,70,776.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee2= new Employee("ycrdzsi","voivkqr",32,87,484.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee3= new Employee("txdxrzv","azpjbbb",4,63,369.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee4= new Employee("pefjukm","mctpbqe",36,0,951.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
Employee employee5= new Employee("shbrzke","lpcnymz",51,86,181.0);
System.out.println("Value of idCounter : " + Employee.getIdCounter());
}
}
class Employee
{
// private members declaration
private int id;
private String name;
private String designation;
private int ssnNo;
private int age;
private double salary;
static int idCounter;// static member
// parameterised constructor
public Employee(String name,String designation,int ssnNo,int age,double salary)
{
this.name=name;
this.designation=designation;
this.ssnNo=ssnNo;
this.age=age;
this.salary=salary;
this.idCounter+=1;
this.id=this.idCounter;
}
// public getters and setters
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setDesignation(String designation)
{
this.designation=designation;
}
public String getDesignation()
{
return designation;
}
public void setSsnNo(int ssnNo)
{
this.ssnNo=ssnNo;
}
public int getSsnNo()
{
return ssnNo;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setSalary(double salary)
{
this.salary=salary;
}
public double getSalart()
{
return salary;
}
public static int getIdCounter()
{
return idCounter;
}
}
/* Output
(base) mansi@mansi-Vostro-15-3568:~$ javac EmployeeDemo.java
(base) mansi@mansi-Vostro-15-3568:~$ java EmployeeDemo
Value of idCounter : 1
Value of idCounter : 2
Value of idCounter : 3
Value of idCounter : 4
Value of idCounter : 5
(base) mansi@mansi-Vostro-15-3568:~$ ^C
/