Studio 9: Map Practice¶
Studio activities should not be started before class! Come to the session and work on the activity with other students!
Studio Setup and Procedure¶
Form a group of 2-3 students and find a TA or instructor to work with.
All but one member of your group should have this web page open so you can follow along and see the instructions as you work.
Plan to work on one computer (using Eclipse).
Initially, one of you will be in charge of typing at that computer.
Throughout the studio you should trade who is in charge of the keyboard.
READ THE FOLLOWING FULLY BEFORE PROCEEDING
Have one person in your group create a new team by clicking here and going to the
OR Create a new teambox at the bottom of the page. The team name should include the last names of all your group members. For example, if Xia and Smith are working together, the team name should be something like “XiaSmith”.After the team is created, all other members of your team should click on the same link and follow the instructions to join the team.
Be careful to join the right team! You won’t be able to change teams yourself and will have to contact instructors if there’s a problem.
Be sure everyone else joins the team! If grades don’t get entered correctly we will use the team to help verify credit for a particular studio.
Finally, one person should import the studio repository into Eclipse, as described in the setup instructions.
All team members will have access to the work pushed to GitHub. Be sure to
Commit and Pushat the end of the day so everyone can refer back to the work later as needed.
Code To Use¶
Map Name to Height Warmup¶
Open the Java file NameToHeight in the studio9 package and edit the main method.
2. Create a new instance of a Map from key type String to value type Integer. Note: Map<K,V> is an interface so you will need to create an instance of a class which implements it.
Associate each member of your team’s name with their height (use inches or centimeters at your preference).
4. Repeatedly prompt the user (via Scanner) for a name. If the user cancels by typing “quit” then stop prompting for more names. Otherwise, look up the name in the map for the associated height. Print out both the name and the height.
Be sure to handle the null case for names that are not in the map and print an appropriate message.
Explain your solution to your TA before moving on.
Word Count¶
This section of the studio can be tested with WordCountTestSuite in the studio9 package.
Open the Java file
WordCountin thestudio9package.Implement the
countWords(words)method to meet this specification:
/**
* Constructs and returns a map of the distinct words in the specified list with
* each word associated with its accumulated count (that is: the number of
* occurrences of the word in the list).
*
* For example, if passed a List<String> containing:
*
* [to, be, or, not, to, be]
*
* the resulting Map<String,Integer> would contain
*
* key="to", value=2;
* key="be", value=2;
* key="or", value=1;
* key="not", value=1;
*
* @param words
* @return a map which contains all of the distinct words as keys, each
* associated with the number of occurrences of the word
*/
Ensure that WordCountTest is working and you have explained your solution to your TA before moving on.
University Database¶
This section of the studio can be tested with UniversityDatabaseTestSuite in the studio9 package.
UniversityDatabasewill rely on a working implementation of theStudentclass from Assignment 7. Copy the code from your Student class into theStudent.javafile in thestudio9package and make sure any errors are resolved before proceeding. You may use which ever implementation you feel is best.Open
UniversityDatabasein thestudio9package.Declare a single
private finalinstance variable of typeMap<String, Student>.You’ll need to import that
Mapinterface (java.util.Map).
Implement the default constructor (
UniversityDatabase()) to initialize the map instance variable.Implement
public void addStudent(String accountName, Student student)which associates with the keyaccountNamethe valuestudentin the map.Implement public int
getStudentCount()Implement
public String lookupFullName(String accountName)by looking up the student and then using thegetFullName()method onStudent. If the student isnull, you should returnnullas the full name.Implement
public double getTotalBearBucks()via iterating over the map.
Ensure that UniversityDatabaseTestSuite is working and you have explained your solution to your TA before moving on.
Demo¶
Commit and Push your work. Be sure that any file you worked on is updated on GitHub.
To get participation credit for your work talk to the TA you’ve been working with and complete the demo/review process. Be prepared to show them the work that you have done and answer their questions about it!
Before leaving check that everyone in your group has a grade recorded in Canvas!