Since lab exams start the week after reading week (labs from the 22nd–28th), I wanted to provide some practice homework. Please attempt these questions before looking at the solutions. The solutions may include in-file documentation that explains the reasoning behind them.
Homework 1 (functions, for loops)
Empty Java File Link (Contains a semi-setup environment for you)
Create functions to draw various polygons — Equilateral Triangle, Square, and Circle — at a position (x, y) with a given size. Include an option to fill the shape (hint: use a boolean parameter). Your square function should be easily adaptable for drawing rectangles. Please assume that (x, y) represents the center of the shape.
Next, make a function to draw a line from (x1, y1) to (x2, y2). This is easier than you might think!
Idea and hints
The goal of this exercise is to create a set of functions that operate independently of the turtle’s current position. If you don’t do that, it’s fine—but turtle graphics involves a lot of “hidden state.” What does that mean?
If you call yertle.forward(), the turtle moves from its current position and angle. If you don’t know what those values are, the result is essentially random, right?
As you’ll learn, managing state is a key part of programming. So it’s a good idea to start practicing how to minimize unnecessary state. By assuming nothing about the current state, we can decouple each function from the rest of the program.
Consider using:
yertle.moveTo(x, y)
Validate your output
Example outputs
Triangle:
Drawing at 50, 50 of size 100 non-filled plus drawing at -50, -50 with size 100 should create a similar image. If your triangles are inverted, that’s fine.

Square
Drawing at the same position with the same size should look similar to this:

Note: It should be trivial to turn these into rectangles.

Circle
Circles are trivial because the turtle provides the function for you.

Lines

It’s just a line between (-100, -100) and (100,100).
Homework 2 (compound functions, for loops)
For this exercise, take the square function from the previous homework (or use mine). Then, create a function that draws alternating colors of squares. You can choose any colors you like — even multiple ones! Your function can draw either along the x-axis or the y-axis.
Finally, make a function that draws stacked rows of alternating colors. The result should look like a checkerboard pattern.
Example output

Homework 3
For this homework, we’ll be combining two images into one. How you choose to do that is up to you! I recommend picking one of the algorithms from the table below — the solution will include examples for each of them.
Since I don’t want to accidentally use copyrighted images, I’ll stick to ones I know are safe — mostly photos I’ve taken myself. Conveniently, that means I get to plug my photography a little… and more importantly, my dog!
List of pictures I will be using.
Feel free to use your own pictures!
These pictures are also on the github for this homework.
List of potential combination methods
The size of the strips, checkerboard cells, etc., should be user-controllable.
The list below is roughly ordered by difficulty:
- Completely Random
- Stripes (Either vertical or horizontal)
- Perlin Noise (Ordered Random)
- Checkerboard
- Rings
- Spiral
Sample Outputs
Random Mixing (Using custom noise function)

Vertical Lines

Horizontal Lines

Checkerboard

Diamond Board

Rings

Spiral

Sin + Cos Blending

Tangent based function

Hidden Pattern

Rings2

Solutions
The solution I’ve written for this exercise uses one of Java’s more complex features. I chose this approach because I wanted to experiment with different functions without duplicating a bunch of code.
Once you start learning more advanced programming concepts — and, more importantly, truly understand them — you’ll see how powerful they can be. The features I’ve used here combine aspects of functional programming with Java’s lambda expressions. Working on this reminded me how enjoyable these tools are to use; as you’ll see in the solution, they make the code elegant and easy to extend. (And honestly, this isn’t even the best way to do it — just the quick version I put together before bed.)
Java Lambda explanation
Since this is a complicated topic that falls well outside the scope of anything you will do in your first and second years, let us break the problem down into sections.
I implore you to look at a piece of code, even if you don’t understand it, and ask yourself:
- Does any of this resemble code I’m familiar with?
- What patterns or structures do I recognize?
- e.g., loops, function calls, conditionals
- Can I identify inputs and outputs?
- You can see in the picture display what the code outputs
- Where is data being transformed or reused?
- Are variables being updated, passed around, or returned?
- What patterns or structures do I recognize?
Once you’ve asked those questions about a piece of unfamiliar code, can you infer what it is doing?
Starting at the beginning.
Let’s start with a basic function that uses features you’re probably already familiar with mixRandom().
This function uses Math.random() to select between the two images.

