Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help creating a program using Scala. object subtyping { // Instances of C

ID: 3905169 • Letter: I

Question

I need help creating a program using Scala.

object subtyping {

// Instances of Counter have a integer field that can be incremented, decremented, or read.
class Counter {
    private var n = 0
    def increment () = { n = n + 1 }
    def decrement () = { n = n - 1 }
    def get () : Int = n
}

// EXERCISE 1: complete the following function.
// The observeCounter function has one parameter f: a function that accepts (a reference to) a Counter instance but returns nothing.
// The observeCounter function should call f with (a reference to) an object (of a class extending Counter).
// Your class that extends Counter must keep track of the total number of times that increment/decrement have been called.
// I.e., if the increment method is called 3 times on an instance, and the decrement method is called 2 times on the same instance, then it should store 5 (somewhere other than the existing field n).
// observeCounter should call f, and then return the total number of times that increment/decrement were called on the instance by f.
def observeCounter (f : Counter => Unit) : Int = {
    // TODO: Provide definition here.
    -1
}

// EXERCISE 2: complete the following function.
// It is the same as observeCounter except that f has a parameter of type List[Counter] not Counter.
// f will insist that the List[Counter] has length 3.
// You must return a List[Int] not an Int.
// The first element of the result List[Int] must correspond to the number of times that increment/decrement were called on the first element of type List[Counter], similarly for the second and third elements.
def observeCounterList (f : List[Counter] => Unit) : List[Int] = {
    // TODO: Provide definition here.
    List (-1, -1, -1)
}

// EXERCISE 3: complete the following function.
// It is the same as observeCounterList except that f has a parameter of type Array[Counter] not List[Counter].
// f will insist that the Array[Counter] has length 3.
// You must return a Array[Int] not a List[Int].
// The first element of the result Array[Int] must correspond to the number of times that increment/decrement were called on the first element of type Array[Counter], similarly for the second and third elements.
def observeCounterArray (f : Array[Counter] => Unit) : Array[Int] = {
    // TODO: Provide definition here.
    List (-1, -1, -1).toArray
}
}

Explanation / Answer

object subtyping {

// Instances of Counter have a integer field that can be incremented, decremented, or read.
class Counter {
    private var n = 0
    def increment () = { n = n + 1 }
    def decrement () = { n = n - 1 }
    def get () : Int = n
}

// EXERCISE 1: complete the following function.
// The observeCounter function has one parameter f: a function that accepts (a reference to) a Counter instance but returns nothing.
// The observeCounter function should call f with (a reference to) an object (of a class extending Counter).
// Your class that extends Counter must keep track of the total number of times that increment/decrement have been called.
// I.e., if the increment method is called 3 times on an instance, and the decrement method is called 2 times on the same instance, then it should store 5 (somewhere other than the existing field n).
// observeCounter should call f, and then return the total number of times that increment/decrement were called on the instance by f.
def observeCounter (f : Counter => Unit) : Int = {
    class Count extends Counter {
       private var x = 0
       private var n = 0
       override def increment () = {
           n = n + 1
           x = x + 1
       }
       override def decrement () = {
           n = n - 1
           x = x + 1
       }
       override def get () : Int = {
           x
       }
   }
   val y = new Count
   f(y)
   val a : Int = y.get
   a
}

// EXERCISE 2: complete the following function.
// It is the same as observeCounter except that f has a parameter of type List[Counter] not Counter.
// f will insist that the List[Counter] has length 3.
// You must return a List[Int] not an Int.
// The first element of the result List[Int] must correspond to the number of times that increment/decrement were called on the first element of type List[Counter], similarly for the second and third elements.
def observeCounterList (f : List[Counter] => Unit) : List[Int] = {
   class Count extends Counter {
       private var x = 0
       private var n = 0
       override def increment () = {
           n = n + 1
           x = x + 1
       }
       override def decrement () = {
           n = n - 1
           x = x + 1
       }
       override def get () : Int = {
           x
       }
   }
   val x = new Count
   val y = new Count
   val z = new Count
    val countList = List (x, y, z)
   f(countList)
   val a = List(x.get, y.get, z.get)
   a
}

// EXERCISE 3: complete the following function.
// It is the same as observeCounterList except that f has a parameter of type Array[Counter] not List[Counter].
// f will insist that the Array[Counter] has length 3.
// You must return a Array[Int] not a List[Int].
// The first element of the result Array[Int] must correspond to the number of times that increment/decrement were called on the first element of type Array[Counter], similarly for the second and third elements.
def observeCounterArray (f : Array[Counter] => Unit) : Array[Int] = {
    class Count extends Counter {
       private var x = 0
       private var n = 0
       override def increment () = {
           n = n + 1
           x = x + 1
       }
       override def decrement () = {
           n = n - 1
           x = x + 1
       }
       override def get () : Int = {
           x
       }
   }
   val x = new Count
   val y = new Count
   val z = new Count
   val countArray:Array[Counter] = Array (x, y, z)
   f(countArray)
   val a = Array (x.get, y.get, z.get)
   a
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote