Skip to content

Commit 0863499

Browse files
committed
toString, hashCode and equals method implementation
1 parent c631612 commit 0863499

File tree

2 files changed

+219
-176
lines changed

2 files changed

+219
-176
lines changed

4.Classes-Objects/Employee.scala

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,72 @@
1-
import scala.beans.BeanProperty
2-
3-
/**
4-
* val creates accessors(getters), access to inner state
5-
* var creates both accessors and mutators(setters)
6-
* you can view class file using javap -p <class name>
7-
*
8-
* call another constructor by invoking this
9-
*
10-
* Scala does not have Checked Exceptions. All Exceptions are unchecked in scala, even SQLException and IOException
11-
*
12-
* extends keyword is used to subclass a class
13-
* You can have multiple public classes in one file
14-
*
15-
* override keyword is mandatory for method overriding
16-
*
17-
*/
18-
class Employee (@BeanProperty val firstName:String, @BeanProperty var lastName:String, @BeanProperty val title:String) //This is default constructor
19-
{
20-
require(firstName.nonEmpty, "First Name cannot be empty!!") //preconditions using require. If not met, will throw IllegalArgumentException
21-
require(lastName.nonEmpty, "Last Name cannot be empty!!")
22-
require(title.nonEmpty, "Title cannot be empty!!")
23-
24-
if(title.contains("Senior") || title.contains("Junior")) //Preconditions using manual check
25-
throw new IllegalArgumentException("Title cannot contain Senior or Junior!!")
26-
27-
//Another constructor
28-
def this(firstName:String, lastName:String ) = {this(firstName, lastName, "Programmer") //if constructor block is multiline, First call must be this
29-
println("Multiline constructor block!!")}
30-
31-
def fullName = s"$firstName $lastName"
32-
33-
def copy(firstName:String = this.firstName, lastName:String=this.lastName, title:String=this.title):Employee =
34-
{
35-
new Employee(firstName,lastName, title)
36-
}
37-
}
38-
39-
class Department(val name:String)
40-
41-
class Manager(firstName:String, lastName:String, title:String, val department:Department) extends
42-
Employee(firstName, lastName, title) {
43-
override def fullName = s"$firstName $lastName, ${department.name}, Manager"
44-
45-
override def copy(firstName: String = this.firstName, lastName: String = this.lastName, title: String = this.title): Manager = {
46-
new Manager(firstName, lastName, title, new Department("Toys"))
47-
}
48-
}
49-
1+
import scala.beans.BeanProperty
2+
3+
/**
4+
* val creates accessors(getters), access to inner state
5+
* var creates both accessors and mutators(setters)
6+
* you can view class file using javap -p <class name>
7+
*
8+
* call another constructor by invoking this
9+
*
10+
* Scala does not have Checked Exceptions. All Exceptions are unchecked in scala, even SQLException and IOException
11+
*
12+
* extends keyword is used to subclass a class
13+
* You can have multiple public classes in one file
14+
*
15+
* override keyword is mandatory for method overriding
16+
*
17+
*/
18+
class Employee (@BeanProperty val firstName:String, @BeanProperty var lastName:String, @BeanProperty val title:String) //This is default constructor
19+
{
20+
require(firstName.nonEmpty, "First Name cannot be empty!!") //preconditions using require. If not met, will throw IllegalArgumentException
21+
require(lastName.nonEmpty, "Last Name cannot be empty!!")
22+
require(title.nonEmpty, "Title cannot be empty!!")
23+
24+
if(title.contains("Senior") || title.contains("Junior")) //Preconditions using manual check
25+
throw new IllegalArgumentException("Title cannot contain Senior or Junior!!")
26+
27+
//Another constructor
28+
def this(firstName:String, lastName:String ) = {this(firstName, lastName, "Programmer") //if constructor block is multiline, First call must be this
29+
println("Multiline constructor block!!")}
30+
31+
def fullName = s"$firstName $lastName"
32+
33+
def copy(firstName:String = this.firstName, lastName:String=this.lastName, title:String=this.title):Employee =
34+
{
35+
new Employee(firstName,lastName, title)
36+
}
37+
38+
override def equals(x:Any):Boolean =
39+
{
40+
if(! x.isInstanceOf[Employee]) false
41+
else
42+
{
43+
val other = x.asInstanceOf[Employee]
44+
other.firstName == this.firstName &&
45+
other.lastName == this.lastName &&
46+
other.title == this.title
47+
}
48+
}
49+
50+
override def hashCode:Int=
51+
{
52+
var result = 19
53+
result = 31 * result + firstName.hashCode
54+
result = 31 * result + lastName.hashCode
55+
result = 31 * result + title.hashCode
56+
result
57+
}
58+
59+
override def toString = s"Employee($firstName, $lastName, $title)"
60+
}
61+
62+
class Department(val name:String)
63+
64+
class Manager(firstName:String, lastName:String, title:String, val department:Department) extends
65+
Employee(firstName, lastName, title) {
66+
override def fullName = s"$firstName $lastName, ${department.name}, Manager"
67+
68+
override def copy(firstName: String = this.firstName, lastName: String = this.lastName, title: String = this.title): Manager = {
69+
new Manager(firstName, lastName, title, new Department("Toys"))
70+
}
71+
}
72+
Lines changed: 147 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,147 @@
1-
val emp1 = new Employee("Gabbar", "Singh")
2-
println("FirstName:" + emp1.firstName)
3-
println("LastName:" + emp1.lastName)
4-
5-
emp1.lastName = "Babbar"
6-
println("Modified LastName :" + emp1.lastName)
7-
8-
println("title:" + emp1.title)
9-
10-
try {
11-
val emp2 = new Employee("Dennis", "", "C Programmer")
12-
}catch{
13-
case iae:IllegalArgumentException => println(iae.getMessage)
14-
}finally {
15-
println("Continuing with program")
16-
}
17-
18-
try {
19-
val emp2 = new Employee("Dennis", "Ritchie", "Senior C Programmer")
20-
}catch{
21-
case iae:IllegalArgumentException => println(iae.getMessage)
22-
case th:Throwable => println("Exception caught!!" + th.getMessage) //This will caught everything including OutofMemoryError and others that can terminate JVM
23-
}finally {
24-
println("Continuing with program")
25-
}
26-
27-
28-
//AFter making changes for DepartMent and Maanger
29-
val dept = new Department("Computer Science")
30-
31-
val mgr = new Manager("Alan", "Turing", "Mathematician", dept)
32-
println("Department:" + mgr.department.name)
33-
34-
println("Full Name:" + mgr.fullName)
35-
36-
val cpyMgr = mgr.copy(title="Encryption")
37-
println("Copied Manager: " + cpyMgr.fullName + "-" + cpyMgr.title)
38-
39-
/**
40-
*Sample output
41-
*
42-
******Before adding var and val to class params
43-
E:\Courses\scala\IntellijIDEA\src>scalac Employee.scala
44-
45-
E:\Courses\scala\IntellijIDEA\src>javap -p Employee.class
46-
Compiled from "Employee.scala"
47-
public class Employee {
48-
public Employee(java.lang.String, java.lang.String);
49-
}
50-
51-
******After adding var and val to class params
52-
E:\Courses\scala\IntellijIDEA\src>scalac Employee.scala
53-
54-
E:\Courses\scala\IntellijIDEA\src>javap -p Employee.class
55-
Compiled from "Employee.scala"
56-
public class Employee {
57-
private final java.lang.String firstName;
58-
private java.lang.String lastName;
59-
public java.lang.String firstName();
60-
public java.lang.String lastName();
61-
public void lastName_$eq(java.lang.String);
62-
public Employee(java.lang.String, java.lang.String);
63-
}
64-
65-
E:\Courses\scala\IntellijIDEA\src>scala -cp . EmployeeScript.scala
66-
FirstName:Gabbar
67-
Modified LastName :Babbar
68-
69-
E:\Courses\scala\IntellijIDEA\src>scala -cp . EmployeeScript.scala
70-
FirstName:Gabbar
71-
LastName:Singh
72-
Modified LastName :Babbar
73-
74-
75-
---After adding title parameter to constructor
76-
E:\Courses\scala\IntellijIDEA\src>scalac Employee.scala
77-
78-
E:\Courses\scala\IntellijIDEA\src>scala -cp . EmployeeScript.scala
79-
Multiline constructor block!!
80-
FirstName:Gabbar
81-
LastName:Singh
82-
Modified LastName :Babbar
83-
title:Programmer
84-
85-
---After handling Exceptions using Try Catch
86-
E:\Courses\scala\IntellijIDEA\src>scala EmployeeScript.scala
87-
Multiline constructor block!!
88-
FirstName:Gabbar
89-
LastName:Singh
90-
Modified LastName :Babbar
91-
title:Programmer
92-
requirement failed: Last Name cannot be empty!!
93-
Continuing with program
94-
Title cannot contain Senior or Junior!!
95-
Continuing with program
96-
97-
98-
----------------After adding classses for DepartMent and Manager
99-
E:\Courses\scala\IntellijIDEA\src>scala EmployeeScript.scala
100-
Multiline constructor block!!
101-
FirstName:Gabbar
102-
LastName:Singh
103-
Modified LastName :Babbar
104-
title:Programmer
105-
requirement failed: Last Name cannot be empty!!
106-
Continuing with program
107-
Title cannot contain Senior or Junior!!
108-
Continuing with program
109-
Department:Computer Science
110-
111-
--------------After adding changes for method overriding
112-
E:\Courses\scala\IntellijIDEA\src>scala -nc EmployeeScript.scala
113-
Multiline constructor block!!
114-
FirstName:Gabbar
115-
LastName:Singh
116-
Modified LastName :Babbar
117-
title:Programmer
118-
requirement failed: Last Name cannot be empty!!
119-
Continuing with program
120-
Title cannot contain Senior or Junior!!
121-
Continuing with program
122-
Department:Computer Science
123-
Full Name:Alan Turing, Computer Science, Manager
124-
Copied Manager: Alan Turing, Toys, Manager-Encryption
125-
126-
127-
*/
1+
val emp1 = new Employee("Gabbar", "Singh")
2+
println("FirstName:" + emp1.firstName)
3+
println("LastName:" + emp1.lastName)
4+
5+
emp1.lastName = "Babbar"
6+
println("Modified LastName :" + emp1.lastName)
7+
8+
println("title:" + emp1.title)
9+
10+
try {
11+
val emp2 = new Employee("Dennis", "", "C Programmer")
12+
}catch{
13+
case iae:IllegalArgumentException => println(iae.getMessage)
14+
}finally {
15+
println("Continuing with program")
16+
}
17+
18+
try {
19+
val emp2 = new Employee("Dennis", "Ritchie", "Senior C Programmer")
20+
}catch{
21+
case iae:IllegalArgumentException => println(iae.getMessage)
22+
case th:Throwable => println("Exception caught!!" + th.getMessage) //This will caught everything including OutofMemoryError and others that can terminate JVM
23+
}finally {
24+
println("Continuing with program")
25+
}
26+
27+
28+
//AFter making changes for DepartMent and Maanger
29+
val dept = new Department("Computer Science")
30+
31+
val mgr = new Manager("Alan", "Turing", "Mathematician", dept)
32+
println("Department:" + mgr.department.name)
33+
34+
println("Full Name:" + mgr.fullName)
35+
36+
val cpyMgr = mgr.copy(title="Encryption")
37+
println("Copied Manager: " + cpyMgr.fullName + "-" + cpyMgr.title)
38+
39+
//equals method
40+
val emp2 = new Employee("Gabbar", "Singh")
41+
val emp3 = new Employee("Gabbar", "Singh")
42+
43+
/* == operator is used for equals method
44+
eq operator is used for comparing object whether
45+
both objects are referring to same object or not
46+
*/
47+
48+
println("Object equality: " + (emp2 == emp3)) //true
49+
println("Object reference comparison:" + (emp2 eq emp3)) //false
50+
println("Hashcode equality:" + (emp2.hashCode == emp3.hashCode)) //true
51+
println("Hashcode:" + emp2.hashCode)
52+
53+
println("ToString:" + emp2)
54+
/**
55+
*Sample output
56+
*
57+
******Before adding var and val to class params
58+
E:\Courses\scala\IntellijIDEA\src>scalac Employee.scala
59+
60+
E:\Courses\scala\IntellijIDEA\src>javap -p Employee.class
61+
Compiled from "Employee.scala"
62+
public class Employee {
63+
public Employee(java.lang.String, java.lang.String);
64+
}
65+
66+
******After adding var and val to class params
67+
E:\Courses\scala\IntellijIDEA\src>scalac Employee.scala
68+
69+
E:\Courses\scala\IntellijIDEA\src>javap -p Employee.class
70+
Compiled from "Employee.scala"
71+
public class Employee {
72+
private final java.lang.String firstName;
73+
private java.lang.String lastName;
74+
public java.lang.String firstName();
75+
public java.lang.String lastName();
76+
public void lastName_$eq(java.lang.String);
77+
public Employee(java.lang.String, java.lang.String);
78+
}
79+
80+
E:\Courses\scala\IntellijIDEA\src>scala -cp . EmployeeScript.scala
81+
FirstName:Gabbar
82+
Modified LastName :Babbar
83+
84+
E:\Courses\scala\IntellijIDEA\src>scala -cp . EmployeeScript.scala
85+
FirstName:Gabbar
86+
LastName:Singh
87+
Modified LastName :Babbar
88+
89+
90+
---After adding title parameter to constructor
91+
E:\Courses\scala\IntellijIDEA\src>scalac Employee.scala
92+
93+
E:\Courses\scala\IntellijIDEA\src>scala -cp . EmployeeScript.scala
94+
Multiline constructor block!!
95+
FirstName:Gabbar
96+
LastName:Singh
97+
Modified LastName :Babbar
98+
title:Programmer
99+
100+
---After handling Exceptions using Try Catch
101+
E:\Courses\scala\IntellijIDEA\src>scala EmployeeScript.scala
102+
Multiline constructor block!!
103+
FirstName:Gabbar
104+
LastName:Singh
105+
Modified LastName :Babbar
106+
title:Programmer
107+
requirement failed: Last Name cannot be empty!!
108+
Continuing with program
109+
Title cannot contain Senior or Junior!!
110+
Continuing with program
111+
112+
113+
----------------After adding classses for DepartMent and Manager
114+
E:\Courses\scala\IntellijIDEA\src>scala EmployeeScript.scala
115+
Multiline constructor block!!
116+
FirstName:Gabbar
117+
LastName:Singh
118+
Modified LastName :Babbar
119+
title:Programmer
120+
requirement failed: Last Name cannot be empty!!
121+
Continuing with program
122+
Title cannot contain Senior or Junior!!
123+
Continuing with program
124+
Department:Computer Science
125+
126+
--------------After adding changes for method overriding
127+
E:\Courses\scala\IntellijIDEA\src>scala -nc EmployeeScript.scala
128+
Multiline constructor block!!
129+
FirstName:Gabbar
130+
LastName:Singh
131+
Modified LastName :Babbar
132+
title:Programmer
133+
requirement failed: Last Name cannot be empty!!
134+
Continuing with program
135+
Title cannot contain Senior or Junior!!
136+
Continuing with program
137+
Department:Computer Science
138+
Full Name:Alan Turing, Computer Science, Manager
139+
Copied Manager: Alan Turing, Toys, Manager-Encryption
140+
141+
Object equality: true
142+
Object reference comparison:false
143+
Hashcode equality:true
144+
Hashcode:-1813557963
145+
ToString:Employee(Gabbar, Singh, Programmer)
146+
147+
*/

0 commit comments

Comments
 (0)