Object-Oriented Programming in Scala

Problem 1: Dungeons & Dragons

In Dungeons and Dragons a knight battles a dragon in a dungeon until one (or both) dies.

Create a Scala project called DnD. Add Dragon and Knight classes and a Dungeon object with a main method. The Dungeon object should also contain a random number generator:

val random = new scala.util.Random(System.nanoTime())

Dragons and Knights have names and health. Health is an integer with initial value 100. When health reaches 0, the knight or dragon dies. Each has an attack method with a victim parameter. The dragon's victim is a knight, the knight's is a dragon.  The attack method prints a message ("name of attacker is stabbing/flaming name of victim"), calculates a random amount of damage less than the attacker's health, then decrements the victim's health by the damage.

Dungeon.main should create a dragon and a knight, have both attack each other until one dies, then print the health of both.

Implement Dragon, Knight, and Dungeon. Use built-in setters and getters to access health and name. Of course name should not have a setter.

Problem 2: Generics

Create and test a generic queue class with operations enqueue (add an item to the rear), dequeue (remove item from the front), isEmpty, and toString. Consider storing queue elements in a private array buffer:

import scala.collection.mutable.ArrayBuffer

Create a companion object with apply and main methods. In main create a queue of names called waitingList. Add five names, print the queue, then iteratively remove and print items from the queue until it is empty.

Problem 3:

Complete the implementation of Indus

Problem 4:

Complete the implementation of Acorn.

Problem 5:

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.

Projects

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.