Homework 4 Redux
Homework 4: Webkinz Redux
Setup and Handin
Setup
In order to setup homework4, download homework4_scala
. Place the
hw4
folder into your cs112 Project folder in InteliJ.
Handin
Handin the following files to gradescope when submitting the assignment:
Webkinz.scala
BigWebkinzHouse.scala
README.txt
Remember to include a README.txt when submitting your assignment!
Staff Assistance
Your friendly TAs and Professor are here to help with this assignment! You can find our hours schedule here.
The Assignment
Finishing the Scala lab
Before you begin this problem, please finish the Scala
Lab. You
should have a Webkinz class that takes in the arguments: name: String
and
species: String
. The class should also have a favoriteFoods: Array
attribute
and a numFood: Integer
attribute. In the lab, we tell you to define
favoriteFoods
as a val. Change this to be a var
for this assignment!
Once you have completed the Scala Lab, move your Webkinz.scala
to the hw4
package in order to begin the Scala section of this homework.
Big Webkinz House
We would like you to write several methods in the BigWebkinzHouse
class in
BigWebkinzHouse.scala
. These functions will access the list of Webkinz stored
in the house, webkinzList
. For several of the methods, the Scala List
Documentation
may be useful.
resetFood
The resetFood(): Unit
method should clear the numFoods
and favoriteFoods
field
of each Webkinz in webkinzList
(i.e., it should set numFoods
to 0 and
favoriteFoods
to an empty Array
).
You should use a for-loop to implement this method.
feedAll
The feedAll(food: Map[String,String]): Unit
method takes in a Map
assigning
Webkinz names to food items. It should feed (i.e., call the feed
method) each
Webkinz in webkinzList
its corresponding food from food
; if a Webkinz isn’t
in food
, it shouldn’t be fed.
For instance, if Webkinz named Alex and Brantley are in the house and we
call house.feedAll(Map("Brantley" -> "scrambled eggs", "Doug" -> "scrambled
tofu"))
, you should feed Brantley "scrambled eggs"
.
You should use a for-loop to implement this method.
totalFood
The totalFood(species: String): Int
method should return the total of the
numFoods
fields of every Webkinz in the house of the given species
.
You should use list methods to implement this method.
isHungry
The isHungry(name: String, species: String): Boolean
method should return true
if there is a Webkinz in the house with the given species and name whose
numFoods
is less than 10, and false
otherwise.
You should use list methods to implement this method.
names
The names(allSpecies: Set[String]): List[String]
method should return all of
the names of Webkinz in the house whose species is contained in allSpecies
.
You should use list methods to implement this method. You can test if an element
is in a Set
using the contains
method.
Testing
For this assignment (and only this assignment!), we aren’t asking you to write
tests. We have included a Main.scala
file; you can edit and run this file to
experiment with the methods you’ve written.
README
In your README.txt
, please include answers to the following questions:
- We asked you to use a for-loop for some methods and
List
methods for others. What do the for-loop methods have in common? What do the other methods have in common? - The
webkinzList
field of theBigWebkinzHouse
class is aval
even though some of the methods modify its contents. Why does Scala allow this? - Did you discuss this assignment with any other students? Please list their cs logins.
- How many late days are you using on this assignment?