Computer Laboratory 07
Deadline: Mon, Oct 27, 2025 5pm
Before you start
- This assignment is due Mon, Oct 27, 2025 5pm and will be turned in on gradescope
- The assignment is intended to be done in groups of up to 3 people, although you’re allowed to work on your own if you want.
- You can start the assignment before your lab period, although I recommend not getting too far – the lab TAs often have useful hints, and I don’t want you wasting too much time struggling with something that the TAs might announce during lab.
- For more rules see the lab rules document on canvas.
- By method count this is a long lab. This is a bit deceptive, many of the methods are about 1 line long, and 3 of them are nearly identical. Don’t let the method count intimidate you. That said, it’s also important to not lose too much time on any one problem – make sure you’re using the TA support available in-lab and asking for help when you get stuck. The TAs are around to help you with syntax translation problems you might face. (And they will do their best if you get stuck on the geometry.)
Introduction
In this lab, we will create some simple procedural art. Procedural art is a form of artistic expression in which the artist creates artwork by writing code. When run, these “art programs”, in turn, create digital art (music files, images, etc.). Our code will be focused on relatively simple visual art created by simple geometric shapes.
Since this is still a quite early Java lab – we won’t be building a novel framework for procedural art – but instead extending an existing codebase with some new geometric utilities. This will give you a lot of practice reading and using Java objects in different configurations: arrays of objects, objects in other objects, etc.
From an educational standpoint, in this lab you will:
- Practice reading code written by others and thinking about how to use it.
- Practice creating and using Java objects, calling methods, and thinking about how objects relate to each other.
- Get experience with two common situations that tend to cause trouble: arrays of objects and objects stored inside other objects.
- Build your expertise with Java syntax and object-oriented programming.
- Get practice and feedback on your Java code style (this will be graded!)
- See one method developed in three different contexts (instance method, static method on same class using private variables, static method on different object.)
- Allow you to express yourself creatively in Java!
While this lab may not be as computationally difficult as some earlier labs in this class, it focuses on important concepts and syntax. As you work on this lab, therefore, don’t just “edit until it works.” Instead, focus on working deliberately – make sure you understand the syntax and concepts behind everything you do. If you do find yourself getting stuck, you are of course free to get help from course staff or your lab partner. More importantly however: after you do resolve the issue, make sure to come back after debugging and review what the correct syntax for a given task is!
On the other hand – if you do find this to be an easier lab than most:
- pat yourself on the back – you’ve really got the nuances of object-oriented syntax down,
- take this moment to focus on developing your code style – think actively about how to make your code maximally readable! and
- Take this as a moment to REALLY show off – the last part of this assignment is very open-ended to give everyone a chance to explore Java and create something fun and interesting!
(With permission) we intend to post our favorite results to the class as a celebration of our creativity!
Software environment setup
This lab will be done using a Java programming environment. We recommend the IntelliJ IDE running on your own personal computer. See lab 6 for instructions about how to set this up.
A few reminders:
- Source code goes in the
srcfolder in Java. Java is VERY particular about where files go, and what they are called. - Lab06 has syntax guides if you still find translating to Java difficult.
- Do not have any folders inside src – these are called packages and will make your code fail the autograder.
- Download every provided file and move them all to the src folder before starting.
- Code style elements like comments and Javadocs are important and easier to do as you go. Don’t write hard-to-read code and think “I can document this later.”
Files
This lab will involve the following provided files. You will need to read these files, but should not need to change them:
Point.Java– A simple Java class representing a 2d point.Ring.Java– A simple Java class representing a ring.ShapeDrawer.Java– A more complicated Java class providing a toolkit for creating 2d drawingsShapeUtilsTester.Java– A Java program that tests the ShapeUtils class you will have to write.
This lab also involves the following files you will edit or create:
Circle.Java– A simple Java class representing a circle.ShapeUtils.Java– A Java class with many static functions that perform various geometric computations on Java objectsDrawSolar.Java– A Java program that creates an image fitting a few key requirements.solar.png– A copy of the type of image your code can generate.
Instructions
Before beginning you should:
- Setup an IntelliJ project.
- Download the provided files and place them in the src folder.
- Create empty versions of the two required Java files that are not provided, and ensure that your names are in a comment at the top of the file (as this is required for code-style).
- Update the comment at the top of the
Circleclass to include your name. - Comment out the code in the main method ShapeUtilsTester until it shows no errors – you will need to progressively uncomment it as you implement more methods (but Java will not run AT ALL if there are ANY syntax errors.)
- Skim the formal requirements sections and the Java syntax sections. (You’ll want to look over everything before you get started)
- Read, and review the provided classes – you will be expected to use these files, and the provided source code is your guide for doing that. You may find it useful to take quick notes so you know what methods are available, and roughly what they are called. Remember – your goal isn’t to read the CODE, your goal is to read method names and Javadocs.
- Only then should you think about doing any novel programming.
All of these steps before coding can reasonably be discussed with others (even those outside your group), but please solve the actual programming-parts of this problem collaborating only with your lab partner.
Formal Requirement
Code reading
The first required task here is to read the provided Java classes and make sure you know how to use them. The steps for this, as presented in class, are as follows:
- Open the file.
- (If your editor supports it) “fold” all the code – you should not need to read the code (sometimes that can even make understanding how to use a class harder!)
- Read only the function names/parameters/return types and Java docs.
- Make a few informed guesses if things are not fully clear, and don’t be afraid to ask for more info if you need to.
- Write some testing code for yourself to make sure you understand anything that isn’t clear enough. Don’t forget the **power* of playing around with the code to see what works.
Be aware that code-reading is an IMPORTANT SKILL, and one that many consider harder than code writing! Even though this requirement has no formal “hand-in” – give it your best effort and practice just like the code-writing requirements! Many of the most common questions on this lab are directly answered in the Javadocs of provided classes.
NOTE: It is reasonable to go over this reading task with others in, or out-of your group. As you do so, however, make sure you’re not just having someone else read the code to you. Discuss questions and compare answers, don’t trust someone else to read code for you.
ShapeUtils
The ShapeUtils class has five required functions. Many of these can be done in only a few lines with the right syntax. You can do these in any order – and you may not find that the provided order here is the “easiest” order to take things.
public static double distance(Point p1, Point p2)public static Point getCenter(Point[] points)public static double getArea(Ring r)public static double getArea(Circle c)(this will actually be documented in the next section… but it’s listed here for completeness)public static boolean isIn(Circle c, Point p)
distance
The distance method takes two points and computes the distance between them: \(distance = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\)
getCenter(Point array)
The getCenter function should take an array of points and return a point that represents the “center” of those points. This can be computed by taking the average x value of each point in the array, and the average y value of each point in the array, and making a new point from those two averages. If an empty array is given the point \((0,0)\) should be returned.
getArea(Circle)
The getArea method takes a circle and returns it’s area. The area of a circle can be computed by the following equation: \(A = \pi r^2\)
(Note this function will also be listed in the next section…)
getArea(Ring)
The getArea function takes a Ring and return its area. The area of a ring can be computed by the following equation: \(A = \pi r_1^2\ - \pi r_2^2\)
Hints:
- We don’t store two radiuses, we store the inner circle object and the thickness of the ring. The real challenge is “how can you compute the two radiuses from this?”
- You may find it useful to visualize a ring – you can use the ShapeDrawer to do this, but you can also think “doughnut”. It’s a circular shape with an empty center.
isIn(Circle, Point)
This function takes a circle and checks if a point is in the circle. Compared to the triangle case, this is a lot easier, simply compute the distance from the center of the circle to the target point. If that distance is less-than or equal to the radius of the circle, the point is in the circle.
Area of a Circle
This section is intended to give you exposure to the differences in how you write code in three different contexts by having you implement the same behavior (area of a circle) in three different functions.
- Instance method (Circle class method
public double getArea()) - Same-class function (Circle class function
public static double getArea(Circle c) - Other-class function (ShapeUtils function)
public static double getArea(Circle c)
getter method
On the Circle class fill-in the empty function public double getArea() (You should also write a Javadoc for this)
The area of a circle can be computed from only it’s radius: \(A = \pi r^2\)
For \(\pi\) please use Math.PI – which is a maximum precision approximation to the value of \(\pi\).
Note – in this method you will have direct access to the private radius variable. You will have direct access to radius because this is not a static method – therefore this will always have to be run on some Circle, and that Circle’s radius variable can be used directly.
An example for clarity:
double centerX = center.getX();
same-class static method
On the circle class fill-in the empty function public static double getArea(Circle c)
The area equation is, of course, the same here. The only difference is how you access the radius variable. This method runs in a static context, this means that, unlike the previous function, it isn’t called like someCircle.getArea() it would have to be called on the class itself Circle.getArea(someCircle). This also means that you only have direct access to static properties and cannot just write radius (since there isn’t a clear definition of what “radius” means in this case).
To update your code for this context you will need to modify it to use the c circle variable to get the radius value. Note – since this is still in the Circle class you can still access the private variables. Make sure you understand why you need to make these changes when moving from an instance method to a static one.
(An example for clarity)
double centerX = c.center.getX();
Other class static method
For our final function add a method public static double getArea(Circle c) to the ShapeUtils class. Like the last function this runs in a static context and must be given a triangle to operate on. Unlike the last function this is on a different class, so it can only use public properties. To access the points you will need to use the public getter methods (getCenter(), getRadius()) on the circle class.
Again – make sure you understand why you need to make these changes when the function is in a different class.
An example for clarity:
double centerX = c.getCenter().getX();
Draw Solar
The DrawSolar class should have one method: public static void main(String[] args).
This program should use the ShapeDrawer, as well as the Point, Ring and Circle classes to draw a simple Solar System figure. This should be similar to the image above, but it does not need to be identical. The image file needs to be saved to the file “solar.png”. If you name the file something else (or forget to tell the shapeDrawer to draw it) the autograder will not accept it.
Your solar system picture must:
Must have a Circle sun
Must have an Circle earth
The sun must locate at the center of the canvas, and should be in a reasonable “sun” color (yellow-to-red typically)
The earth must be another color, with colors in the blue-to-green space being most reasonable.
Earth must move along with a Ring orbit that sun is its center (I.E. you need to draw a ring for the orbit and place the earth upon this ring).
The image itself should be at least 200 pixels wide by 200 pixels tall. You should seek to make a larger picture than this, but 200-by-200 is the minimum we’ll allow
You will need to change colors from the default draw colors (you won’t need to use our colors, but you will need to use more than just the defaults)
- To do this you will need to use the
Java.awt.colorclass, feel free to search that to see how to use it. - The built-in Java color class defines a few common/simple colors, but also allows you to create new colors using the RGB color space.
- Intellij has a tool built-in to help you pick numbers representing colors visually for RGB, as does google (if you google search “rgb color picker” you can find a tool to help find colors)
- To do this you will need to use the
Beyond these requirements you are free to do what you wish. Make sure this class runs correctly and submit your solar.png to demonstrate that you’ve run the code correctly.
If you find yourself finishing early, you are encouraged to explore a bit – a few ideas:
- Add more planets!
- Add moons!
- Make the locations compute randomly
- What about asteroids, stars, or space-ships?
- The earth’s orbit is actually Ellipse, can you modify the orbit from circle to Ellipse?
- Using single color on planet is boring – give that planet some texture!
Note – we may give extra credit for drawings that we find particularly impressive either artistically (it looks very good), or technically(there are some complicated bits of code going into your output such as randomized functions and/or recursive drawing functions).
Java Syntax Guide
Most of the required Java syntax has been in readings and/or in lecture before now, but there are a few minor details I wanted to include as a reminder.
Classes like
Java.lang.MathandJava.util.Randomare available for you to use. If you can’t remember what functions are available, try looking up the official Java documentation.- https://docs.oracle.com/Javase/8/docs/api/Java/lang/Math.html
- https://docs.oracle.com/Javase/8/docs/api/Java/util/Random.html (remember – you will need to import the Random class)
We will be requiring Javadocs (written by you) in this assignment. As a reminder these use a two-star comment placed before the function:
/**
* compute the sum of numbers between a minimum (inclusive)
* and maximum value (inclusive) value.
*/
public static int sumOfNums(int min, int max) {...}
Remember – documentation like this does not need to be super long, but it should explain what the function is, what it does, and if unclear – what it’s parameters and return type mean.
- Static, or class, methods have the static modifier – these can be called without making objects. If a function or variable does not have the static method, however, it is an instance-property. This means that any function or variable not listed with “static” – is designed to be used with objects of a given type and cannot be used directly.
- As an example, the Point method
getX()does not have the static modifier, so you can’t usePoint.getX()to call the function, instead you would need to reference a point object directly (perhaps using a variable) such as:pointVar.getX()– as you need to run getX on one specific object. - Static methods can be called directly from a class. As an example, the distance function you will write in ShapeUtils can be called from any other class like:
ShapeUtils.distance(point1, point2)Within the ShapeUtils class, however, you can call distance directly:distance(point1, point2). - Constructors – you can tell that a function is a constructor if it’s name matches the class name (and it lists no return type) There’s a special syntax for calling constructors. Example:
public class Foo {
public Foo(int x) {...} // this is a constructor
public static void (String[] args) {
// and here we can see it being used-- note the syntax:
// new <classname>(<parameters>)
Foo variable = new Foo(7);
}
}
More information on any and all of these topics can be found in the zybooks textbook, or by asking for assistance from your lab TA.
Remember – the purpose of this lab is to get comfortable with this object-oriented syntax – it’s OK if you spend some time wrapping your head around this syntax and it’s rules. Just make sure you know these rules by the deadline and you’re fine!
Java Autograder Notes
The Java autograder is much fussier than the python autograder. This is mostly due to Java’s nature as a compiled language. While we could often partially test python files that had errors in some functions – for your code to be testable your code must compile, have all the correct function definitions, and all the correct names. Even once you get the tests running you should also be aware that the autograder for Java does not give as clear feedback as the python autograder when things go wrong. As such you are expected to be testing you code on your own. Do not use the autograder as a debugging tool.
Due to some strange things in the autograder, we have a few specific requirements for your code:
- The code you submit must be valid and compileable Java code. (We cannot test code with syntax errors. If we can’t test it, we can’t give you autograder credit for it)
- The code you submit must have no package statement in it. (We have to do some weird things with package structures to get the code to compile correctly.) YOU WILL GET 0 points if your code has a package statement! Your IDE may have put one in without you noticing so you should check for this. If you see code like
package com.whatever;at the top of your file you need to delete it. - The code you submit must match the provided function signature (name, parameter types, and modifiers) EXACTLY. Any mis-match, no matter how minor, may cause the test code to not compile, which would prevent testing.
As a general rule, if you started with the template file on canvas and didn’t modify anything other than the function bodies you should be fine this time. Likewise, if your code can be run with the provided testing code (with no modifications to this testing code) you should be fine. If you do run into testing issues, do not hesitate to reach out to course staff to help with debugging.
Submission
For this lab you need to turn in:
ShapeUtils.JavaCircle.JavaDrawSolar.Javasolar.png
Grading on this assignment will follow the given general rubric:
50 points autograder – This is split somewhat chaotically across the methods.
10 points code style:
- Your name (and the name of any collaborators) should be in a comment at the top of the file
- Correctly formatted JavaDoc (this is the
/** ... */comment blocks placed before each function that describes the function - Good variable names
- Consistent tabbing (while Java does not require this the same way python did – we do require it. Reading and grading poorly tabbed code is a painful experience)
- Outside of these explicit requirements, as normal, we will generally be looking for (and giving feedback on) anything that makes the code harder/more painful to read, and will take away points if it’s particularly bad.
20 points for the DrawSolar and solar.png
20 points for the following functions:
- 6 The three versions of the Circle getArea functions
- 2 points distance(Point)
- 6 getCenter(Point array)
- 4 getArea(Ring)
- 2 isIn(Circle)