HTW Berlin Medieninformatik HTW Berlin
Fachbereich 4
Internationaler Studiengang
Internationale Medieninformatik (Bachelor)
Info 1: Informatik I
Winter Term 2022/23

Laboratory 5 : Lambda Functions

 

Pre-lab

This week's lab work is intended help you better understand lambda functions.

What to Bring to Lab

Please bring these exercises printed out or written out with you to lab. Please have your name on your page.

P1. If you have a collection called myList, what Java code would you have to write to apply some code to each of the members in the list?

P2. Given the following code:

   public void printStudent (Student s)
   {
       System.out.println (s.getDetails());
   }

What is the equivalent lambda in Java? What syntax variations are possible?

P3. Given the animal-monitoring-v1 example, write pseudo-code for determining how many elephants a particular spotter saw on a particular day.

P4. Given the animal-monitoring-v1 example, write pseudo-code to create a stream containing only those sightings that have a count greater than 0.

 

Post-Lab, AKA  What To Turn In

Your completed assignment, submitted in Moodle as a pdf, should include:

Lab assignments are due before your next lab at 22.00. They may, of course, be turned in earlier. You hand them in by preparing the report in PDF and submitting it to Moodle.


Assignment

Seeing Elephants

  1. Download the animal-monitoring-v1 project and check that it runs. Now rewrite the printList method in the AnimalMonitor class to use a lambda, just as we did in the lecture. Now apply each of the variations that you recorded in P2, compile them and run them. Did they all work? Record the results for your lab report.
  2. Rewrite the printSightingsOf method in the AnimalMonitor class to use streams and lambdas. Test to make sure that your project still works as before.
  3. Write a method in the AnimalMonitor class to print the details of all the sightings recorded on a particular dayID, which is passed as a parameter to the method.
  4. Write a method that used two filter calls to print details of all the sightings of a particular animal made on a particular day—the method takes the animal name and the day ID as parameters. Does the order of the two filter calls matter in your solution? Justify your answer.
  5. Write a method to print the counts of all sightings of a particular animal. Your method should use the map operations as part of the pipeline. If a pipeline contains a filter operation and a map operation, does the order of the operations matter to the final result? Justify your answer.
  6. Rewrite the printEndangered method in your project to use streams and test that it works correctly. Detail how you tested this method in your report.
  7. (For the bored) There is a special "::" notation in Java that can be used with lambdas. Research the syntax, and rewrite printSightingsBy to use this syntax. Does the operation of the method change?
  8. (For the bored) Add a method to AnimalMonitor that takes two parameters, a spotter-ID and a day-ID and returns a String containing the names of the animals seen by the spotter on that particular day. You should include only animals whose sighting count is greater than zero. Now add a method that takes an animal and a day-ID and returns the spotters who saw this animal, if any, on that particular day.

Writeup

Submit your writeup as your post-lab to the Moodle area by 22.00 of the night before your next lab.

 


Copyright Prof. Dr. Debora Weber-Wulff
Questions or comments: <weberwu@htw-berlin.de>
Some rights reserved. CC-BY-NC-SA - Copyright and Warranty

The exercises are adapted from Objects First with Java, A Practical Introduction Using BlueJ. David Barnes & Michael Kölling, 2016