This course has already ended.
You cannot submit this assignment

You need to sign in and enrol to submit exercises.

Assignment 4

Study the following class and the associated test app. Consider what happens when the app runs.

import scala.collection.mutable.Buffer

// In this toy program, we use a buffer of integers to represent the virtual genes of sheep.
class Sheep(val name: String, val genes: Buffer[Int]):

  // Causes a particular sort of "mutation" in a sheep's genes.
  def mutate() =
    this.genes(0) += 1
    this.genes(1) += 5

  /* Creates a clone of the sheep. The clone is given a new name; its genes
     are identical to those of the original sheep. Later mutations of the
     original sheep don't impact on the clone, and vice versa. */
  def clone(nameOfClone: String) = Sheep(nameOfClone, this.genes)

end Sheep


object SheepTest extends App:
  val first = Sheep("Polly", Buffer(2, 5))
  val second = first.clone("Dolly")
  second.mutate()
5 points

Only one of the following claims about the clone method is correct. Which one?

Posting submission...

Earned points

0 / 5

Exercise info

Assignment category
Graded
Your submissions
0 / 5
Deadline
Wednesday, 5 October 2022, 18:00
Late submission deadline
Saturday, 15 April 2023, 12:00 (-100%)
Group size
1-2
Total number of submitters
911