Stanford CS106a Assignment 2, Problem 4 – Target – Part 1

The problem is as follows:
Create a figure that is simply three GOval objects, two red and one white, drawn in the correct
order. The outer circle should have a radius of one inch (72 pixels), the white circle
has a radius of 0.65 inches, and the inner red circle has a radius of 0.3 inches. The
figure should be centered in the window of a GraphicsProgram subclass.

Here is my first part of the solution to the problem. The output is correct, however, the guide states I should use constants. I will re-write the program later in Part 2 once I have learned how to use constants. But for now here is the solution to the problem and the output below.


/*
* File: Target.java
* Name: Jason Coltrin
* Section Leader: n/a
* -----------------
* This file is the starter file for the Target problem.
* This figure is simply three GOval objects, two red and one white, drawn in the correct
* order. The outer circle should have a radius of one inch (72 pixels), the white circle
* has a radius of 0.65 inches, and the inner red circle has a radius of 0.3 inches. The
* figure should be centered in the window of a GraphicsProgram subclass.
*/

import acm.graphics.*;
import acm.program.*;

import java.awt.*;

public class Target extends GraphicsProgram {
public void run() {
drawLargeRedOval();
drawWhiteOval();
drawSmallRedOval();
}

// precondition: a canvas of 200pixels across and 200pixels top to bottom,
// the center is 100x100 pixels.
// postcondition is a red filled circle with 1 inch radius with 72pixels has
// a center of 36
// 100 - 36 = 64,64 is where the left and bottom of the rectangle in which
// the oval is located should be placed

public void drawLargeRedOval() {
GOval myOval = new GOval(64, 64, 72, 72);
myOval.setFilled(true);
myOval.setColor(Color.RED);
myOval.setFillColor(Color.RED);
add(myOval);

}

// precondition: a 200x200pxl canvas with a 72pxl red circle centered at
// 100x100
// postcondition: another white circle added on top of the red circle. .65
// of 72 = 46.8 - half of 46.8 = 23.4
// 100 - 23.4 = 76.6, is where the left and bottom of the GOval rectangle is
// set.
public void drawWhiteOval() {
GOval myOval = new GOval(76.6, 76.6, 46.8, 46.8);
myOval.setFilled(true);
myOval.setColor(Color.WHITE);
myOval.setFillColor(Color.WHITE);
add(myOval);
}

// precondition: a 200x200pxl canvas with a 46.8 white circle on top of a
// red 72pxl circle with a center of 100x100
// post condition: another red circle added on top of the white circle. .3
// of 72 = 21.6 - half of 21.6 = 10.8
// 100 - 10.8 = 89.2, is where the left and bottom of the small re oval is
// set.
public void drawSmallRedOval() {
GOval myOval = new GOval(89.2, 89.2, 21.6, 21.6);
myOval.setFilled(true);
myOval.setColor(Color.RED);
myOval.setFillColor(Color.RED);
add(myOval);
}
}

Target

Condition that I will work on in part 2:
Do you make appropriate use of constants? Several of the programs you’ll write – especially
the graphics programs – will require values that will not be immediately evident from context.
When appropriate, introduce constants into your program to make the program more
customizable and easier to read.

Here is the code that contains the constants. So for example, the boss wants to make the logo bigger, replacing the value of the constants is easier:


import acm.graphics.*;
import acm.program.*;

import java.awt.*;

