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
- Author Java code in the IntelliJ IDEA IDE.
- Develop methods in Java from their specification that incorporate one or more (possibly nested) control structures such as
if
-else
statements,for
loops, andwhile
loops.
Before Discussion
- Go through our setup guide to install IntelliJ and get it configured on your computer. Don’t worry if you run into issues, the course staff can assist during class.
- If you have not programmed in Java before (or if you’d like a refresher), it may be useful to look through our transitioning to Java page, which describes the Java syntax that you’ll need for this discussion and the first assignment. It may also be helpful to have that page open in a separate tab during discussion to use as a reference.
Reminder: Discussion Guidelines
The work that you complete in discussion serves as a formative assessment tool, meaning 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. Your grade in discussion is based entirely on attendance and participation; if you show up and you are actively engaged with the activity (working on the activity on your computer, discussing the activity with your group, asking and answering questions, etc.) for the entire 50-minute section period, you will earn full credit. If you complete the activity early, helping other students is a great way to further your own understanding. You do not need to submit any of the work that you complete during discussion.
Since discussion activities are not graded for correctness, we do not place any restrictions on resources that you may use to complete them, which include notes, books, unrestricted conversations with other students, internet searches, and the use of large language models or other generative AI. We advise you to be pragmatic about your use of these resources and think critically about whether they are enhancing your learning. 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 critically thinking to “puzzle” them out.
Working together in small groups is encouraged during discussion!
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.
Median
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.
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.
-
First, use a
for
-loop to do a brute force calculation in the methodgcdLoop()
. 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\). -
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 methodgcdEuclideanIterative()
. Here, awhile
-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. -
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.
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).
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
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.