==
and eq
?s
below?result
below?val
and a var
?xs
.Map
referenced by a val
named langs
such thatscala> langs("Lisp")
res0: String = John McCarthy
scala> langs("Java")
res1: String = James Gosling
scala> langs("Pascal")
res2: String = Niklaus Wirth
scala> langs("Scala")
res3: String = Martin Odersky
langs
from the previous question to create a Seq
of the last names of the values in langs
, that is, List(McCarthy, Gosling, Wirth, Odersky)
.Write a function called mean
that takes a variable number of Double
parameters and returns their arithmetic mean (sum divided by number of numbers).
Given xs: List[Int]
, write an invocation of the filter
method on xs
that passes a function literal which selects only the odd numbers in xs
.
Write a function named listToString[T]
that takes a single List[T]
and returns a string representation of the List[T]
. The listToString
function should have a single expression which calls a nested helper function which is recursive and uses pattern matching to recursively accumulate a string representation. Your helper function may use an if statement instead of pattern matching for partial credit.
Write the minimal Scala definition of a (non-case) class named Item
which has two fields, name
of type String
and hauptstadt
of type String
.
Write an equals
method for the Item
class above using the recipe we discussed in class.
Write a hashCode
method for the Item
class above using the recipe we discussed in class.
Write a companion object for the Item
class above with a factory method that allows us to create an Item
object with expression like Item("Key Lime", 3.14)
(leaving off operator new
).
Given the Person
class below, write the minimal non-final subclass of Person
named Employee
that adds a mutable salary: Double
field and initializes the fields defined in Person
.
next
which takes a single parameter fußgängerampel: AmpelMann
and returns the next AmpelMann
in the Grün -> Rot -> Grün
cycle. Your next
function body should be a single match
expression.def hauptstadt(land: Bundesland) = land match {
case land: Berlin => "Berlin"
case land: Brandenburg => "Potsdam"
case land: MecklenburgVolpommern => "Schwerin"
case land: SachsenAnhalt => "Magdeburg"
case land: Sachsen => "Dresden"
case land: SchleswigHolstein => "Kiel"
case land: FreieHansestadtHamburg => "Hamburg"
case land: HansestadtBremen => "Bremen"
case land: Niedersachsen => "Hannover"
case land: NordrheinWestfalen => "Düsseldorf"
case land: FreistaatThüringen => "Erfurt"
case land: Hessen => "Wiesbaden"
case land: RheinlandPfalz => "Mainz"
case land: Saarland => "Saarbrücken"
case land: BadenWürttemberg => "Stuttgart"
case land: FreistaatBayern => "München"
}