Object-Oriented Programming in Scala

Implementing Traits: The Adapter Pattern

An online weather station requires a thermometer that outputs the average of the temperatures in degrees Fahrenheit of all cities in a given list:

trait IThermometer {
            // = avg degrees Farenheit
            def getMeanTemperature(cities List[String]): Double
}

Assume the following class is given:

class CenTherm {
            // = degrees Centigrade
            def computeTemp(city: String) = { ... }
}

Show by implementation how would you use the Adapter Pattern to implement the IThermometer interface?

You can test your solution using a mock CenTherm object.

Console

Develop a resusable console class. Your class should provide a reusable control loop (i.e., repl) method that calls an abstract execute method. You should also provide reusable exception classes for different types of errors: severe, recoverable, warning.

Test your Console by developing a simple trig calculator.

A Model-View-Controller Framework

Develop a model-view-controller framework.

A model should provide a protected notify function that notifies all views if the model has changed. Consider using Java's Observer-Observable classes for this, or implement your own mechanism following the Publisher-Subscriber pattern. A model should also provide a save method that writes the model to a file. (So models must be serializable to do this.) A model should also provide a public Boolean flag: unsavedChanges. This flag is set to false in the save method and true in the notify method.

Implement Model.

A view is a trait that is linked to a model and redraws itself each time the model changes.

Implement View

A controller is also a trait linked to a model.

Implement Controller

Reactor Console

Test your MVC framework by implementing a system for controlling the temperature of a nuclear reactor.

The reactor will extend Model. It has a single attribute: temperature, and methods for incrementing and decrementing temperature by 50 degrees. These methods should also call the inherited notify method. Use a companion object to define a constant called criticalTemperature. The reactor should not be hotter than this. (Probably 1500 degrees is a good safe value.)

Create a console object that mixes the reusable Console class developed earlier and the Controller trait of the framework. Override the execute function to handle the increment and decrement commands.

An alarm is a view that warns the engineer that the reactor's temperature is too hot. Provide a variety of views, ones that beep and ones that pop up an Alert Dialogs with various messages.

Omega

Omega is an interpreted languages. Programmers type Omega phrases into a console. The phrases are executed. Executing a phrase produces a value (and may update variables and the environment as a side effect.) The console converts the value to a string, prints the string, and the cycle repeats.

Omega Values

Values in the Omega programming language are serializable and can be converted to strings. Examples of values include Booles, Numbers, Variables, and Functions.

A Number encapsulates a Scala Double. It implements the arithmetic operators: +, -, /, * and the comparison opertators: =, <, <=, >, >=.

A Boole encapsulates a Scala Boolean. It implements the arithmetic operators: &&, ||, and !.

A function encapsulates a Scala function and implements the apply operator ().

A Variable encapsulates another Omega value and provides getters and setters for this value.

Implement Value, Number, Boole, Function, and Variable.

Omega Environment

For now, an Omega environment simply extends Serializable and scala.collection.mutable.HashMap

Implement Environment.

Omega Phrases

An Omega phrase knows how to execute itself relative to a given environment. Examples of Omega phrases include expressions (no side effects), commands (which update variables), and definitions (which add new bindings to the given environment).

A numeral encapsulates a Number. Executing a numeral simply returns this number.

A Bool encapsulates a Boole. Executing a Bool simply returns this Boole.

Implement Phrase, Expression, Numeral, and Bool.

A FunCall has an operator (something of type List[Value]=>Value, for now) and operands (a list of expressions). Executing a FunCall means applying the operator to the list of Values obtained by mapping the execute method to the list of operands.

Implement FunCall.

Test your implementation by creating a few FunCall objects and executing them. For example: xor, square, min, sin, etc.