CS 151 - Day 13

Cover page image

Cay S. Horstmann

Lab

lab

Preparation

  1. The first time, it takes a very long time to fetch all dependencies—do what's on this slide before the lab.
  2. Download SBT: https://www.scala-sbt.org/download.html On Ubuntu, follow the “Linux (deb)” instructions. I have no experience doing this on anything other than Linux, but this may help: https://www.scala-sbt.org/1.x/docs/Setup.html
  3. Run:
    cd cs151
    sbt new playframework/play-java-seed.g8 --name=lab13
    
  4. Run
    cd lab13
    sbt run
  5. Point your browser to localhost:9000. You should see a welcome screen.
  6. We want to build a clicker app where students can respond to a clicker question. We won't build a web UI on the server—that's so 1990s. Instead, we'll send commands to the server such as this one:
    http://localhost:9000/select/day13q1/C
    
    To do that, you need to Add this method to the HomeController class:
    public Result select(String problem, String choice) {
       return ok("You selected " + choice + " for " + problem + "\n").as("text/plain");
    }
    
  7. Save the file. Point your browser to http://localhost:9000/select/day13q1/C. What happens?
  8. Look at the section “The routes file syntax” in this page. Then fix up conf/routes so that your method gets called. What did you do?
  9. What output do you get now for http://localhost:9000/select/day13q1/C
  10. Add this field to HomeController:
    private static HashMap<String, HashMap<String, Integer>> results = new HashMap<>();
    
    In the select method, add this code before the return statement:
    if (results.get(problem) == null) 
       results.put(problem, new HashMap<>());
    if (results.get(problem).get(choice) == null) 
       results.get(problem).put(choice,  0);
    results.get(problem).put(choice, 
       results.get(problem).get(choice) + 1);
    What does the code do?
  11. Add another method public Result view(String problem) that yields the map of counts for the given problem. Just turn the map results.get(problem) into a string. What is your method?
  12. Add a route to it. Point your browser to
    http://localhost:9000/select/day13q1/C
    http://localhost:9000/select/day13q1/A
    http://localhost:9000/select/day13q1/B
    http://localhost:9000/select/day13q1/C
    http://localhost:9000/view/day13q1
    
    What happens?

A Web Framework