:mortar_board: Sunway University BSc in CS notes :memo: (201701 intake)
Lecturer: Dr Chin Teck Min
Textbook used: Programming in Scala, 3rd Edition by Odersky, Spoon, Venners. Artima Press.
Terminology | Explanation |
---|---|
Class | A class is a pattern or blueprint for creating an object. A class contains all attributes and behaviors that describe or make up the object. |
Object | An object is an instance of a class. It is a construct that combines a state (data) and behavior (operations). When combined, the state and the behavior represents an abstraction of a “real-world” object. |
Instance | An object created from a class (is an instance of of that class). |
Constructor | A special method that is called when creating a new object which initializes the object state. |
Attributes | Characteristics that describe the object (sometimes referred to as properties) |
Methods | Operations (or actions) that objects perform or operations which are performed to an object. Sometimes referred to as behaviors. A collection of statements that, when run together, performs a task. |
Encapsulation | Refers to the combining of an object’s attributes and behaviors into a single package, i.e. a class. The programmer then can then access the methods and properties of the class without having to understand the implementation. |
Parameter | A special kind of variable passed to a subroutine/procedure/function call i.e in def add_num(num1, num2) , num1 and num2 are parameters in the function. |
Argument | Values supplied to the subroutine/procedure when it is called i.e in def add_num(num1, num2) , when calling add_num() , you would enter add_num(2,6) and thus 2 and 6 are arguments in the function call. |
Behavior | Tasks and actions that are performed by the object. |
Characteristic | Properties, qualities, or states, of an object. |
Variable | An identifier in the program that stores values or reference(s) that are mutable. |
Constant Variable | Immutable variables - variables that cannot be changed after being declared and assigned. |
Instance Variable | A non-static field of a class. Each individual object of a class has its own copy of such a field. This is in contrast to a class variable which is shared by all instances of the class. Instance variables are used to model the attributes of a class. |
Instance Method | A subroutine of an object. |
Static Variable/Class Variable | A variable defined inside a class body. Such a variable belongs to the class as a whole, and is, therefore, shared by all objects of the class. |
Predefined Library | A library of high-quality classes and methods that is shipped with compilers by default that provides solutions to common problems. |
User Defined Library | A library of classes and methods that are written by the programmer themselves for more specific problems. |
Type | A set of data values, as well as |
Generalization | The process of avoiding code and data duplication among several classes by creating a super class to those classes and moving the duplicate code and data up into that superclass, which is commonly abstract. |
Specialization | The process of extending an existing class by adding new features is called using inheritance for specialization. |
Singleton Object | An object that has only one instance in the entire program and is defined using the object keyword. |
Companion Object | A singleton object that has the same name with a class name, and it is used to describe every instance of the class. |
Method Signatures | A method signature is part of the method declaration. It is the combination of the method name and the parameter list. |
Overriding | A method defined in a super class may be overridden by a method of the same name defined in a sub class. The new method with the same name will have a different output/signature. |
Abstract Class | A class with the abstract reserved word in its header. Abstract classes are distinguished by the fact that you may not directly construct objects from them using the new operator. An abstract class may have zero or more abstract methods. |
Concrete Class | A class that can have instances. |
Polymorphism | Polymorphism refers to the capability of having methods with the same names and parameter types exhibit different behavior depending on the receiver. In other words, you can send the same message to two different objects and they can respond in different ways. |
Overloading | Using the same names to mean different things in different contexts. A method with the same name/identifier but produces different results. |
Parametric Polymorphism | A function or data type that is written generically so that it can handle values identically without depending on its type. public static |
Subtype Polymorphism | Allows a function to be written to take an object of a certain type, but also works correctly if passed an object that belongs to a different type, that is a subtype of the original type. Executors.newSingleThreadExecutor().submit(runnable); |
Cohesion | A measure of how focused the tasks of a module is. The extent to which a component performs a single well-defined task. |
Coupling | The dependencies of modules on each other. When classes are aware of each other because their instances must interact. |
Interface | When a class implements an interface, the class inherits no implementation from the interface, only method signatures and static variables. |
Trait | Used to share interfaces and fields between classes. They are similar to Java 8’s interfaces. |
Generic Programming | Allows types to be parameterized in classes, interfaces, or methods, much like formal parameter use in method declaration. |
Iterator | An interface for collection in Java/Scala library that specifies hasNext and next abstract function. |
Data Field | Represents the attribute of an object. |
Property | A method in OOP to expose private data field through getter and setter so that data can be encapsulated in within the class to hide internal data field handling and also allow implementation of data validation. |
Mixin | A class that contains implementation for use by other classes without having to be the parent class of those other classes. |
Instantiation | The creation of an instance of a class - that is an object. The creation of an object from a class. |
Initialization | Assigning values to an object’s attributes, assigning the first value to a variable. |
Aggregation | A relationship in which an object contains one or mre other subordinate objects as part of its state. The subordinate objects typically have no independent existence separate from their containing object. For example, a gas station object might contain several pump objects. These pumps will only exist as long as the station does. It is also referred to as the has-a relationship. |
Inheritance | Refers to the capability of creating a new class from an existing class. The new inherited class contains all methods, variables, and other attributes from the parent class. Also known as a is-a relationship. |
Composition | A strict form of aggregation where the life of the part is tied to the life of the whole. |
Identifier | A programmer-defined name for a variable, method, class, or interface. |
Has-A Relationship | Also known as aggregation. |
Is-A Relationship | Also known as inheritance. |
case class Currency(value: Double)
case class RM(_value: Double) extends Currency(_value)
class Hardware (
val name: String,
val brand: String,
val productionDate: String,
val price: Currency
)
class PowerSupply(
_name: String,
_brand: String,
_productionDate: String,
_price: Currency,
val voltage: Double
) extends Hardware(_name, _brand, _productionDate, _price)
case class MHz(value: double)
class VideoCard(
_name: String,
_brand: String,
_productionDate: String,
_price: Currency,
val memory: Double,
val clockSpeed: MHz,
val numCores: Int,
val resolutions: Array[String],
) extends
Hardware(_name, _brand, _productionDate, _price)
class Computer(var hardwares: Array[Hardware])
object MyApp extends App {
val cosairPW: Hardware = new PowerSupply(
"Cosair PW1000",
"Cosair",
"10/03/2018",
RM(1000.0),
240.0
)
val gtxVideo: VideoCard = new VideoCard(
"GTX1800",
"Nvidia",
"14/09/2018",
RM(5000),
5000,
Mhz(1000),
8,
Array("1900x1600", "1200x600")
)
val total = cosairPW.price.value + gtxVideo.price.value
val hardwares:Array[Hardware] = Array(cosairPW, gtxVideo)
val com = new Computer(hardwares)
com.hardwares(0)
}
case class State(
hp:Double,
attack:Double,
defense:Double,
sp_attack:Double,
speed:Double
)
case class Breeding(
eggGroup:String,
malePercentage:Double,
femalePercentage:Double,
eggCycle:Int
)
abstract class Pokemon(){
val givenName: String
val height: Double
val weight: Double
val gender: String
val state: State
}
object Pokemon{
val copyrightCompany: String = "Nintendo"
}
class Bulbasaur(
val height: Double,
val weight: Double,
val gender: String,
val state: State
) extends Pokemon() {
}
trait PokemonCharacteristic {
val name: String
val types: Array[String]
val species: String
val ability: Array[String]
val evolution: String
val baseState: State
val breeding: Breeding
val weakness: String
}
object Bulbasaur extends PokemonCharacteristic {
val name: String = "Bulbasaur"
val types: Array[String] = Array("Grass","Poison")
val species: String = "Seed Pokemon"
val ability: Array[String] = Array("Overgrow","Chlorophyll")
val evolution: String = "Ivysaur"
val baseState: State = State(40,49,49,65,45)
val breeding: Breeding = Breeding("Seed Pokemon", 87.5,12.5,20)
val weakness: String = "Fire"
}
new Bulbasaur("BULBOBAGGINS", 0.71, 6.9, "Male", State(50,50,50,50,50))
case class Stat(
int: Double,
str: Double,
agi: Double,
atk: Double,
moveSpeed: Double,
armor: Double
)
class Skill(val name: String, val desc: String, val ability: String)
trait SkillCost {
val manaCost : Array[Int]
val cooldown: Array[Int]
}
trait SkillDamage {
val damageType: String
}
class FreezingField(
_name: String,
_desc: String,
_ability: String
) extends Skill(_name, _desc,_ability)
with SkillCost
with SkillDamage {
val manaCost: Array[Int] = Array(200,400, 600)
val cooldown = Array(110,100, 90)
val damageType = "Magical"
}
abstract class Hero (
var activeStat: Stat,
var level: Int,
var hitPoint: Double,
var mana : Double,
val skill: Array[Skill]
) {
def move() : Unit = {}
}
trait HeroCharacteristic {
val name: String
val baseStat: Stat
def castSkill: Unit
}
class CrystalMaiden(
_activeStat: Stat,
_level: Int,
_hitPoint: Double,
_mana : Double,
_skill: Array[Skill]
) extends Hero(_activeStat, _level, _hitPoint, _mana, _skil)
object CrystalMaiden extends HeroCharacteristic {
val name: String = "CrystalMaiden"
val baseStat: Stat = Stat(16, 16, .....)
def castSkill: Unit = {
}
}
class Huskar(
_activeStat: Stat,
_level: Int,
_hitPoint: Double,
_mana : Double,
_skill: Array[Skill]
) extends Hero(_activeStat, _level, _hitPoint, _mana, _skil)
object Huskar extends HeroCharacteristic {
val name: String = "Huskar"
val baseStat: Stat = Stat(18, 15, .....)
def castSkill: Unit = { }
}
case class Stat(
int:Double, str:Double, agi:Double, atk:Double,
moveSpeed:Double, armor:Double
)
class Skill(
val name:String, val desc: String
)
trait HeroCharacteristic{
val name:String,
val baseStat:Stat
}
abstract class Hero(
val activeStat: Stat,
val level: Int,
val hitPoint: Double,
val mana: Double,
val skill:Array[Skill]
)
class CrystalMaiden(
_activeStat: stat,
_level: Int,
_hitPoint: Double,
_mana:Double,
_skill:Array[Skill]
)
extends Hero (
_activeStat, _level, _hitPoint, _mana, _skill
)
object CrystalMaiden extends HeroCharacteristic {
val name:String = "Crystal Maiden"
val baseStat = Stat(16,16, .....)
def castSkill: Unit = {
}
}
class Huskar (
_activeStat: Stat,
_level: Int,
_hitPoint: Double,
_mana: Double,
_skill: Array[Skill]
) extends HeroCharacteristic {
val name: String = "Huskar"
val baseStat: Stat = Stat(16,16, .....)
def castSkill: Unit = {
}
}
trait LeftWing {
val leftWingHeight: Double = 100.0
val leftWingWidth: Double = 50.0
val leftWingWeight: Double = 50
}
trait RightWing {
val rightWingHeight: Double
val rightWingWidth: Double
val rightWingWeight: Double
}
class Vehicle
class Aeroplane extends Vehicle
class Airbus306() extends Aeroplane with LeftWing with RightWing {
val rightWingHeight: Double = 100.0
val rightWingWidth: Double = 50.0
val rightWingWeight: Double = 50
}
class Boeing707() extends LeftWing with RightWing {
val rightWingHeight: Double = 200.0
val rightWingWidth: Double = 70.0
val rightWingWeight: Double = 90
}