1
CS 170: Introduction to Computer Science I – Summer 2020
Homework Assignment #1
Submission instructions
Submit your assignment as a zip file and save each program as .java file. Name the program based on problem
numbers. If you submit with a teammate, select team size 2. No email submissions are accepted. No late submissions
are accepted.
General instructions and hints
In those problems asking you to write a method, always call the method several times to test that it works properly
with a variety of different values for the parameters. Test it on more examples than the ones shown in this handout.
Even if the problem asks you for just one method, you can always write additional helper methods to simplify and
organize your code. Make sure you write comments to explain your code.
Comment requirements: Always comment the top of each method (what the method does, the meaning of the
input parameters, the meaning of the output value). Write comments within the methods to explain the
strategy you are using to solve the problem, and to clarify blocks of code that may be difficult to understand.
2
Problem 1: Medieval town (20 points).
Write a Java program that draws the figure below using turtle graphics. Your program will not draw the grid, which
is shown here only to help you measure the size of the figure (each grid square is 10 turtle steps long). Make sure
your code is modular: organize the different components to be drawn in appropriate methods, then call those
methods to compose the entire drawing. Your code should use iteration (loops) wherever appropriate (i.e., minimize
copy and paste). Make sure that your methods have meaningful names, and you include appropriate comments in
your code. Feel free to use turtle’s coloring commands to make your drawing even more interesting. Hint for the
arcs: Math.PI contains the constant number PI (3.1415…). The built-in class Math contains many useful
trigonometry functions:
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
You can approximate a circle with a polygon with many sides. This website describes the relationship between the
radius of a polygon and the length of its side:
http://www.mathopenref.com/polygonradius.html
programs that do not compile get zero points.
+3 for each object correctly drawn in the correct position (towers, walls, arches, trees, mountains, stars)
(only 2 points if the object has imperfections, 0 if it is not a good likeness for the object).
-4 if the code does not use for loops where appropriate
-4 if the code is not modularized using methods
-2 if the methods don’t have meaningful names
-2 if there are no comments, insufficient comments, or bad usage of comments
3
Problem 2: Polyspiral (4 points)
Write the method polyspiral(Turtle t, int n, double base, int rounds) that makes the turtle draw a spiral like those
shown in the figure. The parameter n specifies the polygonal shape of the spiral (3 is triangular, 4 is quadrilateral,
etc.). The parameter base indicates the size of the smallest side of the spiral. The parameter rounds specifies the
number of revolutions of the spiral. See examples below.
polyspiral(t, 3, 20, 5) polyspiral(t, 5, 10, 4) polyspiral(t, 8, 5, 3)
programs that do not compile get zero points.
partial credit for partially correct running solutions (only 2 points if there is a logical error, 0 for more than
one logical error)
-1 if the method name and arguments do not match what is stated in the problem description.
-1 if there are no test cases
-1 if there are no comments, insufficient comments, or bad usage of comments
-1 if the turtle does not go back to the original position
Problem 3: Polywheel (4 points)
Write the method polywheel(Turtle t, int numSides, double length) that makes the turtle draw a polygonal wheel
like those shown in the figure. The parameter numSides specifies the number of sides of the base polygon. The
parameter length indicates the size of the side of each polygon. See examples below.
polywheel(t,3,70) polywheel(t,4,50) polywheel(t,5,40) polywheel(t,6,30) polywheel(t,12,15)
4
Problem 4: Multistar (4 points)
Write the method multistar(Turtle t, int n, double length) that makes the turtle draw a star like those shown in the
figure. The parameter n specifies the number of rays. The parameter length indicates the size of the long rays (the
short rays are 1/4 the length of the long rays). See examples below.
multistar(t, 7, 100) multistar(t, 10, 70) multistar(t, 25, 120)
Problem 5: Pyramid (4 points)
Write the method pyramid(Turtle t, double base, int levels) that makes the turtle draw a pyramid like those shown
in the figure. The parameter base specifies the total length of the base of the pyramid. The parameter levels indicates
the number of layers of blocks that build up the pyramid. See examples below.
pyramid(t, 200, 5) pyramid(t, 200, 10) pyramid(t, 300, 10)
5
Problem 6: Houseline (4 points)
Write the method houseline(Turtle t, int numHouses) that makes the turtle draw a line of houses where the second
house is half as big as the first one, the third one is 1/3 of the first one, the fourth one is 1/4 of the first one, and so
on. The parameter numHouses is the total number of houses in your line. See example below. Hint: you can use the
house code from the course website, adding a parameter to scale the size of a house by an arbitrary factor.
houseline(t, 20)
Bonus points: Early submission
If you submit the entire homework no later than 24 hours before the deadline, and the total score on the rest of this
homework assignment is at least 20 points, you will receive 2 bonus points. The bonus points will be added to the
total score of this homework assignment.
Good luck and have fun!
The post CS 170: Introduction to Computer Science appeared first on My Assignment Online.