But wait — what’s that weird-looking syntax?
Those are called lambdas, also known as anonymous functions. They let you define a function directly inside an expression. You don’t have to give it a name or associate it with a class (hence the “anonymous” part).
You’ll also notice that you aren’t required to define the type of the variable. This is one of the reasons lambdas are so convenient.
Of course, you can explicitly define the type as usual if you want.

Next, you may have noticed that there are no parentheses. For single-line expressions that return a single value, they aren’t required. You can include them if you like — but then you’ll also need to add a return statement.

All of this is equivalent to using a normal function and passing it in as a parameter.

That approach, however, introduces a bit more syntax — in this case, what’s known as a method reference. Method references are also quite useful, but let’s stay on topic for now. Hopefully, you’re starting to see that lambdas aren’t scary; in fact, they make a lot of sense!
Now let’s look at the function definition itself.

Whoa — what’s going on here?
The while loop probably looks familiar, but what about that parameter? That’s a BiFunction, something provided by the Java standard library. It allows us to say:
We want a function that takes two parameters and returns a value.
We then specify the types of those parameters and the return value. Inside the angle brackets (< and >), we tell Java exactly what those types are — ordered from left to right. In this case, we want a function that takes two int values and returns a double. This kind of declaration lets us define what’s called a generic interface, meaning:
We don’t care what the function does, only that it meets these requirements.
Hopefully, that makes sense — but if not, let’s peel back another layer.
How Is BiFunction Implemented?

Surprisingly, it’s quite simple! It doesn’t even define what the function does.
That’s because it’s an interface — essentially a contract (or API) between you, the programmer, and the Java runtime. Interfaces allow you to make behavior generic and reusable. If you’re feeling a bit lost, I highly recommend reading more about Java’s object model — it’ll make this concept much clearer. Maybe I’ll make a separate post on it later.
Hardwork 4
This one is going to be a bit special since it will require using knowledge outside the scope of the class. This topic was far too interesting for me to pass up, so I hope you learn something!
Feel free to skip this one.
For this exercise, I want you to hide information inside an image. For a start, we could hide a string of text inside the image. You may be thinking, how?? Well, the answer is surprisingly simple.
A semi-technical description
To hide a string inside an image, we must define some kind of encoding. Assume we have a string of information of length n. If we assume this to be a normal Java characmake the change as necessary.
ter, we know it takes 2 bytes to store this.
RGB is a 24-bit standard; each color channel receives 8 bits, equivalent to 1 byte. You might see what I am getting at. But wait, if we replace the R and G color channels with our information, we could, in theory, store it; however, wouldn’t that result in a completely different appearance from the original image? Yes! Yes, it would! The solution to this is to spread out bytes over a series of pixels.
If this sounds really complicated and outside the scope of this class, yes, it is. That’s why I called it hard work. You can complete but it may require some individual research.
The method I have described is a rather basic way of hiding information. If you think this field is neat, consider looking into steganography.
Challenge 1
Create a program that can draw an arbitrary string. Since font loading is well outside the scope of this course, make dedicated functions for drawing each character of the English alphabet. You can use either uppercase, lowercase, or both.
As we haven’t covered all the required topics yet, I’ve provided an example program file with everything you’ll need.
There’s no solution provided for this challenge — you’ll know if it works when it does!
Most letters can be constructed from combinations of straight lines and circles. It may be easier to start with capital letters. The letters don’t have to be precise — focus on structure and consistency.
Challenge 2
Given a set of distinct integer points P, draw the convex hull of P
(Explanation, algorithm, examples, and solution coming soon)
Leave a Reply