简体   繁体   中英

Trouble with Scala's class and object

I am having trouble with finding the issue in my Scala code. Task below.

Input: val team1: Team = new Team(8,6) 
       val team2: Team = new Team(6,4)

Output for 'play' method: team1.score = 4
                          team2.score = 0

Output for 'Winner' method: team1 

Task:

Create a Scala class named "Team" and a Scala object named "Referee".

Team will have:

•State values of type Int representing the strength of 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

Referee will have:

•A method named "play" that takes two Team objects as parameters and does not return a value. 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 "Winner" 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

class Team(var offense: Int, var defense: Int){
      var score: Int = 0 //<-Updated Variable
}

object Referee{
def play(team1: Team, team2: Team) = {
    team1.score = team1.offense - team2.defense
    team2.score = team2.offense - team1.defense
    if (team1.offense < team2.defense){
    team1.score = 0
    }
    else if(team2.offense < team1.defense){
    team2.score = 0
    }
}
def Winner(team1: Team, team2: Team) = {
    if (team1.score > team2.score){
    team1
    }
    else if(team2.score > team1.score){
    team2
    }
    else{
    new Team(0, 0)
    }
}
}

Assignments don't have return values. So you're creating a new Team , and assigning it to the variable p , and then throwing it away.

Remove the val p = part.

The "task" that you set is inherently non-functional so it doesn't sit well with Scala. Just for fun, here is a more functional approach to the problem which avoids any mutable values:

case class Team(offense: Int, defense: Int) {
  def score(other: Team): Int = Math.max(0, offense - other.defense)
}

case class TeamScore(team: Team, score: Int) {
  def beats(other: TeamScore): Boolean = score > other.score
}

case class Result(score1: TeamScore, score2: TeamScore) {
  def winner: Option[Team] =
    if (score1 beats score2) {
      Some(score1.team)
    } else if (score2 beats score1) {
      Some(score2.team)
    } else {
      None
    }
}

object Referee {
  def play(team1: Team, team2: Team): Result =
    Result(
      TeamScore(team1, team1.score(team2)),
      TeamScore(team2, team2.score(team1))
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM