Instructions
Objective
Write a program to build a 4 function calculator in any control flow.
Requirements and Specifications
Introduction
You will write a 4 function calculator without using any control flow, meaning you cannot write any code that will be executed conditionally. Instead you will apply object-orientation programming approaches to make decisions based on types instead of values. Specifically, you are expected to use the state pattern to complete this assignment.
The following are banned in your submissions:
- Conditionals
- if/else if/else
- match/case
- throw/try/catch/finally (Can be used to simulate conditionals)
- Loops
- for
- while
- do/while
- Any way of directly simulating Conditionals or Loops that is against the spirit of thi assignment. (Ex. Taking advantage of short circuit evaluations) If your submission, including testing, contains any of the keywords listed above it will not be graded.
Source Code
package calculator.model
class Calculator() {
private var lhs: Double = 0.0
private var rhs: Double = 0.0
private var state: State = new EqualsState
// Accessed by View. You should edit this method as you build functionality
def displayNumber(): Double = {
rhs
}
def clearPressed(): Unit = {
state.clearPressed()
}
def numberPressed(number: Int): Unit = {
state.numberPressed(number)
}
def dividePressed(): Unit = {
state.dividePressed()
}
def multiplyPressed(): Unit = {
state.multiplyPressed()
}
def subtractPressed(): Unit = {
state.subtractPressed()
}
def addPressed(): Unit = {
state.addPressed()
}
def equalsPressed(): Unit = {
state.equalsPressed()
}
def decimalPressed(): Unit = {
state.decimalPressed()
}
abstract class State {
def clearPressed(): Unit = {
lhs = 0.0
rhs = 0.0
state = new EqualsState
}
def numberPressed(number: Int): Unit
def dividePressed(): Unit
def multiplyPressed(): Unit
def subtractPressed(): Unit
def addPressed(): Unit
def equalsPressed(): Unit
def decimalPressed(): Unit
}
abstract class OperationStartState extends State {
def dividePressed(): Unit = {
state = new DivideStartState
}
def multiplyPressed(): Unit = {
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
state = new SubtractStartState
}
def addPressed(): Unit = {
state = new AddStartState
}
def equalsPressed(): Unit = {
state = new EqualsState
}
}
class AddStartState extends OperationStartState {
def numberPressed(number: Int): Unit = {
rhs = 10 * rhs + number
state = new AddState
}
def decimalPressed(): Unit = {
state = new AddDecimalState
}
}
class SubtractStartState extends OperationStartState {
def numberPressed(number: Int): Unit = {
rhs = 10 * rhs + number
state = new SubtractState
}
def decimalPressed(): Unit = {
state = new SubtractDecimalState
}
}
class MultiplyStartState extends OperationStartState {
def numberPressed(number: Int): Unit = {
rhs = 10 * rhs + number
state = new MultiplyState
}
def decimalPressed(): Unit = {
state = new MultiplyDecimalState
}
}
class DivideStartState extends OperationStartState {
def numberPressed(number: Int): Unit = {
rhs = 10 * rhs + number
state = new DivideState
}
def decimalPressed(): Unit = {
state = new DivideDecimalState
}
}
abstract class OperationState extends State {
def numberPressed(number: Int): Unit = {
rhs = 10 * rhs + number
}
}
class AddState extends OperationState {
def dividePressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new MultiplyState
}
def subtractPressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new SubtractState
}
def addPressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs + rhs
state = new EqualsState
}
def decimalPressed(): Unit = {
state = new AddDecimalState
}
}
class SubtractState extends OperationState {
def dividePressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs - rhs
}
def decimalPressed(): Unit = {
state = new SubtractDecimalState
}
}
class MultiplyState extends OperationState {
def dividePressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs * rhs
}
def decimalPressed(): Unit = {
state = new MultiplyDecimalState
}
}
class DivideState extends OperationState {
def dividePressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs / rhs
}
def decimalPressed(): Unit = {
state = new DivideDecimalState
}
}
class EqualsState extends State {
def numberPressed(number: Int): Unit = {
rhs = 10 * rhs + number
}
def dividePressed(): Unit = {
lhs = rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
}
def decimalPressed(): Unit = {
state = new EqualsDecimalState
}
}
class EqualsDecimalState extends EqualsState {
var d = 10.0
override def numberPressed(number: Int): Unit = {
rhs = (math.round(rhs * (d.toInt / 10)) * 10 + number) / d
d *= 10
}
override def decimalPressed(): Unit = {
}
}
abstract class OperationDecimalState extends State {
var d = 10.0
def numberPressed(number: Int): Unit = {
rhs = (math.round(rhs * (d.toInt / 10)) * 10 + number) / d
d *= 10
}
def decimalPressed(): Unit = {
}
}
class AddDecimalState extends OperationDecimalState {
def dividePressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new MultiplyState
}
def subtractPressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new SubtractState
}
def addPressed(): Unit = {
lhs = lhs + rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs + rhs
state = new EqualsState
}
}
class SubtractDecimalState extends OperationDecimalState {
def dividePressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = lhs - rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs - rhs
}
}
class MultiplyDecimalState extends OperationDecimalState {
def dividePressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = lhs * rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
printf(lhs.toString)
printf("\n")
printf(rhs.toString)
printf("\n")
rhs = lhs * rhs
}
}
class DivideDecimalState extends OperationDecimalState {
def dividePressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new DivideStartState
}
def multiplyPressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new MultiplyStartState
}
def subtractPressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new SubtractStartState
}
def addPressed(): Unit = {
lhs = lhs / rhs
rhs = 0.0
state = new AddStartState
}
def equalsPressed(): Unit = {
rhs = lhs / rhs
}
}
}