OO Scala - Class Introduction

Question Click to View Answer

What does the following code print?

class User(n: String) {
  val name: String = n
}

var u = new User(n = "Frank")
println(u.name)

Frank is printed.

The User class is instantiated with the name of the user. The new keyword is used to instantiate the Scala classes, e.g. new User(n = "Frank").

What does the following code print?

class Carrot(val flavor: String)
var c = new Carrot(flavor = "weird")
println(c.flavor)

weird is printed.

The Carrot class is instantiated with a flavor value that is accessible to all instances.

What does the following code print?

class Cat(val name: String) {
  def greet: String = {
    s"My name is $name"
  }
}
var myCat = new Cat(name = "darla")
println(myCat.greet)

My name is darla is printed.

The Cat class is instantiated with a name value. The name value is used in the greet method to return a string. The myCat variable is assigned to an instance of the Cat class. The greet message is sent to the myCat instance using dot notation with this code: myCat.greet.

What does the following code print?

class Singer(var alias: String)
var singers = List(
  new Singer("miley"),
  new Singer("bradley")
)
println {
  singers map(_.alias)
}

List(miley, bradley) is printed.

The Singer class is defined to be instantiated with an alias string. A list of singers is created and the map function is used to create another list that contains the names of the singers.

The map method uses the shortcut placeholder syntax in this example. The following explicit syntax yields the same result:

println {
  singers.map { (s: Singer) =>
    s.alias
  }
}

What does the following code print?

class A {
  def hi = "Hello from A"
}
class B extends A

var bInstance = new B
println {
  bInstance.hi
}

Hello from A is printed.

Class B is defined to extend or inherit from class A. The methods defined in class A are available in class B as well.

What does the following code print?

class Tree {
  def about = "I am made of wood"
}

class Pine extends Tree {
  override def about = super.about + " and sticky!"
}

var aPine = new Pine
println(aPine.about)

I am made of wood and sticky! is printed.

The Tree class is defined with an about method.

The Pine class inherits from Tree and overrides the about method.

super.about refers to the about method in the parent class.

The string "and sticky!" is appended to the about method of the parent class.

What does the following code print?

class Mango(var status: String) {
  def updateStatus(s: String): Unit = {
    status = s
  }
}

var myMango = new Mango("ripe")
println(myMango.status)
myMango.updateStatus("rotten")
println(myMango.status)

The following strings are printed:

ripe
rotten

The Mango class is instantiated with ripe as the status. The updateStatus method is then called to change the status to rotten.

What does the following code print?

class Kitchen(color: String, floorType: String = "tile") {
  def describe = {
    s"The kitchen has $floorType floors"
  }
}

var myKitchen = new Kitchen("purple")
println(myKitchen.describe)

The kitchen has tile floors is printed.

The Kitchen class is instantiated with a color and floorType.

floorType is set to "tile" by default.

myKitchen is not instantiated with a value for floorType so the default value is used when the the describe method is called.

Define a Person class that is instantiated with firstName and lastName variables and has a fullName method that returns the first and last name.

class Person(firstName: String, lastName: String) {
  def fullName = {
    s"$firstName $lastName"
  }
}

var bob = new Person("bob", "loblaw")
println(bob.fullName)

The Person class is instantiated with firstName and lastName variables. The fullName method concatenates the first and last names and returns a string.