Assignment 2

Raffi Khatchadourian

September 22, 2020

  1. During lecture, we simulated a FileLogger trait using println(), which, obviously, does not input into a file. Implement a real Scala trait—based on our lecture—that logs to a file.

  2. During lecture, we discussed an object-level mix-in that logs both to a file and to the console:

    val acct3 = new SavingsAccount with ConsoleLogger with FileLogger
    1. Where does the log go to first, the file or the console? Why?
    2. How would you reverse the order?
    3. Add a time stamp to both logs using a TimestampLogger trait.
    4. Does it matter where you put TimestampLogger in the list of traits?
    5. What happens if you place TimestampLogger between ConsolLogger and FileLogger?
  3. Consider the following Scala code:

    var acct: SavingsAccount = new SavingsAccount
    val acct2: SavingsAccount = new SavingsAccount with FileLogger
    1. What is the relationship between the run-time types of the objects referred to by acct and acct2? Explain this relationship.
    2. Can acct2 refer to the object referred by acct? Why or why not?
    3. Can acct refer to the object referred by acct2? Why or why not?
  4. Explain the difference between the eq and equals methods.

    1. We discussed that application programmers should always use == instead of eq or equals. Can you think of a scenario where this is not true? Explain the use case.