SCALA- PRACTICALS PART 3
Question: Calculate area of hall for 5 flats in a floor.
Solution:
Suppose we have A building with 9th floor. And we have to calculate the area of 5 flat whose dimension are given below
A-901 length len= 40.96, Width wid =20.00
A-902 length len =35.84, Width wid =22.08
A-903 length len =30.08, Width wid =16.00
A-904 length len =45.50, Width wid =25.00
A-905 length len =45.30, Width wid = 23.35
Logic to find out the area of 5 flats of building whose dimensions are given: [len*wid]: logic
Output:
819.2
788.48
480.0
1137.5
964.355
Process finished with exit code 0
Note: without constructor, complier will never return any output
- Default constructor
- Primary constructor
- Auxiliary constructor
println is nothing it’s a by default constructor.
constructor means it will print something.
Step: of complier
- Compiler try to find out where is main method
- Reference for same class.
Question: What is the difference between primary constructor and function?
Answer:
Primary constructor allows to perform encapsulation.
Encapsulation has two points: It allows to perform getter and setter method.
Getter: Try to access the value. It allows only access. Does not allow to change it. By default, immutable. Immutable means val.
Setter: Setter can change and can change means mutable.
class Scala_Wel{
def area(len:Double,wid:Double):Double ={
val c = (len*wid)
println(c)
}
}
object Scal{
def main(args: Array[String]):Double = {
val s1 = new Scala_Wel()
val s2 = new Scala_Wel()
val s3 = new Scala_Wel()
val s4 = new Scala_Wel()
val s5 = new Scala_Wel()
s1.area(40.96, 20.00)
s2.area(35.84, 22.00)
s3.area(30.00, 16.00)
s4.area(45.5, 25.00)
s5.area(41.3, 23.35)
}
}
Classes, Functions and variables all these are by default public.
when we don’t write in Scala by default
No modifiers: by default, public
modifiers:
only the same class can access or the subclass can access or the same package can access.
Types of modifiers:
- No modifier
- Private
- Protected
What is the access pattern?
- Outside package
- Same package
Modifiers |
|
|
Outside
Package |
Same Package |
Same Class |
Sub Class |
Companion Class |
1 |
No Modifier |
Yes |
Yes |
Yes |
Yes |
Yes |
2 |
Private |
No |
No |
Yes |
No |
Yes |
3 |
Protected |
No |
No |
Yes |
Yes |
Yes |
- Singleton object: means there are no class only object. whenever we need to work with a static kind of concept. we want to implement once we don’t want to instantiate. It is used for static cases.
- Companion class: class and object both have same name
got to Scala class: kind it has class, case object, object, case object, trait.
always we need a main method. Without main method compiler will never wake up.
Extension of main method: extends App
If we use extends App, then no need to type main method
object Prop_Vol extends App {
//def main(args:Array[String]) ={}
println("Helli Scala")
}
Helli Scala
Process finished with exit code 0
How to create a method:?
def main(args:Array[String]): Unit = {
}
main method cannot have more methods inside. We cannot write methods inside method
For example:
def main(args:Array[String]): Unit = {
def vol ..........cannot write like this inside def function def vol
}
def cannot have inside def.
Example of singleton object.
The below example is for singleton. It’s a singleton object because there no class directly object
object Prop_Vol {
def main(args:Array[String])={
def vol(l:Double,w:Double,h:Double)={
(l*w*h)
}
println("This is vol of marriage hall"+vol(17.8,12.5,19.0))
}
This is vol of marriage hall4227.5
Process finished with exit code 0
How to instantiate Prop_Vol?
object Prop_Vol {
def main(args:Array[String])= {
println("This is vol of marriage hall"+vol(17.8,12.5,19.0))
}
def vol(l:Double,w:Double,h:Double)= {
(l * w * h)
val p = new Prop_Vol()
}
}
object Prop_Vol {
def main(args:Array[String])= {
println("This is vol of marriage hall"+vol(17.8,12.5,19.0))
val p = new Prop_Vol()
def vol(l:Double,w:Double,h:Double)= {
(l * w * h)
}
}
Page Break
Can not resolve symbol Pro_Vol
object Prop_Vol {
def main(args:Array[String])= {
println("This is for area1:" +area(len =18.0f,wid= 16.5f))
println("This is for area 2:" +area(len=20.0f,wid=18.2f))
}
def area(len:Float,wid:Float) = {
(len*wid)
}
}
This is for area1:297.0
This is for area 2:364.0
Process finished with exit code 0
We cannot instantiate here we can just write a method a same object. But outside object it will get an issue.
what are Scala class object case object, trait and Scala worksheet in intellij IDE and how to select for efficient programming
Factorial function:
Factorial function is recursion. recursive means function calling function.
5! = 5*4*3*2*1
logic
n! =n*(n-1) *(n-2) *(n-3) ...................................................................(n-n+1)
How to use logic in syntax?
Python logic for factorial, java logic for factorial, Scala logic for factorial. logic is same in all the languages only syntax is different
Fibonacci series :0,1, (0+1=1), (1+1=2), (1+2=3), (2+3=5), .....................
any recursive function needs return type.
How write factorial methods in Scala
Factorial without using class
object Prop_Vol {
def main(args:Array[String])= {
println("output of factorial 5!:" + facto(5))
}
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
output of factorial 5!:120
Process finished with exit code 0
object Prop_Vol {
def main(args:Array[String])= {
println("output of factorial 10!:" + facto(10))
}
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
output of factorial 10!:3628800
Process finished with exit code 0
object Prop_Vol {
def main(args:Array[String])= {
println("output of factorial 6!:" + facto(6))
}
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
output of factorial 6!:720
Process finished with exit code 0
object Prop_Vol {
def main(args:Array[String])= {
println("output of factorial 3!:" + facto(3))
}
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
output of factorial 3!:6
Process finished with exit code 0
Factorial using class
the factorial using class: in class we can write function within function then we can call it from my object .
class Factorial_new {
def facto(n:BigInt):BigInt={
if(n==0)1 else n*(n-1)
}
}
object Prop_Vol1{
def main(args:Array[String])={
// create instantiation for class
val fn = new Factorial_new()
println(fn.facto(5))
}
}
20
Concept of Private:
private can be accessible only an only within the class no outside
private can be accessible only an only within the class no outside class Factorial_new {
private val n = 5
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
object Prop {
def main(args:Array[String])={
val f = new Factorial_new()
f.n
}
}
f.n: here symbol is inaccessible from this place . due to private it can not be accessible outside the class.
class Factorial_new {
private val n = 0;
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
object Prop {
def main(args:Array[String])={
val f = new Factorial_new()
println(f.facto(6))
}
}
720
Concept of companion:
Companion when class and object share the same names.
class Factorial_new {
private val n = 0;
def facto(n:BigInt):BigInt = {
if(n==0)1 else n*facto(n-1)
}
}
object Factorial_new {
def main(args: Array[String]) = {
val f = new Factorial_new()
println(f.n)
}
}
0
Conditional expressions:
Whenever we want to use conditional expressions. Make sure that you are writing within the classes.
and if you are creating a function you can write within the within the singleton object.
if-else loop then you need to have a class.