Lab 2: Stormy Weather

26 January 2024

Today’s lab

The purpose of this lab is to give you practice:

  • using Boolean expressions,
  • reading and writing if expressions,
  • writing functions that use if expressions, and
  • writing examples/tests for the functions you write.

Contents

Pair programming

For this lab you’re expected to work with a partner!

This description is adapted from Wellesley CS111 and The National Center for Women and Information Technology. In pair programming, two programmers share one computer. One student is the “driver”, who controls the keyboard and mouse. The other is the “navigator”, who observes, asks questions, suggests solutions, and thinks about slightly longer-term strategies. The two programmers switch roles about every 20 minutes.

If you and your partner have different levels of experience and comfort with programming, you may worry that the less experienced partner will hold the other back or that there’s no benefit to participating actively, but pair programming studies show that paired work is consistently better than work the stronger partner does alone. It is each partner’s job to understand the whole task; that means asking questions when necessary and answering them when possible.

Working in pairs should make you better at programming than would working alone. The resulting work of pair programming nearly always outshines that of the solitary programmer, with pairs producing better code in less time.

Conveniently, you’ll notice the seats are in pairs, side-by-side, and there’s probably a person sitting in the seat next to you. Introduce yourself and try working together for today!

If there isn’t someone next to you, feel free to change seats. If there are an odd number of students, you can work in a group of three, just for today.

Part 1: If only it worked

You’ve been asked to review a program written by a coworker. The problem they were asked to solve was to return the storm category for a given wind speed measurement, using the Saffir–Simpson scale:

Category Wind speed (mph)
Tropical depression ≤ 38
Tropical storm 39–73
1 74–95
2 96–110
3 111–129
4 130–156
5 ≥ 157

Your coworker shows you this solution:

fun saffir-simpson(wind-speed :: Number) -> String:
  doc: "Saffir-Simpson category for a given wind speed in miles per hour"
  if wind-speed > 0:
    "Tropical depression"
  else if wind-speed > 38:
    "Tropical storm"
  else if wind-speed > 73:
    "Category 1"
  else if wind-speed > 95:
    "Category 2"
  else if wind-speed > 110:
    "Category 3"
  else if wind-speed > 129:
    "Category 4"
  else if wind-speed > 156:
    "Category 5"
  end
end

This looks good… but it doesn’t work.

Task: Answer the following questions in a multi-line Pyret comment, which you write like this:

#|
  This is my answer...
  continuing...
  as long as I need it to!
|#
  • Write down an example where the code does not return what you think it should. You only need to consider the function’s behavior for whole numbers, not decimals like 73.5 mph.
  • Why doesn’t this solution work as intended?
  • Why wouldn’t your coworker realize this function isn’t right? What should they have done when they were writing it that they skipped?

Now you know why this solution is incorrect, but can you fix it?

Task: Make a correct, complete version of the saffir-simpson function.

Feel free to start by copy-and-pasting the broken function.

Part 2: Where’s the storm?

Consider making a weather app that will send alerts when there’s a storm on campus. An important first step is asking whether a report of a storm is for an area on campus or not.

Task: Write the function

fun at-vassar(latitude :: Number, longitude :: Number) -> Boolean:
  ...
end

It should return true if the specified latitude and longitude correspond roughly to the Vassar campus, with

  • latitude between 41.656 and 41.693
  • longitude between -73.908 and -73.880

Be sure to test your function! You may want to use these coordinates:

Place Latitude Longitude
Main Building,
Vassar College
41.686804 -73.895664
Griffith Observatory,
Los Angeles
34.119108 -118.300388

Surprisingly, even though the at-vassar function is asking a true–false question, it doesn’t need an if expression!

Whenever you find yourself writing an if expression like

if 42 > 0:
  true
else:
  false
end

you can – and should – replace it with just the question:

42 > 0

This does the same thing – it returns true when the question is true and false when the question is false!

Task: If you used an if in your at-vassar function, rewrite the function to eliminate it!

Part 3: Extreme alert

A Category 5 storm is extremely dangerous, so we want to send everyone an alert if there’s a Category 5 storm over Vassar!

Task: Write the function

fun extreme-alert(wind-speed :: Number, latitude :: Number,
    longitude :: Number) -> Boolean:
  ...
end

that takes the current wind speed and the location of the wind (latitude and longitude), and returns true if the wind speed is "Category 5" on the Saffir–Simpson scale and the storm is over Vassar.

This function should be very short. As in Part 2, you don’t need to use an if. You also don’t need to deal with the wind speed or the latitude and longitude directly; instead, consider how you can use the saffir-simpson and at-vassar functions you’ve already written.

Part 4: Severe alert

Some people would like alerts even if a storm isn’t that severe, so we’ll make a function that checks if the wind speed corresponds to a Tropical Storm or a Category 1–5 hurricane and the classification is for Vassar.

Task: Write the function

fun severe-alert(wind-speed :: Number, latitude :: Number,
    longitude :: Number) -> Boolean:
  ...
end

that returns true if the storm is a categorized as a Tropical storm or a hurricane (1–5) and is over Vassar, and false otherwise.

Here are some tests you can try (or make your own!):

# Hurricane at Main Building
severe-alert(190, 41.686804, -73.895664) is true
# Gentler weather at Main Building
severe-alert(10, 41.686804, -73.895664) is false
# Hurricane at the Griffith Observatory, Los Angeles
severe-alert(190, 34.119108, -118.300388) is false

As in Part 3, you should be able to do this using the saffir-simpson and at-vassar functions you already wrote. Consider how you can keep the body of this function short using Boolean operators and functions.

Submitting the lab

  • When you’ve completed the exercises, show your code to your instructor or one of the coaches.
  • Then upload your lab02.arr file to the Lab 2 assignment on Gradescope. You should submit a single copy of the lab with both your names – see these instructions.