Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to print some attributes of employee along with method. but it is throwing and error.
But I am not able to understand this issue. can someone help to resolve the issue
Error
Error:(12, 43) not found: value grossSalary
println("Employee gross salary is : "+grossSalary)
Code
case class EmployeeGross (empId: Int,empName: String, deptId: Int, var basicSalary: Double) {
var Hra: Double= basicSalary*(30/100)
var Da: Double = basicSalary*(10/100)
var grossSalary: Double = basicSalary + Hra + Da
def grossSalary(basicSalary: Double) = basicSalary + Hra + Da
println("Employee salary information is :"+ empId,empName,deptId,basicSalary)
}
object EmployeeGross {
def main(args: Array[String]): Unit = {
val Eg = new EmployeeGross(1,"test",10,1200.2)
println("Employee gross salary is : "+ grossSalary)
}
}
You are trying to access grossSalary from the EmployeeGross object, probably thinking that this object knows which employee you have in mind. This is not possible to know, you have to use Eg.grossSalary.
Furthermore, you need to multiply by 0.3 and 0.1 rather than 10/100 and 30/100. This is because 10/100 performs integer division, which will yield 0. You could also fix this by writing 10.0/100 which will do floating point division.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
How to convert iterator of a case class to another case class and convert it to list.
Simply converting iterator into list using .toList does not work in scala 2.12
Error : value toList is not a member of Object
case class student(id int, name string)
case class studentNew(studentId int, name string)
val i: Iterator[student] = inputRec
val studentList: List[studentNew] = inputRec.map (s => studentNew(s.id, s.name))
// To convert to case class of id/name to studentId/name and make a list
version scala:2.12
This is a working snippet for 2.12
case class student(id: Int, name: String)
case class studentNew(studentId: Int, name: String)
val inputRec: Iterator[student] = ???
val studentList: List[studentNew] = inputRec.map (s => studentNew(s.id, s.name)).toList
You cant assign an Iterator to a List.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I'm trying to work out the purpose of this in classes. Consider the following example:
class TestMe {
var a: Int = 1
var b: Int = 2
fun setValA(value: Int) {
this.a = value
}
fun setValB(value: Int) {
b = value
}
}
val testInstance1 = TestMe()
val testInstance2 = TestMe()
testInstance1.setValA(3)
testInstance1.setValB(4)
println("instance 2A: ${testInstance2.a}, instance 2B: ${testInstance2.b}") // 1 2
println("instance 1A: ${testInstance1.a}, instance 1B: ${testInstance1.b}") // 3 4
It seems that I can simply omit this value and the results will be the same. Is there anything that I'm missing here?
Many thanks!
Yes, same as Java, but you will have a problem if a is the parameter name also:
fun setValA(a: Int) {
a = a
}
Compilation error:
Val cannot be reassigned
Then you will have to use this:
fun setValA(a: Int) {
this.a = a
}
In addition to user7294900's answer, this can only be omitted in this.<property or method name>, and there are plenty of other uses: e.g. this::class, or
fun doSomething(other: OtherClass) {
other.doSomethingWith(this)
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
so i have a task to make a deck of cards using scala. Im good at object oriented programming and so i made this in a few minutes. Now its time for me to learn functional programming. Oh boy.. Where do i begin with this? How do i even construct these cards? I was thinking maybe i shouls have 3 parallel arrays of information? For specific card id, face and suit? I can use enumerators for values but how do i actually initialize these arrays? Currently im stuck at using arrays. Maybe i should make a list? If so, how would i initialize them as well?
-Thank you!!
A possible implementation in scala:
// algebraic data types are usually used in scala for enum-like concepts
sealed trait Suit
object Suit {
case object Diamond extends Suit
case object Spade extends Suit
case object Club extends Suit
case object Heart extends Suit
val all = List(Diamond, Spade, Club, Heart)
}
sealed trait Card
case class NumberCard (number: Int, suit: Suit) extends Card {
require(number >= 1 && number <= 13, s"Invalid card number: $number")
}
case object Joker extends Card
val deck: List[Card] = Joker :: Joker :: (for {
suit <- Suit.all
number <- 1 to 13
} yield NumberCard(number, suit))
How you store these in collections depends on the operations that you need to perform.
Why not simply have an ID ( 0 to 51 for 52 cards ) and use this :
((ID % 13) + 1) = card value ( 1, 2, 3, ... 13 )
( ID / 13 ) = Suit ( 0,1,2,3 = Hearts, .... )
If you use Jokers, then (ID/13) would equal 4 for the 2 Jokers.
So you keep an array of 52/54 numbers which would be your deck. You keep all these Card ID in this array in any order you want.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
what is the type of this object?
class Zad1[A,B](val fst:A, val snd:B) {
override def toString: String = "(" + fst +","+snd+")"
}
object Zad1 {
def main(args: Array[String]): Unit = {
val v = new Zad1[Int, String](1, "2")
println(v)
}
}
I tried to print the class name with :
println(v.getClass) // would print: class $line8.$read$$iw$$iw$Zad1
The type of a singleton object is its singleton type, ergo, the type of Zad1 is Zad1.type.
This is related to how Scala REPL works.
Though you type just:
scala> class Zad1[A,B](val fst:A, val snd:B) {...}
REPL wraps it into a series of other objects($line8.$read.$iw.$iw), so getClass returns class $line8.$read$$iw$$iw$Zad1.
Read about it here:
Trying to understand how classes declared on the REPL are treated internally
If you run the same as a Scala program(not from REPL), getClass will return something much readable, e.g. class com.example.Zad1
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
According to this, Scala case classes automatically generates an equality method. However:
class SomeCaseClass(val string:String) {}
val a = "123"
assertTrue( a.equals( a ) ) // Passes
assertTrue( new SomeCaseClass(a).equals( new SomeCaseClass(a) ) ) // Fails, Scala 2.10
So, what does this automatically generated equals method do?
It's not a case class. That's the case class:
scala> case class SomeCaseClass(string: String)
defined class SomeCaseClass
scala> val a = "123"
a: String = 123
scala> SomeCaseClass(a) == SomeCaseClass(a)
res1: Boolean = true
== is just a syntax sugar for equals
In your example, you've just called equals (you'll get same result with ===) on regular object (this equals is not automatically generated), so it checked just referential equality.
P.S. You may notice that case class doesn't require new as it has automatically generated companion object. It doesn't require val in constructor as it assumes it by default. {} isn't mandatory for both regular and case classess.