0
votes

I'm doing some exploratory analysis using a Kaggle dataset with Scala. One of the things I have to do is create a companion object for a case class called "Taxpayer", as follows:

case class Taxpayer(
                age: Int,
                workclass: String,
                education: String,
                educationNum: Int,
                maritalStatus: String,
                occupation: String,
                relationship: String,
                race: String,
                sex: String,
                capitalGain: Int,
                capitalLoss: Int,
                hoursPerWeek: Int,
                nativeCountry: String,
                income: String
              )

Given this case class, I have to create a companion object and define the following functions:

  • printData: print the following format "$workclass - $occupation - $nativeCountry - $income"
  • apply: that takes no parameters and returns an instance of the class Taxpayer with Int fields = -1 and String fields = "unknown"

I have tried the following, but it doesn't work ("expected class or object definition"):

object Taxpayer{


def printData(c: Taxpayer) = {
    println(s"${c.workclass} - ${c.occupation} - ${c.nativeCountry} - ${c.income}")
    def apply(age: Int = -1, workclass: String = "unknown", education: "unknown", educationNum: Int = -1, maritalStatus: "unknown",
               occupation: "unknown", relationship: "unknown", race: "unknown", sex: "unknown", capitalGain: Int = -1,
               capitalLoss: Int = -1, hoursPerWeek: Int = -1, nativeCountry: "unknown", income: "unknown") = {
      new Taxpayer(age, workclass, education, educationNum, maritalStatus, occupation, relationship,
        race, sex, capitalGain, capitalLoss, hoursPerWeek, nativeCountry, income)
    }
  }
}

How can I define a function that works with my dataset's rows? Thank you in advance! :)

1

1 Answers

1
votes

Try

object Taxpayer{
  def printData(c: Taxpayer) = {
    println(s"${c.workclass} - ${c.occupation} - ${c.nativeCountry} - ${c.income}")
    def apply(age: Int = -1, workclass: String = "unknown", education: String = "unknown", educationNum: Int = -1, maritalStatus: String = "unknown",
              occupation: String = "unknown", relationship: String = "unknown", race: String = "unknown", sex: String = "unknown", capitalGain: Int = -1,
              capitalLoss: Int = -1, hoursPerWeek: Int = -1, nativeCountry: String = "unknown", income: String = "unknown") = {
      new Taxpayer(age, workclass, education, educationNum, maritalStatus, occupation, relationship,
        race, sex, capitalGain, capitalLoss, hoursPerWeek, nativeCountry, income)
    }
  }
}

It compiles in 2.13.2 https://scastie.scala-lang.org/3zq3SDzVTOuHtpgC6nyPIA

If you want to declare method parameter with default value you should do this like education: String = "unknown". When you write education: "unknown" you do something different (you declare parameter of singleton type without default value).