Discussion 1: Coding in IntelliJ

The goal of today’s discussion is to get you set up with the tools that you’ll need for programming in this class. This includes setting up your IntelliJ environment, opening a project in IntelliJ, writing code, and running unit tests. You’ll also get practice with Java syntax for some foundational control structures such as loops and conditional statements.

Learning Outcomes

  1. Set up and author Java code in the IntelliJ IDEA IDE.
  2. Develop methods in Java from their specification that incorporate one or more (possibly nested) control structures such as if-else statements, for loops, and while loops.

Before Discussion

Reminder: Discussion Guidelines

The work that you complete in discussion serves as a formative assessment tool; it offers the opportunity to assess your understanding of the material and for our course staff to get a “pulse” on how things are going, so we can make adjustments in future classes. It’s also a great place to practice technical communication and collaborative problem solving. You should complete this activity in a group comprising 2-3 students. At the end of class, your discussion TA will check off your progress on the activity and report your score for the day out of 4 points, with 2 points allocated to your presence and engagement during class and 2 points allocated for your completion of that day’s activity. More information about the grading and expectations can be found in the syllabus.

This discussion is meant to help get you comfortable with the IntelliJ IDE and basic Java syntax that we will use throughout the course, so you should complete it using your computer. We advise you to be pragmatic about your use of any web or AI resources during this activity, as this may hide foundational or logistical issues that our course staff can quickly address to put you on a good footing for the rest of the semester. Discussion activities are intended to serve as “strength training” for programming tasks we will expect on assignments and exams (and that you will encounter in future courses and careers), and their main benefit comes from thinking critically to “puzzle” them out.

Open the dis01 Project in IntelliJ

Your TAs will demonstrate how to download a zipped project directory from the course website, extract its contents, open the project in IntelliJ, and navigate its files. Carry out these steps on your computer, and let the TAs know if you run into any issues. Confirm that:

Run all of the test cases by right clicking the “tests” directory and selecting “Run ‘All Tests’”. You should see that all of the test cases fail (you haven’t written any code yet). Now, you’ll complete the definitions of the methods in “Practice.java” to get all of the test cases to pass.

Coding Practice

For the rest of the discussion, you’ll work on six method definitions that aim to get you comfortable with some of the operations and control structures in Java. You can check the correctness of these methods by running the provided test cases. Don’t worry too much about trying to understand all of the code in the test classes; it uses many concepts that we’ll be discussing soon.

Exercise 1: Median of Three
The median of a set of numbers is the number that appears in the middle when the numbers are arranged in sorted order. In particular, for a set of three numbers \(\{a,b,c\}\), its median is \(b\) if at least one of \(\{a,c\}\) is less than or equal to \(b\) and at least one of \(\{a,c\}\) is greater than or equal to \(b\). Complete the definition of the med3() method, which returns the median of its three int parameters (a, b, and c). Use if-else statements (possibly nested) in your definition.
Exercise 2: Greatest Common Divisor
Given two positive integers \(m\) and \(n\), their greatest common divisor \(d = \textrm{gcd}(m,n)\) is the largest integer that is a divisor (i.e., factor) of both \(m\) and \(n\). For example, \(\textrm{gcd}(24,54) = 6\) since \(24 = 6 \cdot 4\) and \(54 = 6 \cdot 9\), and there is no larger integer that is a divisor of both \(24\) and \(54\). You'll use three separate techniques to compute the gcd.
(a)
First, use a for loop to do a brute force calculation in the method gcdLoop(). We know that the gcd of \(m\) and \(n\) is at least 1 (since 1 is a divisor of any positive integer) and it is at most \(m\) (since no number greater than \(m\) can be a divisor of \(m\)). Therefore, we can check every value in this range and return the largest that is a common divisor of \(m\) and \(n\).
(b)
Next, you'll code up an alternate procedure for computing the gcd called the Euclidean algorithm: Assuming \(m \leq n\), we know that if \(m\) is a divisor of \(n\) then \(\textrm{gcd}(m,n) = m\). Otherwise, \(\textrm{gcd}(m,n) = \textrm{gcd}(m,n\) % \(m)\) where % is the modulus operator. Use this reasoning to compute the gcd with a while loop in the method gcdEuclideanIterative(). Here, a while loop is a natural choice since we don't know for how many iterations this procedure will run. Rather, we know a condition when it should stop.
(c)
Finally, write a recursive method, gcdEuclideanRecursive(), that computes the gcd using the Euclidean algorithm. The solution that we have in mind is a single line of code (that incorporates Java's conditional operator).

Now that you've written multiple different versions of the same computation, we can step back and think about which one is best. Which is the easiest to understand? Which do you think will run the fastest? Which will use the smallest amount of memory in your computer? Soon, we'll see how we can formally answer these questions.
Exercise 3: FizzBuzz
FizzBuzz is an elementary/middle school game for learning divisibility that, at one point, was one of the most popular coding interview questions. In the basic version of the game, players take turns counting up numbers 1, 2, 3, .... However, rather than saying any multiple of 3, they say "Fizz". Rather than saying any multiple of 5, they say "Buzz". If a number is a multiple of both 3 and 5, they say "FizzBuzz". (In the real-world game, a player is out if they say the wrong thing, likely forgetting one of the divisors).
(a)
Write a method fizzBuzz() that takes in a parameter \(n\) and prints the outputs for turns 1 through \(n\) in a FizzBuzz game, one per line. The start of the output for \(n \geq 6\) will look like:
1
2
Fizz
4
Buzz
Fizz
(b)
Once you've done that, you can graduate to the hard version of the game. Now, a player says "Fizz" if the number is a multiple of 3, if it contains a 3 anywhere in its decimal representation (such as 13, 36, 134, etc.), or both. Similarly, they say "Buzz" if a number is a multiple of 5, contains a 5 in its decimal representation, or both (so 5, 58, and 70 are all "Buzz" numbers). A player says "FizzBuzz" if both the "Fizz" and "Buzz" properties apply (as in 15, 51, 130, 523). Write a method fizzBuzzHard() that takes in a parameter \(n\) and prints the outputs for turns 1 through \(n\) in a hard FizzBuzz game, one per line.