This Target problem is pretty fun, and is great for understanding the basics of Java that were covered in the last few lectures. Here is the problem:

Here is my solution, which you can also find on github.

The Target consists of three circles of different radiuses. The hardest part of this problem is figuring out how to get the circles to display in the center of the window. Keep in mind that when you’re drawing a circle, the circle is drawn inside a square, where the starting coordinates are in the top left corner, so you have to move over your starting point by half a radius to center the circle.

<pre>/* * File: Target.java * Name: * Section Leader: * ----------------- * This file is the starter file for the Target problem. */ import acm.graphics.*; import acm.program.*; import java.awt.*; public class Target extends GraphicsProgram { public void run() { putOuterCircle(); putMiddleCircle(); putInnerCircle(); } private void putOuterCircle() { int radius = 72; int x = getWidth()/2 - radius/2; int y = getHeight()/2 - radius/2; GOval OuterCircle = new GOval (x, y, radius, radius); OuterCircle.setColor(Color.RED); OuterCircle.setFilled(true); OuterCircle.setFillColor(Color.RED); add(OuterCircle); } private void putMiddleCircle() { double radius = 72*64/100; double x = getWidth()/2 - radius/2; double y = getHeight()/2 - radius/2; GOval MiddleCircle = new GOval (x, y, radius, radius); MiddleCircle.setColor(Color.WHITE); MiddleCircle.setFilled(true); MiddleCircle.setFillColor(Color.WHITE); add(MiddleCircle); } private void putInnerCircle() { double radius = 72*3/10; double x = getWidth()/2 - radius/2; double y = getHeight()/2 - radius/2; GOval InnerCircle = new GOval (x, y, radius, radius); InnerCircle.setColor(Color.RED); InnerCircle.setFilled(true); InnerCircle.setFillColor(Color.RED); add(InnerCircle); } }</pre>

Hi, I have a question. The circle has a radius of 10, why do you divide it by 2? I thought you suppose to take the radius and time by 2 to get the diameter. This is what I did, I take the width minus the diameter and divide by 2 but this isn’t correct. I know your logic is correct, however, I don’t get why you divide the radius by two. The radius is only half of the circle. Thanks

@MaxFusion,

You are correct! I realized this mistake later on in the course, and didn’t go back to correct it. I forgot that you’re supposed to multiply the radius by 2 to get the full width, so I treated the radius as my diameter. Great job for noticing π

I’m taking the class on itunes U. It’s good to know Stanford is giving this away for free. π here’s my answer to this assignment.

Thanks for posting! Your solution is a lot better than mine is π

thanks for sharing this… this is very cool, great to see.. I thought about doing it like this, but didn’t know how.. saw this, and BAM i can see how… great use of method

While discussion in CS is important, posting solutions to assignments on this blog is in clear violation of Stanford’s code of ethics. Public solutions to CS106A assignments facilitate academic dishonesty by providing students who are taking this course with unfair aid. Please remove these solutions so that no student obtains an unfair advantage.

For those students who want to cheat, they’ll find answers regardless of whether I keep this blog or not. In addition, my solutions are not necessarily “the correct” ones, so if students do decide to copy them, they probably won’t get the best grade either way.

My goal with posting these solutions is to help out all the people who are taking this course online. I personally found it very useful to refer to other people’s code to come up with my own answers (since I didn’t have a TA to help me out).

I agree.. thank you for the blog. I am doing the course online and do not have much help, so having this blog as a reference is great.

I did something very simular to you.

Except having setFillColor and setColor is redundant if they’re the same color.

Because you want the oval to be a pure color with the same color for the outer edge so you can simply put setColor.

I also made a private static fill int called “size” and then plugged that into my methods instead of “72” that way you could change the “size” and effect the entire image.

Thanks for putting up your code it was a great verification that i was on the right track!

Love it when someone writes better code than I did!

Hiya

Here\βs my solution;

I doubled the sizes cos it looked small in my screen.

It had a putCircle(); etc at the beginning, but I removed it, even if it is correct top down decomposition. I guess I was looking to do something like Mark Javier has done but he actually knows what he\βs doing.

I have a couple of questions;

1. I tried to do the pyramid problem way too early, and fled to your blog in despair, which made me realize I hadn\βt progressed far enough through the book and lectures to tackle it. Now that I\βve progressed further through the course, I\βd like to try a similar problem, but not that one because I\βve looked at too many other solutions to it. Are there any problems which have a similar difficulty for that level that you may have come across elsewhere? The class guide on github you link to, btw, is a big help in how to pace the course!

2. When I was looking at other people\βs solutions to the pyramid problem, there was a lot of funky stuff going on with method calling- all sorts of weird and wonderful stuff in the brackets after the name. That seems way ahead of where I\βm at- chapter 4 in the book and lecture 7 on video. I\βm assuming that stuff is coming up, I haven\βt missed something thats implied by whats been taught so far? Your solution to the pyramid thing is not only elegant but actually understandable at the level I\βm atβ¦

Thanks alot! This is a great resource for people studying on their own, well done!

Robbie

I don’t know what I am doing wrong… The first one that I did was the pyramid and I have it working. I recently switched computers, and my new one is 64-bit windows 7. Every time I quick-run a program, my eclipse crashes. Every time I run my program, (unfinished) I get the error

“EventDispatchThread.run() line: not available [local variables unavailable]

Source not found”.

I’ll post my unfinished program anyway.