public class Target extends GraphicsProgram {

final static double firstOvalxUpLeft = 64;
final static double firstOvalyUpLeft = 64;

final static double firstOvalHeight = 72;
final static double firstOvalWidth = 72;

final static double secondOvalxUpLeft = 76.6;
final static double secondOvalyUpLeft = 76.6;

final static double secondOvalHeight = 46.8;
final static double secondOvalWidth = 46.8;

final static double thirdOvalxUpLeft = 89.2;
final static double thirdOvalyUpLeft = 89.2;

final static double thirdOvalHeight = 21.6;
final static double thirdOvalWidth = 21.6;

public void run() {
drawLargeRedOval();
drawWhiteOval();
drawSmallRedOval();
}

// precondition: a canvas of 200pixels across and 200pixels top to bottom,
// the center is 100x100 pixels.
// postcondition is a red filled circle with 1 inch radius with 72pixels has
// a center of 36
// 100 - 36 = 64,64 is where the left and bottom of the rectangle in which
// the oval is located should be placed

public void drawLargeRedOval() {
GOval myOval = new GOval(firstOvalxUpLeft, firstOvalyUpLeft,
firstOvalHeight, firstOvalWidth);
myOval.setFilled(true);
myOval.setColor(Color.RED);
myOval.setFillColor(Color.RED);
add(myOval);

}

// precondition: a 200x200pxl canvas with a 72pxl red circle centered at
// 100x100
// postcondition: another white circle added on top of the red circle. .65
// of 72 = 46.8 - half of 46.8 = 23.4
// 100 - 23.4 = 76.6, is where the left and bottom of the GOval rectangle is
// set.
public void drawWhiteOval() {
GOval myOval = new GOval(secondOvalxUpLeft, secondOvalyUpLeft,
secondOvalHeight, secondOvalWidth);
myOval.setFilled(true);
myOval.setColor(Color.WHITE);
myOval.setFillColor(Color.WHITE);
add(myOval);
}

// precondition: a 200x200pxl canvas with a 46.8 white circle on top of a
// red 72pxl circle with a center of 100x100
// post condition: another red circle added on top of the white circle. .3
// of 72 = 21.6 - half of 21.6 = 10.8
// 100 - 10.8 = 89.2, is where the left and bottom of the small re oval is
// set.
public void drawSmallRedOval() {
GOval myOval = new GOval(thirdOvalxUpLeft, thirdOvalyUpLeft,
thirdOvalHeight, thirdOvalWidth);
myOval.setFilled(true);
myOval.setColor(Color.RED);
myOval.setFillColor(Color.RED);
add(myOval);
}
}

Eclipse IDE Tips and Tricks

I wish I had know about these a couple weeks ago, but anyway here are a few tips and tricks I found for using the Eclipse IDE with Java:

// Eclipse tips and tricks
1. Quickly add main method by typing “main” followed by ctrl+space and then enter
2. Quickly format your source code by typing ctrl+shift+f or right click->source->Format
3. Type sysout then ctrl+space, then enter, to generate System.out.println();
4. F11 runs your application in debug mode – if no breakpoints, it will run normally
5. Hit ctrl+shift+o for organize imports. This will give you list of possible imports and then
add or the import to/from the top of the source file.
6. Type syserr and then ctrl+space to quickly type out System.err.println();.
7. to rename a variable, select the variable, right click, choose refactor -> rename, and all
of the instances where that variable name has been used will be changed.
8. You can rename a class as well by right-clicking on the .java file in the Package Explorer
pane, then -> refactor -> Rename.
9. When you highlight a line or block of code and push Ctrl + Alt together,
and the down arrow, it clones the line or block. (You may have to disable your intel
graphics card drivers or your screen will flip.)
10. Use Ctrl+Shift+/ to comment blocks of code and Ctrl + Shift + to uncomment them again.
11. If you want to find all the places a variable is used, right-click on the variable, choose
references -> workspace (or project, hierarchy). This will find all of the variables of
that name even in other classes/java files.
12. In order to find where a variable is declared, click on it, then press F3. This also works
for finding where other classes are declared in other files.
13. To remove/delete whole lines, click anywhere in the line and hit Ctrl+d.
14. Many methods, objects, arrays, etc will autocomplete. For example an array, type the
letter “A” then ctrl+space.
15. For new private fields, you can generate Getters and Setters automatically by right clicking
->Source ->Generate Getters & Setters. The same works for creating Constructors with the
correct parameters.
16. If you want to override methods in a superclass, right click -> source -> Override/Implement
methods…

I’m sure these will help me develop a little faster. Just formatting and adjusting my brackets to make things prettier has taken up a lot of time. Not to mention finding where variables are declared. I’m not familiar with what Getters & Setters or Overriding methods are used for at this time but apparently they are just busy work that is unnecessary to write out every time.