Subtyping in scala program | Computer Science homework help




 * Complete the exercises below.  For each “EXERCISE” comment, add

 * code immediately below the comment.


 * Please see for instructions, including compilation and testing.




 * 1. Submissions MUST compile using SBT with UNCHANGED configuration and tests with no

 *    compilation errors.  Submissions with compilation errors will receive 0 points.

 *    Note that refactoring the code will cause the tests to fail.


 * 2. You MUST NOT edit the SBT configuration and tests.  Altering it in your submission will

 *    result in 0 points for this assignment.


 * 3. You MUST NOT use while loops or (re)assignment to variables (you can use “val” declarations,

 *    but not “var” declarations).  You must use recursion instead.


 * 4. You may declare auxiliary functions if you like.




 * 1. Push your local repository to the repository created for you on Bitbucket before the deadline.


 * 2. Late submissions will not be permitted because solutions will be discussed in class.



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 = {

val o1 = new Counter()



    // 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



Essay Help
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.


Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.


Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.


Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.


Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.