Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
18 changes: 17 additions & 1 deletion FindRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,24 @@
import acm.program.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great solution. However you missed one edge case:

If the user enters the sentinel on the very first input line, then no values have been
entered, and your program should display a message to that effect.

So if for example the user enters 0 the program should report:
No values has been entered.
Your program instead reports:
Smallest: 0
Largest: 0


public class FindRange extends ConsoleProgram {

private static final int SENTINEL = 0;
public void run() {
/* You fill this in */
println("This program finds the largest and smallest numbers");
int a = readInt("?:");
int min = a;
int max = a;
if (a!=SENTINEL){
while (true){
a = readInt("?:");
if (a==SENTINEL) break;
min = (a<min) ? a : min;
max = (a>max) ? a : max;
}
}
println("Smallest: "+min);
println("Largest: "+max);

}
}

22 changes: 20 additions & 2 deletions Hailstone.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* File: Hailstone.java
* Name:
Expand All @@ -10,7 +11,24 @@

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent solution.

public class Hailstone extends ConsoleProgram {
public void run() {
/* You fill this in */
println("This program finds the largest and smallest numbers");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you copied this from the Find Range problem because it is not true that this program finds the largest and smallest numbers.

int n = readInt("?:");
int cnt = 0;
while (true) {
if (isEven(n)) {
println(n + " is even, so I take half: " + n / 2);
n = n / 2;
} else {
println(n + " is odd, so I make 3n+1: " + (3 * n + 1));
n = (3 * n) + 1;
}
cnt++;
if (n==1) break;
}
println("the process took " +cnt+ " steps to reach 1");
}
}

private boolean isEven(int n) {
return n % 2 == 0;
}
}
46 changes: 43 additions & 3 deletions ProgramHierarchy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,53 @@
* This file is the starter file for the ProgramHierarchy problem.
*/

/*
* File: ProgramHierarchy.java
* Name:
* Section Leader:
* ---------------------------
* This file is the starter file for the ProgramHierarchy problem.
*/

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent solution.

public class ProgramHierarchy extends GraphicsProgram {
public class ProgramHierarchy extends GraphicsProgram {
public static final int BOX_WIDTH = 150;
public static final int BOX_HIGHT = 50;
public static final int SPACE = 20;

public void run() {
/* You fill this in. */
int y;
int x;

int x_top = (getWidth() - BOX_WIDTH) / 2;
int y_top = (getHeight() - 3 * BOX_HIGHT) / 2 - BOX_HIGHT;
GRect box_top = createLabelledBox("Program", x_top, y_top);

y = (getHeight() - 3 * BOX_HIGHT) / 2 + (BOX_HIGHT);
x = (getWidth() - 3 * BOX_WIDTH - 2 * SPACE) / 2;
GRect box = createLabelledBox("GraphicsProgram", x, y);
add(new GLine(x_top + box_top.getWidth() / 2, y_top + box_top.getHeight(), x + box.getWidth() / 2, y));

x = x + BOX_WIDTH + SPACE;
createLabelledBox("ConsoleProgram", x, y);
add(new GLine(x_top + box_top.getWidth() / 2, y_top + box_top.getHeight(), x + box.getWidth() / 2, y));

x = x + BOX_WIDTH + SPACE;
createLabelledBox("DialogProgram", x, y);
add(new GLine(x_top + box_top.getWidth() / 2, y_top + box_top.getHeight(), x + box.getWidth() / 2, y));

}
}

private GRect createLabelledBox(String content, int x, int y) {
GLabel label = new GLabel(content);
// label.setFont("Times-72");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out code should be deleted.

GRect box = new GRect(BOX_WIDTH, BOX_HIGHT);
add(box, x, y);
add(label, x + (box.getWidth() - label.getWidth()) / 2, y + (box.getHeight() + label.getAscent() / 2) / 2);
return box;

}
}
20 changes: 14 additions & 6 deletions Pyramid.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@
import acm.program.*;
import java.awt.*;


public class Pyramid extends GraphicsProgram {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent solution.


/** Width of each brick in pixels */
private static final int BRICK_WIDTH = 30;
private static final int BRICK_WIDTH = 50;

/** Width of each brick in pixels */
private static final int BRICK_HEIGHT = 12;
/** Height of each brick in pixels */
private static final int BRICK_HEIGHT = 30;

/** Number of bricks in the base of the pyramid */
private static final int BRICKS_IN_BASE = 14;

public void run() {
/* You fill this in. */

public void run() {
double y = (getHeight() - BRICK_HEIGHT);
for (int i = 0; i < BRICKS_IN_BASE ; i++) {
double x = (getWidth() - BRICK_WIDTH*(BRICKS_IN_BASE-i)) / 2;
for (int j = 0; j < BRICKS_IN_BASE-i ; j++) {
pause(10);
add(new GRect(BRICK_WIDTH , BRICK_HEIGHT), x+(BRICK_WIDTH*j), y-(BRICK_HEIGHT*i));
}
}
}
}

7 changes: 6 additions & 1 deletion PythagoreanTheorem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import acm.program.*;

public class PythagoreanTheorem extends ConsoleProgram {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent solution.


public void run() {
/* You fill this in */
println("Enter Values to compute Pythagorean theorem");
int a = readInt("a:");
int b = readInt("b:");
double c = Math.sqrt( (a*a) + (b*b));
println("c =" + c);
}
}
21 changes: 20 additions & 1 deletion Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,27 @@
import acm.program.*;
import java.awt.*;


public class Target extends GraphicsProgram {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Program works well...just a few coding techniques need to be changed.

public void run() {
/* You fill this in. */

double radius_1 = 72;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The radius should have been declared as constants outside of the run method, also you should have named them outer_radius, middle_radius and inner_radius, that way someone knows which radius corresponds to which circl. So something like

private static final double OUTER_RADIUS= 72;
private static final double MIDDLE_RADIUS = OUTER_RADIUS * 0.65;
private static final double INNER_RADIUS = OUTER_RADIUS * 0.3;

double radius_2 = radius_1*0.65;
double radius_3 = radius_1*0.3;
this.addCenteredCircle(this.createCircle(radius_1, 1));
this.addCenteredCircle(this.createCircle(radius_2, 2));
this.addCenteredCircle(this.createCircle(radius_3, 3));
}

private void addCenteredCircle(GOval circle) {
add(circle, (getWidth()-circle.getHeight())/2,(getHeight()-circle.getHeight())/2);
}

private GOval createCircle(double radius, int circle_number){
GOval circle = new GOval(radius,radius);
circle.setFilled(true);
circle.setColor(circle_number%2==0? Color.WHITE : Color.RED );
return circle;

}
}