Question

In: Computer Science

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 inputs objects score, false otherwise

In a package named "tests" create a Scala class named "TestScore" as a test suite that tests all the functionality listed above

Solutions

Expert Solution

Note:

Please use below scala version and package to run the code, update your build.sbt with below code.

name := "scala-interview"

version := "0.1"

scalaVersion := "2.11.7"

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"

Scala code(Score.scala):

package oop

class Score(x:Int) {
  //Assigning to member variable
  var score:Int = x

  //Increase score value by 1 when player hits goal
  def scoreGoal: Unit = score +=1

  //Winner check
  def isWinner = if(score > x) true else false

}

object oop {

  def main(args: Array[String]): Unit = {

    //Initialize Score class for player1 and player2
    val player1 = new Score(0)
    val player2 = new Score(0)
    //player 1 score a goal
    player1.scoreGoal
    // Results
    println("Player1 score result: " + player1.isWinner)
    println("Player2 score result: " + player2.isWinner)
  }
}

Code with result:

Test cases:

package tests

import org.scalatest._
import oop.Score

class TestScore extends FlatSpec with Matchers {
    val player1 = new Score(0)
    val player2 = new Score(0)

  "Player2 hits NO goal" should "Initial goal is 0 and final score also 0 1.Winner is false" in {
    player1.isWinner should be (false)
  }

  "Player1 hits first goal" should "Initial goal is 0 and after the goal its 1.Winner is true" in {
    player1.scoreGoal
    player1.isWinner should be (true)
  }


  "Player2 hits NO goal" should "Initial goal is 0 and score also 0 .Winner is false" in {
    player2.isWinner should be (false)
  }

  "Player2 hits a goal" should "Initial goal is 0 and score 1 .Winner is True" in {
    player2.scoreGoal
    player2.isWinner should be (true)
  }

}

Test case code screenshot:

Testcases result summary:


Related Solutions

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...
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...
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...
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
Create a class named Employee and its child class named Salesperson. Save each class in its...
Create a class named Employee and its child class named Salesperson. Save each class in its own file. Name your class and source code file containing the main method homework.java. Make sure each class follows these specifications: 1. An employee has a name (String), employee id number (integer), hourly pay rate (double), a timesheet which holds the hours worked for the current week (double array) and email address (String). A salesperson also has a commission rate, which is a percentage...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT