Programmation appliquée en Scala

Copyright © Cay S. Horstmann 2015 Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License

Classes

Constructors

Classes—Differences from Java/C++

How Many Instance Variables?

class Mystery(val x: Double, y: Double) {
  val z = x + y
  def foo = x + z
}
  1. 1
  2. 2
  3. 3
  4. Something else

More About Methods

Objects

  1. Use object for singletons, static methods
    object Accounts {
      private var lastNumber = 0
      def newUniqueNumber() = { lastNumber += 1; lastNumber }
        // Aside: Use () since it mutates state
    }
  2. An object extending App is like main:
    object MyApp extends App {
       println(s"Hello, ${args(0)}!")
    }

Companion Objects

Traits

Reversing The Mixin Order

val pt2 = new Point(3, 4) with ShortLogger with TimestampLogger {
  override val maxLength = 20
}
pt2.move(5, 6)

What does it log?

  1. Wed Mar 11 20:17:01 CET 2015 Moving (3.0 ...
  2. Wed Mar 11 20:17:...
  3. Wed Mar 11 20:17:01 CET 2015 Moving (3.0, 4.0 ...
  4. Wed Mar 11 2...

Lab

Scary looking lab

Part 1: It's About Time

  1. Write a class Time with read-only fields hours and minutes, a method toString, and a method before(other: Time): Boolean that checks whether this time comes before the other. A Time object should be constructed as new Time(h, m), where h is between 0 and 23 and m between 0 and 59. If they aren't, call throw new IllegalArgumentException
  2. Construct a couple of Time objects and test your before method.
  3. Make it so that a full hour can be constructed as new Time(hrs). There are two different ways—what are they?

Part 2: Uniform Access

  1. In a new worksheet, reimplement the Time class from the preceding exercise so that the internal representation is the number of minutes since midnight (between 0 and 24 × 60 – 1). Do not change the public interface.

    Do not use var or val in the primary constructor!

    class Time(hours: Int, minutes: Int) {
      private val minutesSinceMidNight = ...
      ...
    }

    Supply parameterless methods hours, minutes

  2. Now we'll make this a little harder. In the original class, make hours and minutes into mutable fields, so that the following is ok:
    val start = new Time(13, 0)
    start.minutes = 15
    
    What did you have to do?
  3. Do the same for the reimplemented class. You need to provide a setter method with the special name property_=:
    def minutes_=(newValue: Int) { ... }
  4. In the original implementation, as changed in part 2b, it would have been possible to corrupt the field values by calling
    start.minutes = -100
    How can you avoid that in the modified implementation?
  5. Explain what “uniform access” means in this context. What changes does a programmer using the Time class have to make when switching from the original to the reimplemented class?
  6. Why are getters and setters less evil in Scala than in Java?

Part 3: Operators

Do the following with either the original or the reimplemented Time class (your choice).