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

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
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,...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT