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
77 changes: 61 additions & 16 deletions FindRange.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
/*
* File: FindRange.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the FindRange problem.
*/

import acm.program.*;

public class FindRange extends ConsoleProgram {
public void run() {
/* You fill this in */
}
}

/*

Choose a reason for hiding this comment

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

Program doesn't work accurately. It fails the following two cases described in the assignment:

If the user enters only one value before the sentinel, the program should report
that value as both the largest and smallest

So for example if someone enters 5 followed by 0 the program should report:
smallest: 5
largest: 5
Your program instead reports
smallest: 0
largest: 5

Also

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

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

import acm.program.*;

public class FindRange extends ConsoleProgram {

/* You fill this in */

//specifies the value for the sentinel
private static final int SENTINEL = 0;

//runs the program
public void run() {

println("This program finds the largest and smallest numbers.");

//initializes the minimum value
int min = 0;

//intializes the maximum value
int max = 0;

//iterates using while loop when the condition is true
while(true){

//prompts the user to enter a value
int val = readInt("enter val: ");

//checks if the first value entered is 0
if(val == SENTINEL) {
break;
}

//checks if the value is greater than the maximum value
//and then assigns it as the largest
if(val > max){
max = val;
//checks if the value is less than the minimum value
//and then assign it as the smallest
} else if (val < min) {
min = val;
}
}


//prints out the smallest value
println("smallest: " + min);

//prints out the largest value
println("largest: " + max);
}


}

46 changes: 30 additions & 16 deletions Hailstone.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
/*
* File: Hailstone.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the Hailstone problem.
*/

import acm.program.*;

public class Hailstone extends ConsoleProgram {
public void run() {
/* You fill this in */
}
}

/*
* File: Hailstone.java

Choose a reason for hiding this comment

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

Excellent solution!!!

* Name:
* Section Leader:
* --------------------
* This file is the starter file for the Hailstone problem.
*/

import acm.program.*;

public class Hailstone extends ConsoleProgram {
public void run() {
/* You fill this in */
println("Enter a number:");

int n = readInt("enter n: ");

while (n > 1) {
if ((n % 2) == 0){
n = (n / 2);
println("n is even, so I take half: " + n);
}
else if ((n % 2)== 1) {
n = (3 * n) + 1;
println("n is odd, so i make 3n + 1: " + n);
}
}
}
}

83 changes: 65 additions & 18 deletions ProgramHierarchy.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,65 @@
/*
* 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.*;

public class ProgramHierarchy extends GraphicsProgram {
public void run() {
/* You fill this in. */
}
}

/*
* 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.*;

public class ProgramHierarchy extends GraphicsProgram {

Choose a reason for hiding this comment

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

You should breakdown your program into smaller understandable methods. So the run method should have looked like this

public void run() {
    drawProgramBox();
    drawConsoleLine();
    drawConsoleBox();
    drawGraphicsLine();
    drawGraphicsBox();
    drawDialogLine();
    drawDialogBox();
}

And then you would have gone ahead and implement each method.



private static final int HEIGHT = 50;
private static final int WIDTH = 150;

public void run() {
int x;
int y;

x = (getWidth() - WIDTH) / 2;
y = (getHeight() - HEIGHT) /2;

GRect Rect = new GRect(x, y, WIDTH, HEIGHT);
add(Rect);

GLabel label1 = new GLabel("Program", (x + 50), (y + 30));
add(label1);


GLine Line1 = new GLine((x + 75), (y+50), (x+75), (y+100));
add(Line1);

GRect Rect2 = new GRect(x, (y+100), WIDTH, HEIGHT);
add(Rect2);

GLabel label2 = new GLabel("ConsoleProgram", (x + 30), (y + 130));
add(label2);

GLine Line2 = new GLine((x - 100), (y + 100), (x + 75), (y + 50));
add(Line2);

GRect Rect3 = new GRect((x - 175), (y + 100), WIDTH, HEIGHT);
add(Rect3);

GLabel label3 = new GLabel("GraphicProgram", (x - 145), (y + 130));
add(label3);

GLine Line3 = new GLine((x + 75), (y+50), (x+250), (y+100));
add(Line3);

GRect Rect4 = new GRect((x+175), (y+100), WIDTH, HEIGHT);
add(Rect4);

GLabel label4 = new GLabel("DialogProgram", (x + 210), (y + 130));

Choose a reason for hiding this comment

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

Generally you should avoid the use of constant numbers in your program. You have values like (210, 130, 175, 145...etc) throughout your code and this is not a good idea because:

  1. Someone reading your code will be confused how you came up with them
  2. If requirements change your program will be really hard to update. For example imagine for this exercise the requirements changed and we decide that the value for HEIGHT (50) and WIDTH (150) should be changed to HEIGHT = 60 and
    WIDTH = 180

If this happens your program will fail to draw the right diagram. To make it work you will have to go back and start changing all the numbers throughout your program. It should be possible to come up with a solution that works without you doing further modification. The ability of a program to be able to adapt to changes like that is called scalability. So in this case your solution is not scalable enough.

add(label4);




}
}

83 changes: 51 additions & 32 deletions Pyramid.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the
* sample run in the assignment, but you should make sure
* that changing these values causes the generated display
* to change accordingly.
*/

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

public class Pyramid extends GraphicsProgram {

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

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

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

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

/*

Choose a reason for hiding this comment

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

Great work on this one. Now this is a scalable solution (unlike your solution in ProgramHierarchy.java). Now if we decide to change BRICKS_IN_BASE to 20 (instead of 14) and BRICK_WIDTH to 35 (instead of 30) . Your program still draws the right pyramid without you having to go and change things within the code.

* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the
* sample run in the assignment, but you should make sure
* that changing these values causes the generated display
* to change accordingly.
*/

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

public class Pyramid extends GraphicsProgram {

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

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

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

public void run() {
/* You fill this in. */
double x = (getWidth() - BRICKS_IN_BASE * BRICK_WIDTH) / 2;
double y = getHeight() - BRICK_HEIGHT;

for (int row = 0; row < BRICKS_IN_BASE; row++){
drawRow(x, y, (BRICKS_IN_BASE - row));
y -= BRICK_HEIGHT;
x += BRICK_WIDTH / 2;
}
}

private void drawRow(double x, double y, int bricks){
for (int i = 0; i < bricks; i++){
drawBrick((x + i * BRICK_WIDTH), y);
}
}
private void drawBrick(double x, double y){
GRect rect = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
add(rect);
}
}


43 changes: 28 additions & 15 deletions PythagoreanTheorem.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
/*
* File: PythagoreanTheorem.java
* Name:
* Section Leader:
* -----------------------------
* This file is the starter file for the PythagoreanTheorem problem.
*/

import acm.program.*;

public class PythagoreanTheorem extends ConsoleProgram {
public void run() {
/* You fill this in */
}
}
/*
* File: PythagoreanTheorem.java
* Name:
* Section Leader:
* -----------------------------
* This file is the starter file for the PythagoreanTheorem problem.
*/

Choose a reason for hiding this comment

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

Excellent work.

import acm.program.*;

public class PythagoreanTheorem extends ConsoleProgram {
public void run() {
/* You fill this in */
println("Enter values to compute Pythagorean theorem.");
int a = readInt("enter a: ");

int b = readInt("enter b: ");

a = a * a;
b = b * b;

double c = Math.sqrt(a + b);
println("The answer is " + c + " . ");



}
}
62 changes: 45 additions & 17 deletions Target.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
/*
* 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() {
/* You fill this in. */
}
}
/*
* File: Target.java

Choose a reason for hiding this comment

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

The radius and pixel values should have been constants similar to what the starter code for the pyramid question had brick width, brick height and number of bricks on the base as constants. So you should have had something like this:

private static final int PIXELS_PER_INCH = 72;
private static final double RADIUS_OUTER_CIRCLE = 1.0;
private static final double RADIUS_WHITE_CIRCLE = 0.65;
private static final double RADIUS_INNER_CIRCLE = 0.3;

Also again this solution is not scalable (If the assignment requirements changed to have different radiuses your program will not work) and I don't think it uses the right measurements provided by the question (specifically the radiuses: 1.0, 0.65 and 0.3)

* 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() {
/* You fill this in. */
double a = (getWidth() - 72) / 2;
double b = (getHeight() - 72) / 2;

GOval outtercircle = new GOval(a, b, 72, 72);

outtercircle.setFilled(true);
outtercircle.setColor(Color.RED);

add(outtercircle);

double i = (getWidth() - 46.8) / 2;
double j = (getHeight() - 46.8) / 2;

GOval midcircle = new GOval(i, j, 46.8, 46.8);

midcircle.setFilled(true);
midcircle.setColor(Color.white);
add(midcircle);

double x = (getWidth() - 21.6) / 2;
double y = (getHeight() - 21.6) / 2;

GOval innercircle = new GOval(x, y, 21.6, 21.6);
innercircle.setFilled(true);
innercircle.setColor(Color.RED);

add(innercircle);

}
}