0% found this document useful (0 votes)
21 views27 pages

Lab 6

Uploaded by

Mohamed Hazem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views27 pages

Lab 6

Uploaded by

Mohamed Hazem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Lab 6

Concepts of programming languages

1
Agenda
1. Classes
1. Abstract Class & Abstract Method
2. Case Classes
3. Traits
2. Collections
1. Arrays
2. Lists
3. Maps
3. Hands-on

2
Classes

3
Abstract Class &
Abstract Method
An abstract class can
have both abstract
• A class which is declared with abstract keyword is known and
as abstract
non-abstract
class. (implemented)
methods.
abstract class Pet (name: String) {
var age: Int = 0 // concrete field we cannot
val animal: String // abstract field instantiate an
def printName(): Unit // abstract method abstract class
Abstract class maydirectly
be used using
as anew
def printAge(): Unit = { // concrete method operator
base class of some other class
println(age)
which implements the
}
unimplemented members.
}
4
Abstract Class & A class can
Abstract Method extend only one
abstract class.

• Use the “extends” keyword to extend an abstract class.


Dog class is expected
class Dog(name: String) extends Pet(name) { to implement all the
// Implementation of abstract method abstract methods and
override val animal: String = "Dog" fields defined in that
override def printName(): Unit = { abstract class.
println("Hi there, I am " + name)
}
}
• If the extending class but does not implement all the methods and fields
defined in an abstract class, the extending class must be declared
abstract. 5
Abstract Class &
Abstract Method
• Main Class
object mainObj {
def main(args: Array[String]): Unit = {
val dogObj = new Dog("Jimmy")
dogObj.printName() Hi there, I am Jimmy
dogObj.age = 2 2
dogObj.printAge()
}
}

6
Case Classes Setters

Fields
Other details

Getters

• When coding in languages which are a bit more


verbose, such as Java, you often end up using the
tools, such as your IDE, to automatically generate
some code for you.

• What if, instead of seeking support from your IDE,


the compiler could do this for you? Case Class

7
By default, all the
parameters in a
Case Classes case class
are public.

• Case Class is a class with an arbitrary number of parameters for which


the compiler automatically adds ad-hoc code.
case class Member(id: Integer, name: String, country: String)

• Case classes are good for modeling immutable data.

• Case classes are the ideal data containers because of these


functionalities that encourage the use of immutability.

8
What if I want to

Case Classes modify a value of an


existing case class

The new keyword was


• Creating an object of the case class not used to instantiate
the Member case class
object mainObj {
def main(args: Array[String]): Unit = {
val a = Member(10001, "Adam", "UK")
println(a.id) //prints 10001
a.id = 10002 //throws a compile time error
}
You can’t reassign
} a variable in a
case class
because it is a val
9
Case Classes
• Solution:
You can use the copy function to create a new data representation.
object mainObj {
def main(args: Array[String]): Unit = {
val a = Member(10001, "Adam", "UK")
val b = a.copy(id = 10002, name = "Bryan")
println(b.id) //prints 10002
println(b.name) //prints Bryan
println(b.country) //prints UK which was copied from object a
}
}
10
Traits
• Scala has entirely removed interface and introduced traits.

• Trait is something that is much more powerful and flexible than the Java
interface.

• A trait is an object-oriented programming concept that represents a unit


functionality that is usable by mixing it into a class.

11
Traits
• Trait can include either concrete or abstract methods and fields.
trait Shape {
var color = "red" //Concrete field
val shapeName:String //abstract field

def printShapeInfo(): Unit={ //Concrete method


println("Shape name: " + shapeName)
println("Shape color: " + color)
}

def getArea(): Double // abstract method


} 12
class Circle(var radius: Double) extends Shape {
override val shapeName: StringThe= "Circle"
Traits
class doesn’t
implement the abstract
override def getArea(): Double = { methods and fields
Math.PI * this.radius * this.radius
}
• When a class is mixing with a trait, it has access to all of its methods and
fields.def printShapeInfo(): Unit={
override
super.printShapeInfo()
println("Shape
abstract radius: "+ radius)radius: Double) extends Shape {
class Circle(var
}
def draw(): Unit = {
def draw():System.out.println("Drawing
Unit = { Circle")
System.out.println("Drawing
} Circle")
}
def getRadius(): Double = this.radius
def getRadius():
} Double = this.radius
}
13
Traits
• Example Cont…
object mainObj {
def main(args: Array[String]): Unit = {
var circle1 = new Circle(5)

circle1.printShapeInfo()
println("Circle Area: " + circle1.getArea())
}
Shape name: Circle
}
Shape color: red
Shape radius = 5.0
Circle Area: 78.53981633974483
14
Traits trait Happy {
def mood(): Unit = println("I am very happy")
}
• Mixing multiple
trait Rich {
traits with the same val money: Long = 10000000L
class or object }

class StrikeLotteryGuy extends Happy with Rich

object mainObj extends App{


var x = new StrikeLotteryGuy()
x.mood() // print: I am very happy
println(x.money.toString) // print: 10000000

} 15
Collections

16
Collections
• Scala has a very rich collections library, located under the
scala.collection package.

• Example of collections: Array, List, and Map.

17
Arrays
• Arrays in Scala are mutable, indexed collections of values.
val array: Array[Int] = Array(1, 2, 3) 1
println(array(1)) // 2
array(1) = 5 5
println(array(1)) // 5 3
array.foreach(x=>println(x))
val array2 = array.reverse
array2.foreach(println)
3
5
1
18
Lists
• Scala lists internally represent an immutable linked list.
val emptyList: List[Int] = List() // Empty List
var reservedwordsList: List[String] = List("if", "while", "for", "def")

println("Head: " + reservedwordsList.head) // Head: if


println("Tail: " + reservedwordsList.tail) // Tail: List(while. for, def)
println(reservedwordsList.isEmpty) //false
println(reservedwordsList.reverse) //List(def, for, while, if)

19
List
• Append an item to the end of the list.
reservedwordsList = reservedwordsList :+ "val"
println(reservedwordsList) // List(if, while, for, def, val)

• Prepend an item to the beginning of the list


reservedwordsList = "var" :: reservedwordsList
println(reservedwordsList) // List(var, if, while, for, def, val)

20
Maps

• A Map is a collection of key/value pairs where keys are always unique.


Key Value
var map = Map("Machine Learning" -> "B",
"Concepts of programming" -> "A",
"Algorithms"->"C+")
• Display keys and values

map.keys.foreach(key=>println("Key = "+ key + ", value = "+ map(key)


map.values.foreach(println)
21
Maps

• Retrieve a value using its key.

println(map.get("Algorithms")) // Some(C+)

• checks if a map has the specific key.

println(map.contains("Machine Learning")) // true

22
Hands-on

23
• Create a person class as an abstract class. the class has
two attributes (name and gender). The attributes are
constant and cannot be accessed outside the class
Hands-on • Person class has a primary constructor that takes the
name and the gender.
• Person class has an abstract method called work and
abstract field nationality
• Create an employee class extends the person class.
• Employee class has a primary constructor that takes
name, gender , and empid as a parameters.
• Override the abstract field.
• Override the work function: if the empid equals 1 then
print “working”. If it equals 0, print “not working”.
• Create an object of employee and call the work function.
24
Solution

25
Solution

26
Thank you.

27

You might also like