Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

language Scala In a package named Play create a Scala class named Team and a Scala object named Referee. Team will have: Two state values

language Scala

In a package named Play" create a Scala class named Team and a Scala object named Referee. Team will have: Two state values of type Int representing the strength of the team's offense and defense with a constructor to set these values. The parameters for the constructor should be offense first, then defense (Note: These values do not have defined names in this question so you cannot access them in your testing. If we use "teamOffense" and you name it "offense" and access it in your tests, your tests will crash when testing our code since the variable "offense" will not exist). A third state variable of type Int that is not in the constructor that represents the score of the team,is declared as a var, and is initialized to 0 (this variable also does not have a defined name). Referee will have: A method named "playGame" that takes two Team objects as parameters and return type is Unit. This method will alter the state of each input Teamby setting their scores equal to their offense minus the other Team's defense. If a Team's offense is less than the other Team's defense, their score should be 0 (no negative scores). A method named "declareWinner" that takes two Teams as parameters and returns the Teamwith the higher score. If both Teams have the same score, return a new Team object to indicate that neither competing team won (you may choose any values in the constructor call of this new Team).

Testing: In a package named tests" create a Scala class named "TestTeams" as a test suite that tests the functionality listed above.

Sample Usage:

val t1: Team = new Team(7, 3)

val t2: Team = new Team(4, 20)

Referee.playGame(t1, t2)

assert(Referee.declareWinner(t1, t2) == t2)

assert(Referee.declareWinner(t2, t1) == t2)

Note We create Team as a class since we want to create many objects of type Team that will compete against each other. Each team will have different state (offense, defense, score), but will be the same type (Team). Referee is an object since there only needs to be one of them and the object has no state. The same referee can officiate every game between any two teams. We pass references of objects of type Team to the Referee. Since the Referee has the references, when it changes the score of a Team, that change is made to the state of that Team throughout the program. This change can be tested by checking the reference returned by the declareWinner method since you cannot check the score directly

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions