Question

In: Computer Science

Question: In a package named "oop" create a Scala class named "Team" and a Scala object...

Question: In a package named "oop" create a Scala class named "Team" and a Scala object named "Referee". Team will have:

• 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 then defense

• A third state variable named "score" of type Int that is not in the constructor, is declared as a var, and is initialized to 0 Referee will have:

• A method named "playGame" that takes two Team objects as parameters and return type Unit. This method will alter the state of each input Team by 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 Team with the higher score. If both Teams have the same score, return a new Team object with offense and defense both set to 0

Solutions

Expert Solution

Input:

Team.scala:

package oop

class Team(val offense:Int, val defense:Int){ //Data class with parameterized constructor
  var score: Int = 0 //local variable
}

object Referee{

  def playGame(team_1:Team, team_2: Team):Unit = { //Method: playGame with return type Unit
    /*
    Single line if else statement to assign score
    If offense is less than other team's defense, assign 0
    Otherwise perform offense - defense
     */
    team_1.score = if(team_1.offense < team_2.defense) 0 else team_1.offense - team_2.defense
    team_2.score = if(team_2.offense < team_1.defense) 0 else team_2.offense - team_1.defense
  }

  def declareWinner(team_1:Team, team_2: Team):Team = { //Method: declareWinner return type Team
    if (team_1.score == team_2.score) //Check if both teams have same score
      return new Team(0, 0) //Return object with offense and defense as 0

    if (team_1.score > team_2.score) { //Check if first team won
      println("Team 1 won!")
      return team_1 //return object of first team
    }

    println("Team 2 won!")
    return team_2 //Otherwise return object of second team
  }

  def main(args: Array[String]): Unit = { //main method
    val team_1 = new Team(5,2) //create object for 1st team
    val team_2 = new Team(3,7) //create object for 2nd team

    playGame(team_1, team_2) //call playGame
    val winner = declareWinner(team_1, team_2) //Return object of winning team

  }
}

Output:

Explanation:

  • The given code has a class named Team and an object named Referee.
  • The class has three member variables: offense, defense and score. offense and defense are not modifiable. They have been initialized through parameterized constructor. score is a local variable of class Team.
  • Referee has 2 methods: playGame and declareWinner.
  • playGame is taking two team objects as input and modifying their scores according to offense and defense.
  • declareWinner is checking for winning object based on score and returning it
  • main() method is responsible for the execution of the program.
  • In the given screenshot of output, you will find a directory structure on the left where the code is residing in "oop" package. So make sure you follow a similar directory structure in order to create package.

Related Solutions

In a package named "oop" create a Scala class named "Score" with the following: • A...
In a package named "oop" create a Scala class named "Score" with the following: • A constructor that takes an Int and stores it in a member variable named score • A method named scoreGoal that takes no parameters and has return type Unit that increments the score by 1 • A method named isWinner that takes a Score object as a parameter and returns a Boolean. The method returns true if this instances score is strictly greater than the...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Instructions 1. Create a class named after your favorite object in the whole world. For example,...
Instructions 1. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method (1 point) 2. Create Instance Variables (attributes) (2 point) Create at least 3 private instance fields (attributes) for your class You must use at least 3 different data types for your fields 3. Create getter (accessor) and setter (mutator) methods   Create a getter (accessor) method for...
Design a class named Account (put it in a package named accountspackages) with the following UML...
Design a class named Account (put it in a package named accountspackages) with the following UML diagram: Account -customerID: int -customerName: String -balance: double +setCustomerID(int): void +setCustomerName(String): void +setBalance(double):void +getCustomerID(): int +getCustomerName(): String +getBalance(): double +deposit(double): void +withdraw(double): void +printInformation():void The method withdraw(double) withdraws a specified amount from the account if the amount is less than or equal the balance, otherwise the method prints the message: Sorry! The account does not have sufficient funds. The method printInformation() prints:     the...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
In java: -Create a class named Animal
In java: -Create a class named Animal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT